开发者

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

开发者 https://www.devze.com 2023-03-19 06:48 出处:网络
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 wh

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. Wh开发者_JS百科en 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消