是否可以从NSURL获得域名和顶级域名?没有www或其他什么?

时间:2013-12-06 18:04:25

标签: ios objective-c nsurl

说我有http://www.youtube.comhttp://youtube.comhttps://www1.youtube.com/moretext。如何编写支票以查看所有这些网址是否来自youtube.com?

我试过了url.host,似乎保留了www.等等,这不是我想要的。

我基本上只想说:

if ([url isFromWebsite:@"youtube.com"]) {
    // Do things.
}

这可能吗?

4 个答案:

答案 0 :(得分:4)

这是NSURL上未经测试的类别,它将提供您想要的方法。

@implementation NSURL (IsFromWebsite)

- (BOOL) isFromWebsite:(NSString *)domain
{
    NSArray *selfHostComponents = [[self host] componentsSeparatedByString:@"."];
    NSArray *targetHostComponents = [domain componentsSeparatedByString:@"."];

    NSInteger selfComponentsCount = [selfHostComponents count];
    NSInteger targetComponentsCount = [targetHostComponents count];
    NSInteger offset = selfComponentsCount - targetComponentsCount;

    if (offset < 0)
        return NO;

    for (NSUInteger i = offset; i < selfComponentsCount; i++) {
        if (![selfHostComponents[i] isEqualToString:targetHostComponents[i - offset]])
            return NO;
    }

    return YES;
}

@end

编辑:Jesse Rusak建议的另一种(也是未经测试的)做同样事情的方法:

@implementation NSURL (IsFromWebsite)

- (BOOL) isFromWebsite:(NSString *)domain
{
    NSArray *selfComponents = [[self host] componentsSeparatedByString:@"."];
    NSArray *targetComponents = [domain componentsSeparatedByString:@"."];
    NSInteger sizeDifference = [selfComponents count] - [targetComponents count];

    if (sizeDifference < 0)
        return NO;

    return [[selfComponents subarrayWithRange:NSMakeRange(sizeDifference, [targetComponents count])]
            isEqualToArray:targetComponents];
}

@end

答案 1 :(得分:1)

NSURL - 实际上是URL - 并不太关心URL主机部分的实际内容;这取决于客户解释和处理。因此,您可以自行处理此信息。

这是一个很简单的方法:

- (BOOL)isYouTubeURL:(NSURL *)url;
{
    NSString *host = url.host;

    // Test the simple case of the domain being a direct match
    // Case-insensitive, as domain names are defined to be
    if ([host caseInsensitiveCompare:@"youtube.com"] == NSOrderedSame) {
        return YES;
    }

    // And now the more complex case of a subdomain
    // Look back from just the end of the string
    NSStringCompareOptions options = NSBackwardsSearch|NSAnchoredSearch|NSCaseInsensitiveSearch;
    NSRange range = [host rangeOfString:@".youtube.com" options:options];
    return (range.location != NSNotFound);
}

如果需要,您可以通过仅搜索域进行优化,然后查看前一个字符是否不存在(上面的情况#1),或者之前有.(上面的情况#2) 。更进一步,CFURL有API告诉你主机所在的原始字符串中的位置,并可以直接搜索它。

答案 2 :(得分:0)

如果有人在Swift 3中需要bdesham的答案。

extension URL {
    func isFromWebsite(domain: String) -> Bool {
        var selfHostComponents = self.host!.components(separatedBy: ".")
        var targetHostComponents = domain.components(separatedBy: ".")
        let selfComponentsCount = selfHostComponents.count
        let targetComponentsCount = targetHostComponents.count
        let offset = selfComponentsCount - targetComponentsCount
        if offset < 0 {
            return false
        }
        for i in offset..<selfComponentsCount {
            if !(selfHostComponents[i] == targetHostComponents[i - offset]) {
                return false
            }
        }
        return true
    }
}

答案 3 :(得分:-4)

您可以将以下代码用作NSString类别

- (BOOL)isFromWebsite: (NSString *)website {
     NSRange searchResult = [self rangeOfString:website];
     if (searchResult.location != NSNotFound) {
         return YES;
     }
     return NO;
}

如果是用例,您也可以使用不区分大小写的比较。 希望这会有所帮助。