2

How can you get the host of an NSURL without the subdomains?

Here are some inputs.
The part that should remain is in bold, and what should be stripped is in italic:

server.com
www.server.com
media.server.com
host1.media.server.com

So far, sounds very easy to implement.
But it should also correctly handle the following inputs, as well as any weird case I didn't think about that is commonly accepted or specified in any relevant RFC.

server.co.uk
media.server.co.uk
127.0.0.1
...

Guillaume
  • 21,685
  • 6
  • 63
  • 95

2 Answers2

3

See Extract domain name from a host name This problem doesn't seem to have an easy solution.

Community
  • 1
  • 1
Heng-Cheong Leong
  • 824
  • 1
  • 8
  • 13
0

This doesn't answer the hard question, but if you're matching a URL to a white list of specific domains (like I was when I found this question) you can use code like this to check:

NSArray *domainsForVideo = @[@"youtube.com",
                             @"youtu.be",
                             @"videoschool.pvt.k12.md.us",
                             @"vimeo.com"];

NSString *host = [url host]; //where URL is some NSURL object

BOOL hostMatches = NO;

for (NSString *testHost in domainsForVideo) {
    if ([[host lowercaseString] isEqualToString:testHost]) hostMatches = YES;  // matches youtube.com exectly

    NSString *testSubdomain = [NSString stringWithFormat:@".%@",testHost];  // matches www.youtube.com but not fakeyoutube.com
    if ([[host lowercaseString] rangeOfString:testSubdomain].location != NSNotFound) hostMatches = YES;
}

if (hostMatches) {
    // this is a Video URL
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287