Let’s see some useful method about NSURL Return the name of a file (with or without extension) from NSURLRequest:
1 2 3 4 5 6 7 8 9 |
- (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:
1 2 3 4 5 6 |
- (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:
1 2 3 4 5 6 7 8 9 10 11 |
// 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]; |