How do I get around NSCocoaErrorDomain:257 when pulling a file from the Files app?

后端 未结 1 1755
醉话见心
醉话见心 2021-01-12 22:43

I\'m trying to access a file to pull a copy into my app so that users can associate it with relevant information. It used to work just fine up until recently, and now I sudd

相关标签:
1条回答
  • 2021-01-12 23:11

    It turns out there's a "using" function that tells the app its accessing files outside of it's sandbox. The methods startAccessingSecurityScopedResource and stopAccessingSecurityScopedResource on NSURL need to be wrapped around the code using the url, like so:

    BOOL isAcccessing = [url startAccessingSecurityScopedResource];
    NSError* error = nil;
    NSString *path = [url path];
    NSData *data = [NSData dataWithContentsOfFile:path options: 0 error: &error];
    if(data == nil) {
        NSLog(@"Failed to read file, error %@", error);
    }
    if (isAccessing) {
        [url stopAccessingSecurityScopedResource];
    }
    

    I'm not sure if there's anything specific to iOS 13 that requires this when it didn't previously, but that is the only real change between it working and not working.

    0 讨论(0)
提交回复
热议问题