Question

I've been at this all night and its driving me nuts, I have an application which is called by openURL calls from Safari. Safari sends me the fileURL and copies the file in to Documents/Inbox and I then want to read the contents of that file in to a string. However after trying many varied ways of locating / accessing the file ...... my app still says the file doesn't exist.

So I get sent the fileURL from Safari like this ..

file://localhost/Users/plasma/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/EE3A1801-98CF-4C7A-8ED1-639A81316B1C/Documents/Inbox/HereIsYourFile.txt

If I browse to this path in the terminal I can confirm that is the location and the file exists.

macbook:Inbox plasma$ pwd

/Users/plasma/Library/Application Support/iPhone Simulator/6.0/Applications/EE3A1801-98CF-4C7A-8ED1-639A81316B1C/Documents/Inbox

macbook:Inbox plasma$ ls
HereIsYourFile.txt

I put this path in a String called receivedURL but trying to read the file just gives an empty string and an error 260.

NSString *myFileContents = [NSString stringWithContentsOfFile:receivedURL
                                             encoding:NSUTF8StringEncoding
                                             error:&error];

This infers the file is not there, so I tried proving that it exists in my app ...

        NSFileManager *filemgr;
        filemgr = [NSFileManager defaultManager];

        if ([filemgr fileExistsAtPath:receivedURL ] == YES) {
            NSLog (@"File exists");
        } else {
            NSLog (@"File not found");
        }

Its says it doesn't exist!!!! I can see it .. its there, so why can't my app. I read on the documentation page Here that files in this location should be visible and readable by the application.

Any help on accessing this file would be greatly appreciated.

Thanks In Advance

Plasma

Was it helpful?

Solution

How do you create receivedURL from the NSURL given to you when the app is launched? You should be doing this:

NSString *receivedURL = [url path];

Make sure that receivedURL doesn't have the file:// at the beginning.

Another option is to use the NSURL to load the string from the file:

NSString *myFileContents = [NSString stringWithContentsOfURL:url
                                         encoding:NSUTF8StringEncoding
                                         error:&error];

instead of converting the URL to a file path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top