Reputation: 1076
For example:
"http://www.youtube.com" --> ".com"
"http://www.google.com.gr" --> ".com.gr"
"https://made.in.china.chinesewebsite.닷컴" --> ".닷컴"
The host of a URL is a dead-end.
NSURL *URL;
[URL host];
Upvotes: 0
Views: 149
Reputation: 131481
Get the host of the url, break it up at the .
delimiter, and take the last component.
- (NSString *) tldOfURL: NSURL *theURL; {
NSString *host = theURL.host;
NSArray *parts = [host componentsSeparatedByString: @"."];
if parts.count < 2 {
return nil;
}
return parts.lastObject;
}
At first my answer was to use NSURLComponents
, as in most cases that class does all the heavy lifting for you, but it looks like it doesn't have a mechanism for extracting the TLD.
Note that the above will return nil if the host name does not contain at least one period (.
) and will return an empty string if the host ends with a period. Both of these cases should be treated as errors.
P.S. I'm starting to get rusty at Objective-C syntax. Hard to believe, given how many years I's spent writing Objective-C!
Upvotes: 1