Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设备锁定时,以当前扩展名加载文件_Ios_Swift_Nsfilemanager_Today Extension - Fatal编程技术网

Ios 设备锁定时,以当前扩展名加载文件

Ios 设备锁定时,以当前扩展名加载文件,ios,swift,nsfilemanager,today-extension,Ios,Swift,Nsfilemanager,Today Extension,在设备解锁的my today extension中,这行代码按预期工作,从映像路径返回数据: let imageData = NSData(contentsOfFile: path) 但是,当我的设备被密码锁定时,它返回nil。当设备锁定时,是否有办法访问文件系统中的图像?我可以访问UserDefaults,但不能访问共享组目录中的文件。下面是我如何创建路径,调用imagePath,在这两种情况下,该路径都正确填充了我期望的路径: func rootFilePath() -> Strin

在设备解锁的my today extension中,这行代码按预期工作,从映像路径返回数据:

let imageData = NSData(contentsOfFile: path)
但是,当我的设备被密码锁定时,它返回nil。当设备锁定时,是否有办法访问文件系统中的图像?我可以访问UserDefaults,但不能访问共享组目录中的文件。下面是我如何创建路径,调用
imagePath
,在这两种情况下,该路径都正确填充了我期望的路径:

func rootFilePath() -> String? {
    let manager = NSFileManager()
    let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier(GROUP_ID)
    if let unwrappedURL = containerURL {
        return unwrappedURL.path
    }
    else {
        return nil
    }
}

func imagePath() -> String? {
    let rootPath = rootFilePath()
    if let uPath = rootPath {
        return "\(uPath)/\(imageId).png"
    }
    else {
        return nil
    }
}

我刚想出来!您需要相应地设置文件权限:

NSFileManager *fm = [[NSFileManager alloc] init];
NSDictionary *attribs = @{NSFileProtectionKey : NSFileProtectionNone};
NSError *unprotectError = nil;

BOOL unprotectSuccess = [fm setAttributes:attribs
                             ofItemAtPath:[containerURL path]
                                    error:&unprotectError];
if (!unprotectSuccess) {
    NSLog(@"Unable to remove protection from file! %@", unprotectError);
}

在许多情况下,您通常不希望这样做,但由于信息是从锁定屏幕查看的,因此我可以删除文件保护。

有什么动作吗?我遇到了同样的问题。没有。。。还没有。这篇帖子上的那个家伙声称他根本没有问题,尽管我不相信,我觉得文件系统只是被密码锁住了——尽管我还没有搜索文档。你能分享完整的代码吗?如果“containerURL”为nil,那么您如何在其上设置属性?@strangetimes如果为nil,那么您遇到的问题比试图取消对不存在的文件的保护更大。我建议你问一个新问题。对不起,我以为你在上面说它实际上返回零?你是在它给你一个零的时候解除保护,还是在设备未锁定的时候解除保护?@strangetimes理想情况下,你在写它的时候解除保护。此特定代码适用于以前编写的代码,需要删除。这只能在设备解锁时完成。谢谢。知道了。这就是我需要的。我的SQLite数据库已经创建,需要删除上面的安全性。我猜这是一个永久性的改变。