2014-09-19 91 views
1

在我今天的擴展與我的設備解鎖,這行代碼按預期運行,從圖像路徑返回數據:加載文件時,設備被鎖定

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 
    } 
} 
+0

對此有何動靜?我遇到了同樣的問題。 – 2014-10-02 02:29:47

+0

不...還沒有。而這個帖子上的人聲稱他根本沒有問題,儘管我不相信它,我覺得文件系統只是用密碼鎖定 - 雖然我還沒有搜索過文檔。 http://stackoverflow.com/a/25844434/431271 – ccwasden 2014-10-02 13:49:34

回答

0

我想通了!您需要設置相應的文件權限:

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); 
} 

在你通常不會想這樣做很多時候,但由於信息意在從鎖定屏幕看到,我OK與刪除文件保護。

+0

你能分享完整的代碼嗎?如果'containerURL'爲零,那麼你如何在其上設置屬性? – strangetimes 2015-08-28 02:55:19

+0

@strangetimes如果它是零,那麼你有一個更大的問題比試圖解除不存在的文件。我建議問一個新問題。 – 2015-08-28 18:03:27

+0

對不起,我以爲你上面說過,它實際上返回零?當設備沒有被鎖定時,是否在你給它一個零的時候取消它的保護,或者你之前是否做了保護? – strangetimes 2015-08-28 18:40:38