NSURL baseURL returns nil. Any other way to get the actual base URL

北战南征 提交于 2019-12-09 05:19:44

问题


I think I don't understand the concept of "baseURL". This:

NSLog(@"BASE URL: %@ %@", [NSURL URLWithString:@"http://www.google.es"], [[NSURL URLWithString:@"http://www.google.es"] baseURL]);

Prints this:

BASE URL: http://www.google.es (null)

And of course, in the Apple docs I read this:

Return Value The base URL of the receiver. If the receiver is an absolute URL, returns nil.

I'd like to get from this example URL:

https://www.google.es/search?q=uiviewcontroller&aq=f&oq=uiviewcontroller&sourceid=chrome&ie=UTF-8

This base URL

https://www.google.es

My question is simple. Is there any cleaner way of getting the actual base URL without concatenating the scheme and the hostname? I mean, what's the purpose of base URL then?


回答1:


-baseURL is a concept purely of NSURL/CFURL rather than URLs in general. If you did this:

[NSURL URLWithString:@"search?q=uiviewcontroller"
       relativeToURL:[NSURL URLWithString:@"https://www.google.es/"]];

then baseURL would be https://www.google.es/. In short, baseURL is only populated if the NSURL is created using a method that explicitly passes in a base URL. The main purpose of this feature is to handle relative URL strings such as might be found in the source of a typical web page.

What you're after instead, is to take an arbitrary URL and strip it back to just the host portion. The easiest way I know to do this is a little cunning:

NSURL *aURL =  [NSURL URLWithString:@"https://www.google.es/search?q=uiviewcontroller"];
NSURL *hostURL = [[NSURL URLWithString:@"/" relativeToURL:aURL] absoluteURL];

This will give a hostURL of https://www.google.es/

I have such a method published as -[NSURL ks_hostURL] as part of KSFileUtilities (scroll down the readme to find it documented)

If you want purely the host and not anything like scheme/port etc. then -[NSURL host] is your method.




回答2:


Docs for BaseURL.

baseURL
Returns the base URL of the receiver.

- (NSURL *)baseURL
Return Value
The base URL of the receiver. If the receiver is an absolute URL, returns nil.

Availability
Available in iOS 2.0 and later.
Declared In
NSURL.h

Seems it only works for relative URLs.

You could possibly use ...

NSArray *pathComponents = [url pathComponents]

and then take the bits you want.

Or try...

NSString *host = [url host];



回答3:


It might be just me, but when I think further about the double-URL solution, it sounds like something that could stop working between OS updates. So I decided to share yet another solution, definitely not beautiful either, but I find it more readable by the general public since it doesn't rely on any hidden specificity of the framework.

if let path = URL(string: resourceURI)?.path {
  let baseURL = URL(string: resourceURI.replacingOccurrences(of: path, with: ""))
  ...
}



回答4:


you can use host method

NSURL *url = [[NSURL alloc] initWithString:@"http://www.hello.com"];

NSLog(@"Host:%@", url.host);

result:

Host:www.hello.com



回答5:


Here's a quick, easy, and safe way to fetch the base URL:

NSError *regexError = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http://.*/" options:NSRegularExpressionCaseInsensitive error:&regexError];

if (regexError) {
    NSLog(@"regexError: %@", regexError);
    return nil;
}

NSTextCheckingResult *match = [regex firstMatchInString:url.absoluteString options:0 range:NSMakeRange(0, url.absoluteString.length)];

NSString *baseURL = [url.absoluteString substringWithRange:match.range];


来源:https://stackoverflow.com/questions/15895734/nsurl-baseurl-returns-nil-any-other-way-to-get-the-actual-base-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!