在我今天的设备解锁的扩展中,这行代码按预期工作,从图像路径返回数据:
let imageData = NSData(contentsOfFile: path)
但是,当我的设备用密码锁定时,它返回零。当设备锁定时,有什么方法可以访问文件系统中的图像吗?我可以很好地访问 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);
}
在许多情况下,您通常不想这样做,但由于信息旨在从锁定屏幕上查看,因此我同意删除文件保护。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)