检索根URL - NSString

时间:2011-02-17 21:03:47

标签: objective-c url nsstring

我正在尝试获取包含url的NSString的根URL。例如,如果传递的网址是secure.twitter.com,我希望返回twitter.com。这适用于我在下面的课程。但是对于一些较长的网址,它不起作用......

这是我的方法:

-

(NSString *)getRootDomain:(NSString *)domain
{
    NSString*output = [NSString stringWithString:domain];

    if ([output rangeOfString:@"www."].location != NSNotFound)
    {
    //if the www is still there, get rid of it
    output = [domain stringByReplacingOccurrencesOfString:@"www." withString:@""];
    }

    if ([output rangeOfString:@"http://"].location != NSNotFound)
    {
    //if the http is still there, get rid of it
    output = [domain stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    }

    if ([output rangeOfString:@"https://"].location != NSNotFound)
    {
    //if the https is still there, get rid of it
    output = [domain stringByReplacingOccurrencesOfString:@"https://" withString:@""];
    }

    NSLog(@"New: %@",output);

    NSArray*components = [output componentsSeparatedByString:@"."];

    if ([components count] == 2) //dandy, this is an easy one
    {
        return output;
    }

    if ([components count] == 3) //secure.paypal.com
    {
        NSString*newurl = [NSString stringWithFormat:@"%@.%@",[components objectAtIndex:1],[components objectAtIndex:2]];

        return newurl;
    }

    if ([components count] == 4) //secure.paypal.co.uk
    {
        NSString*newurl = [NSString stringWithFormat:@"%@.%@.%@",[components objectAtIndex:1],[components objectAtIndex:2],[components objectAtIndex:3]];

        return newurl;
    }


    //Path Components will return the root url in its array in object 0 (usually)

    NSArray*path_components = [output pathComponents];  

    return [path_components objectAtIndex:0];
}

如何为任何网址开展此工作?

6 个答案:

答案 0 :(得分:4)

您可以考虑利用NSURL和NSString来执行此操作,如下所示:

(NSString *)getRootDomain:(NSString *)domain
{
    // Return nil if none found.
    NSString * rootDomain = nil;

    // Convert the string to an NSURL to take advantage of NSURL's parsing abilities.
    NSURL * url = [NSURL URLWithString:domain];

    // Get the host, e.g. "secure.twitter.com"
    NSString * host = [url host];

    // Separate the host into its constituent components, e.g. [@"secure", @"twitter", @"com"]
    NSArray * hostComponents = [host componentsSeparatedByString:@"."];
    if ([hostComponents count] >=2) {
        // Create a string out of the last two components in the host name, e.g. @"twitter" and @"com"
        rootDomain = [NSString stringWithFormat:@"%@.%@", [hostComponents objectAtIndex:([hostComponents count] - 2)], [hostComponents objectAtIndex:([hostComponents count] - 1)]];
    }

    return rootDomain;
}

答案 1 :(得分:0)

NSArray *array = [[newURL host] componentsSeparatedByString: @"."];
NSLog(@"%@", [NSString stringWithFormat:@"%@.%@", [array objectAtIndex:[array count]-2], [array objectAtIndex:[array count]-1]]);

答案 2 :(得分:0)

要获取网址的部分内容,此处为the perfect answer。但是,您希望拆分URL的主机部分。使用NSURL,您可以获得如下主机:url.host

从主持人那里,无法知道哪个是重要部分。可能有一个名为secure.twitter.com的主机和另一个名为twitter.host1.com的主机。

如果您有自定义规范,请删除“安全”。如果它是主机前缀,请实现它。但是如果你想找到一个通用的解决方案,我宁愿保存整个主机字符串。

答案 3 :(得分:0)

网址中的第一个点始终是域名的一部分,我们可以使用它来制作这个简单而高效的方法。 (它适用于子域和多个虚线TLD,如co.uk)

-(NSString*)domainFromUrl:(NSString*)url
{
    NSArray *first = [url componentsSeparatedByString:@"/"];
    for (NSString *part in first) {
        if ([part rangeOfString:@"."].location != NSNotFound){
            return part;
        }
    }
    return nil;
}
NSLog(@"%@",[self domainFromUrl:@"http://foobar1.com/foo/"]);
NSLog(@"%@",[self domainFromUrl:@"http://foobar2.com/foo/bar.jpg"]);
NSLog(@"%@",[self domainFromUrl:@"http://foobar3.com/"]);
NSLog(@"%@",[self domainFromUrl:@"http://foobar4.com"]);
NSLog(@"%@",[self domainFromUrl:@"foobar5.com"]);




2012-08-15 23:25:14.769 SandBox[9885:303] foobar1.com
2012-08-15 23:25:14.772 SandBox[9885:303] foobar2.com
2012-08-15 23:25:14.772 SandBox[9885:303] foobar3.com
2012-08-15 23:25:14.773 SandBox[9885:303] foobar4.com
2012-08-15 23:25:14.773 SandBox[9885:303] foobar5.com

答案 4 :(得分:0)

您需要检查后缀。

示例:

#import "NSURL+RootDomain.h"

@implementation NSURL(RootDomain)

- (NSString *)rootDomain {
    NSArray *hostComponents = [self.host componentsSeparatedByString:@"."];
    NSArray *suffixs = [NSArray arrayWithObjects:@"net", @"com", @"gov", @"org", @"edu", @"com.cn", @"me", nil];

    if ([hostComponents count] >= 2) {
        if ([hostComponents[hostComponents.count - 2] isEqualToString:@"cn"]) {
            if ([suffixs containsObject:[hostComponents lastObject]]) {
                return [NSString stringWithFormat:@"%@.%@.%@", hostComponents[hostComponents.count - 3], hostComponents[hostComponents.count - 2], hostComponents.lastObject];
            } else {
                return [NSString stringWithFormat:@"%@.%@", hostComponents[hostComponents.count - 2], hostComponents.lastObject];
            }
        } else {
            return [NSString stringWithFormat:@"%@.%@", hostComponents[hostComponents.count - 2], hostComponents.lastObject];
        }
    }

    return self.host;
}

答案 5 :(得分:0)

[[NSURL URLWithString:@"http://someurl.com/something"] host]

输出 someurl.com