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


i think don't understand 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 apple docs read this:

return value base url of receiver. if receiver absolute url, returns nil.

i'd 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 simple. there cleaner way of getting actual base url without concatenating scheme , hostname? mean, what's purpose of base url then?

-baseurl concept purely of nsurl/cfurl rather urls in general. if did this:

[nsurl urlwithstring:@"search?q=uiviewcontroller"        relativetourl:[nsurl urlwithstring:@"https://www.google.es/"]]; 

then baseurl https://www.google.es/. in short, baseurl populated if nsurl created using method explicitly passes in base url. main purpose of feature handle relative url strings such might found in source of typical web page.

what you're after instead, take arbitrary url , strip host portion. easiest way know little cunning:

nsurl *aurl =  [nsurl urlwithstring:@"https://www.google.es/search?q=uiviewcontroller"]; nsurl *hosturl = [[nsurl urlwithstring:@"/" relativetourl:aurl] absoluteurl]; 

this give hosturl of https://www.google.es/

i have such method published -[nsurl ks_hosturl] part of ksfileutilities (scroll down readme find documented)

if want purely host , not scheme/port etc. -[nsurl host] method.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -