To convert a file path into an NSURL:
1 |
NSURL *localURL = [NSURL fileURLWithPath:localPath]; |
To create a local path from an NSURL:
1 |
NSString *localPath = [localURL filePathURL]; |
NSURLs can also be created directly from an NSString:
1 2 3 |
NSURL *yourURL = [[NSURL alloc]initWithString:@"http://wpguru.co.uk"]; // or NSURL *yourURL = [NSURL URLWithString:@"http://wpguru.co.uk"]; |
If you’re ever tried to pass a local path into this method you’ll have noticed that it doesn’t work. Use the above methods instead.
You can also add path components to a URL, for example to reference your Documents directory:
1 2 3 |
// create a path and append a component NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"Documents"]; |
will give something like
1 |
/var/mobile/Applications/FCEB24E4-7705-4DE2-ABE8-3B50C717A483/Documents |
NSURLs have the same method called URLByAppendingPathComponent:
1 2 |
NSURL *myURL = [NSURL URLWithString:path]; myURL = [myURL URLByAppendingPathComponent:@"MyDirectory"]; |