简体   繁体   中英

iOS Simulator Thinks Documents Directory is /var/root/Documents

I am trying to read all the files in the simulator in the Documents directory, but I get a Cocoa error 260. When I log [self applicationDocumentsDirectory] , it says /var/root/Documents. Anyone know why this would happen? It only happens when I run from the command line. When running in the simulator itself the code correctly outputs the right documents folder for the simulator.

UPDATE I am seeing a path of "file://localhost/Users/MyUserName/Documents" now. So weird...

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

The fix was to chmod -R 777 "/Users/MyUserName/Documents" and then change my method to this:

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    if (![[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:NULL])
    {
        NSLog(@"Error: Create documents folder failed %@", basePath);
    }

    return basePath;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM