卸载应用程序时删除钥匙串项目 [英] Delete keychain items when an app is uninstalled

查看:28
本文介绍了卸载应用程序时删除钥匙串项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 idandersen 的 scifihifi-iphone 代码作为钥匙串并使用

I am using idandersen's scifihifi-iphone code for keychain and save password using

[SFHFKeychainUtils storeUsername:@"User" andPassword:@"123"
              forServiceName:@"TestService" updateExisting:YES error:&error];

当我从设备中删除应用程序时,密码仍保留在钥匙串中.

When I delete the application from the device, the password remains in the keychain.

当用户从设备中删除应用程序时,我想从钥匙串中删除密码.我该怎么做?

I want to remove the password from the keychain when the user deletes the application from the device. How can I do this?

推荐答案

您可以利用 NSUserDefaults 通过卸载应用程序清除这一事实.例如:

You can take advantage of the fact that NSUserDefaults are cleared by uninstallation of an app. For example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Clear keychain on first run in case of reinstallation
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"]) {
        // Delete values from keychain here
        [[NSUserDefaults standardUserDefaults] setValue:@"1strun" forKey:@"FirstRun"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    //...Other stuff that usually happens in didFinishLaunching
}

这会在您的应用程序第一次运行时检查并设置 NSUserDefaults 中的FirstRun"键/值(如果尚未设置).有一条注释,您应该在其中放置代码以从钥匙串中删除值.可以调用 Synchronize 以确保FirstRun"键/值立即持久化,以防用户在系统持久化之前手动终止应用程序.

This checks for and sets a "FirstRun" key/value in NSUserDefaults on the first run of your app if it's not already set. There's a comment where you should put code to delete values from the keychain. Synchronize can be called to make sure the "FirstRun" key/value is immediately persisted in case the user kills the app manually before the system persists it.

这篇关于卸载应用程序时删除钥匙串项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆