views:

49

answers:

2

I would like to save a file downloaded from the internet in iPhone.

Can I use the url as the file name? If not, what transformation should I apply to the url to obtain a valid file name?

I need to find the local copy of the file later using its url.

Edit: Not asking where or how to write a file to disk. The question is limited to getting a valid file name from an url.

+1  A: 

Can I use the url as the file name?

You can use just about anything you want for a file name. As long as it's a valid file name for the underlying file system.

If not, what transformation should I apply to the url to obtain a valid file name?

This is unanswerable because there's no way to specify what "valid" means here.

I need to find the local copy of the file later using its url.

You probably want to save it to your application's Documents directory. There already are plenty of questions relating to saving files on iPhone here on Stack Overflow, and I urge you to read some of them.

Shaggy Frog
If there's no whay to specify what "valid" means, then how would you pick a name for the temporary file? The question is: how do I get a valid file name from the url?
hgpc
A valid name is anything the filesystem on the iPhone will accept. If you have an `NSURL` object, you could try using the `lastPathComponent` method.
Shaggy Frog
That's what I'm (kinda) using right now, but it doesn't produce an unique name. Two urls with the same last path component give the same file name.
hgpc
So... add something to the name to make it unique
Shaggy Frog
Yes, most likely something taken from the rest of the url. That's why I'm asking which transformation should I apply to the url to get a valid file name.
hgpc
+1  A: 

For lack of a better method, I'm using a persistent dictionary to map the urls to unique names.

hgpc