OGeek|极客世界-中国程序员成长平台

标题: ios - 从锁定屏幕处理本地操作时无法从 iOS 应用沙箱读取 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:43
标题: ios - 从锁定屏幕处理本地操作时无法从 iOS 应用沙箱读取

为了处理本地操作,我的应用需要读取一个文件来决定要做什么。当本地操作来自与横幅或警报交互的用户时,这可以正常工作。当本地操作来自与锁定屏幕上的通知交互的用户时,它也可以工作,只要应用当前处于后台(而不是完全关闭)。

但是,如果应用完全关闭,并且用户尝试与锁定屏幕上的通知进行交互,我会收到一条错误提示

"The file ... couldn’t be opened because you don’t have permission to view it."

有谁知道可能出了什么问题,以及在这种情况下我如何才能访问以前保存的文件?

例如,下面的代码可以正常工作,除非由于锁定屏幕上的通知而调用了 handleActionWithIdentifier,并且应用程序已关闭。

// Handle local actionable notifications
- (void)applicationUIApplication *) application handleActionWithIdentifier: (NSString *) identifier forLocalNotification: (UILocalNotification *) notification completionHandler: (void (^)()) completionHandler
{
    //NSString *test = @"hello world";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent"test"];
    //[test writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"*********************");
    NSLog(@"trying to read test from handle action");
    NSLog(@"trying to read test from this location: %@", appFile);
    NSError *testerror;
    NSStringEncoding testencoding;
    NSString *testString = [NSString stringWithContentsOfFile:appFile usedEncoding:&testencoding error:&testerror];
    if (testerror) {
        NSLog(@"Failed to read storage file: %@", testerror);
    } else if (testencoding != NSUTF8StringEncoding) {
        NSLog(@"Incorect encoding of storage file.");
    } else {
        NSLog(testString);
    }
    NSLog(@"*********************");
}

发生错误时上述代码的完整输出如下:

Aug 11 14:39:42 iPhone Tandem[614] <Warning>: *********************
Aug 11 14:39:42 iPhone Tandem[614] <Warning>: trying to read test from handle action
Aug 11 14:39:42 iPhone Tandem[614] <Warning>: trying to read test from this location: /var/mobile/Containers/Data/Application/2C31417E-EF8E-4E0A-AC37-F4CB5BE9A4A5/Documents/test
Aug 11 14:39:42 iPhone Tandem[614] <Warning>: Failed to read storage file: Error Domain=NSCocoaErrorDomain Code=257 "The file “test” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/2C31417E-EF8E-4E0A-AC37-F4CB5BE9A4A5/Documents/test, NSUnderlyingError=0x145577d30 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
Aug 11 14:39:42 iPhone Tandem[614] <Warning>: *********************

如果我尝试从 application: didFinishLaunching: 读取文件也会出现同样的问题。



Best Answer-推荐答案


您应该检查文件保护属性。我的猜测是它设置为 NSFileProtectionCompleteNSFileProtectionCompleteUnlessOpen 这意味着在设备锁定时无法读取文件。您可以将属性设置为 NSFileProtectionNone 以在设备锁定时读取文件,但请注意它不会以加密状态存储。

见:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/#//apple_ref/doc/constant_group/File_Protection_Values

关于ios - 从锁定屏幕处理本地操作时无法从 iOS 应用沙箱读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38906776/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4