在AppDelegate中使用方法管理我自己的应用时遇到问题。我希望我的文件在应用程序取消激活时加密,并在应用程序处于活动状态时解密,这是我的代码:
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// encrypt file
NSString *_path = path_to_file;
if ([PVIFileHelper isfileExisting:_path] && ![[NSUserDefaults standardUserDefaults] boolForKey:kFileEncrypted]) {
To_do_Encrypted_file;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFileEncrypted];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// decrypt data
NSString *_path = path_to_file;
if ([PVIFileHelper isfileExisting:_path] && [[NSUserDefaults standardUserDefaults] boolForKey:kFileEncrypted]) {
To_do_Decrypted_file;
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kFileEncrypted];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
kFileEncrypted
是保持解密文件状态的密钥。
但是当应用程序崩溃时(由于任何其他原因),我的文件被混淆了解密状态。应用解密和反向时,kFileEncrypted
值为YES。
我的错是什么?