Here is my code:
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if (url != nil && [url isFileURL]) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if ( [[NSFileManager defaultManager] isReadableFileAtPath:[url absoluteString]] ) {
NSLog(@"READABLE!");
[[NSFileManager defaultManager] copyItemAtPath:[url absoluteString] toPath:[documentsPath stringByAppendingString:@"/timecode.xml"] error:nil];
} else {
NSLog(@"NOT READABLE!");
}
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[documentsPath stringByAppendingString:@"/timecode.xml"]];
if (fileExists) {
NSLog(@"THERE!");
} else {
NSLog(@"NOT THERE!");
}
}
}
For some reason when I open a file from the mail app (or any other app, including good reader) into my app, it says it isn't readable. And obviously, the file doesn't exist.
Any ideas why this is happening?
Because an URL is not a path.
[url absoluteString]
looks like
file:///var/mobile/XXXXXXXX/MyApp.app/Documents/foo.txt
You need to use
[url path]
instead.
Also, two comments:
url != nil && [url isFileURL]
is highly superfluous.
[url isFileURL]
will return NO
anyway if the URL is nil
(since messaging nil
always returns zero).
Two:
[documentsPath stringByAppendingString:@"/timecode.xml"]
Don't reinvent the wheel/try to guess the path separator. This line should be
[documentsPath stringByAppendingPathComponent:@"timecode.xml"]