轉載自:http://blog.csdn.net/yiyaaixuexi/article/details/19486335
資料保護API
1)檔案保護
- NSDictionary *attributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
- forKey:NSFileProtectionKey];
- [[NSFileManager defaultManager] setAttributes:attributes
- ofItemAtPath:filePath
- error:nil];
- //檔案保護等級屬性清單
- NSFileProtectionNone //檔案未受保護,随時可以通路 (Default)
- NSFileProtectionComplete //檔案受到保護,而且隻有在裝置未被鎖定時才可通路
- NSFileProtectionCompleteUntilFirstUserAuthentication //檔案收到保護,直到裝置啟動且使用者第一次輸入密碼
- NSFileProtectionCompleteUnlessOpen //檔案受到保護,而且隻有在裝置未被鎖定時才可打開,不過即便在裝置被鎖定時,已經打開的檔案還是可以繼續使用和寫入
應用執行個體
把一段資訊infoStrng字元串寫進檔案,然後通過Data Protection API設定保護。
- NSString *documentsPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DataProtect"];
- [infoString writeToFile:filePath
- atomically:YES
- encoding:NSUTF8StringEncoding
- error:nil];
- NSDictionary *attributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
- forKey:NSFileProtectionKey];
- [[NSFileManager defaultManager] setAttributes:attributes
- ofItemAtPath:filePath
- error:nil];
裝置鎖屏(帶密碼保護)後,即使是越獄機,在root權限下cat讀取那個檔案資訊也會被拒絕。