2011-02-10 64 views
2

你怎麼能獲得NSURL的主機沒有子域名沒有子域名的NSURL的主機

這裏有一些輸入。
應該保留的部分是勇敢的,哪些應該被剝奪是斜體:

server.com
WWW。server.com
media。server.com
host1.media。server.com

到目前爲止,聽起來很容易實現。
但它也應該正確處理以下輸入,以及我沒有想到的任何奇怪的情況,這在任何相關的RFC中都被普遍接受或指定。

server.co.uk
媒體。server.co.uk
127.0.0.1
...

回答

0

這不回答這個問題很難回答,但如果你匹配一個URL到特定域的白名單(像我是當我發現這個問題),你可以使用這樣的代碼來檢查:

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 
}