Let’s see some useful method about NSURL
Return the name of a file (with or without extension) from NSURLRequest
:
- (NSString *)getFilenameFromRequest:(NSURLRequest *)request avoidExtension:(BOOL)avoidExtension
{
NSString *filePath = [[[request URL] path] lastPathComponent];
if (avoidExtension) {
NSArray *a = [filePath componentsSeparatedByString:@"."];
return [a objectAtIndex:0];
}
return filePath;
}
Return the file extension from NSURLRequest
:
- (NSString *)getExtensionFromRequest:(NSURLRequest *)request
{
NSString *filePath = [[[request URL] path] lastPathComponent];
NSArray *a = [filePath componentsSeparatedByString:@"."];
return [a objectAtIndex:1];
}
Finally, How to convert an address from NSString
to NSURL
and vice versa:
// filename
NSString *filePath = @"/path/filetext.txt";
// NSString to NSURL
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filePath]; // do release
// or
NSURL fileURL = [NSURL fileURLWithPath: filePath]; // autorelease
// NSURL to NSString
NSString *filePath = [fileUrl absoluteString];