Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get root of website from NSString or NSUrl

Tags:

ios

cocoa

nsurl

Any ideas how I can get the root of a website from an NSString or an NSURL? So if my URL was http://www.foo.com/bar/baragain how would I get http://www.foo.com/?

like image 713
trx25 Avatar asked Nov 24 '10 04:11

trx25


2 Answers

By using [url scheme] and [url host] like so:

NSURL *url = [NSURL URLWithString:@"http://www.foo.com/bar/baragain"];
NSLog(@"Base url: %@://%@", [url scheme], [url host]);
// output is http://www.foo.com
like image 112
Domestic Cat Avatar answered Oct 17 '22 15:10

Domestic Cat


NSURL *url = [NSURL URLWithString:@"http://www.foo.com/bar/baragain"];
NSURL *root = [NSURL URLWithString:@"/" relativeToURL:url];
NSLog(@"root = '%@'", root.absoluteString); // root = 'http://www.foo.com/'
like image 41
Hafthor Avatar answered Oct 17 '22 15:10

Hafthor