ios 卸载应用程序时删除钥匙串项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4747404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:36:40  来源:igfitidea点击:

Delete keychain items when an app is uninstalled

iosobjective-ckeychain

提问by enc

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

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

[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?

当用户从设备中删除应用程序时,我想从钥匙串中删除密码。我怎样才能做到这一点?

回答by Amro

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

你可以利用这一事实的优点NSUserDefaults通过一个应用程序的卸载清除。例如:

- (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
}

This checks for and sets a "FirstRun" key/value in NSUserDefaultson 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.

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

回答by bwcooley

For users looking for a Swift 3.0version of @amro's answer:

对于正在寻找@amro 答案的Swift 3.0版本的用户:

let userDefaults = UserDefaults.standard

if !userDefaults.bool(forKey: "hasRunBefore") {
     // Remove Keychain items here

     // Update the flag indicator
     userDefaults.set(true, forKey: "hasRunBefore")
}

*note that synchronize() function is deprecated

*请注意,不推荐使用同步()函数

回答by Shane Fitzgibbon

There is no trigger to perform code when the app is deleted from the device. Access to the keychain is dependant on the provisioning profile that is used to sign the application. Therefore no other applications would be able to access this information in the keychain.

从设备中删除应用程序时,不会触发执行代码。对钥匙串的访问取决于用于签署应用程序的配置文件。因此,没有其他应用程序能够访问钥匙串中的此信息。

It does not help with you aim to remove the password in the keychain when the user deletes application from the device but it should give you some comfort that the password is not accessible (only from a re-install of the original application).

当用户从设备中删除应用程序时,它不会帮助您删除钥匙串中的密码,但它应该让您感到一些安慰,即密码无法访问(只能通过重新安装原始应用程序)。

回答by rsc

For those looking for a Swift version of @amro's answer:

对于那些正在寻找@amro 答案的 Swift 版本的人:

    let userDefaults = NSUserDefaults.standardUserDefaults()

    if userDefaults.boolForKey("hasRunBefore") == false {

        // remove keychain items here


        // update the flag indicator
        userDefaults.setBool(true, forKey: "hasRunBefore")
        userDefaults.synchronize() // forces the app to update the NSUserDefaults

        return
    }

回答by InquisitorJax

C# Xamarin version

C# Xamarin 版本

    const string FIRST_RUN = "hasRunBefore";
    var userDefaults = NSUserDefaults.StandardUserDefaults;
    if (!userDefaults.BoolForKey(FIRST_RUN))
    {
        //TODO: remove keychain items
        userDefaults.SetBool(true, FIRST_RUN);
        userDefaults.Synchronize();
    }

... and to clear records from the keychain (TODO comment above)

...并清除钥匙串中的记录(上面的 TODO 注释)

        var securityRecords = new[] { SecKind.GenericPassword,
                                    SecKind.Certificate,
                                    SecKind.Identity,
                                    SecKind.InternetPassword,
                                    SecKind.Key
                                };
        foreach (var recordKind in securityRecords)
        {
            SecRecord query = new SecRecord(recordKind);
            SecKeyChain.Remove(query);
        }

回答by Muhammad Nayab

@amro's answertranslated to Swift 4.0:

@amro 的答案已转换为 Swift 4.0:

if UserDefaults.standard.object(forKey: "FirstInstall") == nil {
    UserDefaults.standard.set(false, forKey: "FirstInstall")
    UserDefaults.standard.synchronize()
}

回答by stephen

Files will be deleted from your app's document directory when the user uninstalls the app. Knowing this, all you have to do is check whether a file exists as the first thing that happens in application:didFinishLaunchingWithOptions:. Afterwards, unconditionally create the file (even if it's just a dummy file).

当用户卸载应用程序时,文件将从应用程序的文档目录中删除。知道了这一点,您所要做的就是检查文件是否存在作为application:didFinishLaunchingWithOptions:. 之后,无条件地创建文件(即使它只是一个虚拟文件)。

If the file did not exist at time of check, you know this is the first run since the latest install. If you need to know later in the app, save the boolean result to your app delegate member.

如果检查时该文件不存在,则您知道这是自最新安装以来的第一次运行。如果您稍后需要在应用程序中知道,请将布尔结果保存到您的应用程序委托成员。

回答by Stavash

This seems to be the default behavior on iOS 10.3 based on behavior people have been witnessingin beta #2. Haven't found any official documentation about this yet so please comment if you have.

根据人们在 beta #2 中看到的行为,这似乎是 iOS 10.3 上的默认行为。还没有找到任何关于此的官方文档,所以如果你有,请发表评论。