Remove last portion of the NSURL: iOS

Shailesh picture Shailesh · May 11, 2013 · Viewed 10.8k times · Source

I am trying to remove just the last part of the url, Its a FTP URL.

Suppose, I have a URL like: > ftp://ftp.abc.com/public_html/somefolder/. After removing the last portion I should have it as: ftp://ftp.abc.com/public_html/.

I have tried using stringByDeletingLastPathComponenet and URLByDeletingLastPathComponent, but they dont remove the last portion correctly. They change the entire looks of the url.

for instance, after using the above said methods, here is the URL format i get ftp:/ftp.abc.com/public_html/. It removes one "/" in "ftp://", which is crashing my program.

How is it possible to removve just the last part without disturbing the rest of the URL ?

UPDATE:

NSURL * stringUrl = [NSURL URLWithString:string];
NSURL * urlByRemovingLastComponent = [stringUrl URLByDeletingLastPathComponent];
NSLog(@"%@", urlByRemovingLastComponent);

Using above code, I get the output as :- ftp:/ftp.abc.com/public_html/

Answer

mxweas picture mxweas · May 11, 2013

Hmm. URLByDeletingLastPathComponent works perfectly given the above input.

NSURL *url = [NSURL URLWithString:@"ftp://ftp.abc.com/public_html/somefolder/"];
NSLog(@"%@", [url URLByDeletingLastPathComponent]);

returns

ftp://ftp.abc.com/public_html/

Do you have some sample code that is yielding improper results?

Max