function to get the file name of an URL

Solution 1:

The following line does the job if url is a NSString:

NSString *filename = [url lastPathComponent];

If url is a NSURL, then the following does the job:

NSString *filename = [[url path] lastPathComponent];

Solution 2:

Try this:

Edit: from blow comment

NSString *url = @"http://www.google.com/a.pdf";
NSArray *parts = [url componentsSeparatedByString:@"/"];
NSString *filename = [parts lastObject];

Solution 3:

I think if you have already had the NSURL object, there is lastPathComponent method available from the iOS 4 onwards.

NSURL *url = [NSURL URLWithString:@"http://www.google.com/a.pdf"];
NSString *filename = [url lastPathComponent];

Solution 4:

Swift 3

Let's say that your url is http://www.google.com/a.pdf

    let filename = url.lastPathComponent

    \\filename = "a.pdf"