1

I want to get the current domain of UIWebView

For example:

http://a.m.tmall.com/i15104234454.htm?sid=a0c935526ab19ee7051225579c90cf96&spm=875.124399.275145.1 -> tmall.com

Is there any reliable way to do this?

I saw someone suggest this:

 NSString *currentURL = myWebView.request.URL.absoluteString;

I know regex could do it. Any other way?

pasawaya
  • 11,515
  • 7
  • 53
  • 92
jeswang
  • 1,017
  • 14
  • 26

2 Answers2

5

First, what do mean when you say you want to get the domain. In essence, how rigorous do you want to be? Domain parsing can be very complex (see: how to get domain name from URL).

At the very least start, with URL.absoluteURL.host instead of URL.absoluteString. That way you don't have to extract the host from the URL string.

You may also want use [webView stringByEvaluatingJavaScriptFromString:@"document.domain"] as your starting point. At least that way you will know what the browser thinks the domain is.

Hope any of that mess helps.

Community
  • 1
  • 1
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
1

In Swift you get host URL from web view by :

webView.url?.host

more info here