Question

I'm trying to cut a URL down from this:

http://www.huffingtonpost.com/2013/08/01/edward-snowden-russia_n_3688225.html

to this:

http://www.huffingtonpost.com/

using NSMakeRange(Location, Length)

My code:

if ([URLContainer hasPrefix:@"http://www."]) {            
    NSLog(@"URL Detected");

    NSUInteger Location = 0;            
    NSUInteger Length = 29;

    NSString *URLCutDown =  [URLContainer substringWithRange: NSMakeRange(Location, Length)];

    Label = URLCutDown;
}

My problem is that I don't quite know how to make the end of Length stop at the forward slash (/).

Was it helpful?

Solution

Why not use NSURL's baseURL method:

NSString * baseURL = [URLContainer baseURL];

OTHER TIPS

Instead of splitting with a range you should use the methods of NSURL such as host, scheme, ...

Here's a slightly cunning solution:

NSURL *startingPoint = [NSURL URLWithString:@"http://www.huffingtonpost.com/2013/08/01/edward-snowden-russia_n_3688225.html"];
NSURL *result = [[NSURL URLWithString:@"/" relativeToURL:startingPoint] absoluteURL];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top