Monday, April 22, 2013

NSFileManager isReadableFileAtPath example ios


isReadableFileAtPath:

Returns a Boolean value that indicates whether the invoking object appears able to read a specified file.
- (BOOL)isReadableFileAtPath:(NSString *)path
Parameters
path
A file path.
Return Value of [NSFileManager isReadableFileAtPath]
YES if the current process has read privileges for the file at path; otherwise NO if the process does not have read privileges or the existence of the file could not be determined.
Discussion of [NSFileManager isReadableFileAtPath]
If the file at path is inaccessible to your app, perhaps because it does not have search privileges for one or more parent directories, this method returns NO. This method traverses symbolic links in the path. This method also uses the real user ID and group ID, as opposed to the effective user and group IDs, to determine if the file is readable.
Example of [NSFileManager isReadableFileAtPath]
-(BOOL)application:(UIApplication *)application
           openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {

    if ([url isFileURL]) {
        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        if ( [[NSFileManager defaultManager] isReadableFileAtPath:[url path]] ) {
            NSLog(@"READABLE!");
            [[NSFileManager defaultManager] copyItemAtPath:[url path] 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!");
        }
    }
}