检测到即将删除的 iOS 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6911361/
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
Detect iOS application about to delete?
提问by Water7
For my iOS
application, what event will be triggered when user a is about to delete the application?
对于我的iOS
应用程序,当用户a即将删除应用程序时会触发什么事件?
采纳答案by Remy Vanherweghem
No such thing, sorry.
没有这种事,抱歉。
The best you can do is do is check for the UIApplicationWillTerminateNotification
notification but more importantly save the state of your app (on a server for example) when it's transitioning to the background and cross your fingers your user will not delete your app when it's not running. Because once your app closed, you don't have any control anymore.
您能做的最好的事情是检查UIApplicationWillTerminateNotification
通知,但更重要的是在您的应用程序转换到后台时保存您的应用程序的状态(例如在服务器上),并交叉手指您的用户不会在它未运行时删除您的应用程序。因为一旦您的应用程序关闭,您就无法再进行任何控制。
EDIT: Since you want to clear the keychain's content when the app is deleted, I suggest you take a look at thisother question. Basically, what is suggested by some answers there is not to remove the content of the keychain at delete time, but instead when the user first launches the app using NSUserDefaults.
编辑:既然你想,当应用程序被删除,清除钥匙串的内容,我建议你先看看这个其他问题。基本上,一些答案所建议的不是在删除时删除钥匙串的内容,而是在用户首次使用 NSUserDefaults 启动应用程序时。
EDIT: Luis Ascorbe commented with an idea: using Push Notification's feedback service ( https://stackoverflow.com/a/7912045/157401) Of course, that's far from perfect (not all users subscribe to the notifications, notification tokens might be invalidated for other reasons, etc.) but that's still something to consider.
编辑:Luis Ascorbe 评论了一个想法:使用推送通知的反馈服务(https://stackoverflow.com/a/7912045/157401)当然,这远非完美(并非所有用户都订阅了通知,通知令牌可能会失效)出于其他原因等),但这仍然需要考虑。
EDIT: Starting with iOS 10.3 Beta 2, keychain data appears to no longer be persisted when an app is deleted.
编辑:从 iOS 10.3 Beta 2 开始,删除应用程序后,钥匙串数据似乎不再保留。
回答by Anusha
We cannot exactly know when the user has deleted the application. However, I came across a situation today to detect uninstallation of application which is both device and user specific (only in specific case it will be known).
我们无法确切知道用户何时删除了应用程序。但是,我今天遇到了一种情况,即检测应用程序的卸载,该应用程序是特定于设备和用户的(仅在特定情况下才会知道)。
The following scenario may help you where you need to delete the data based on user and device: If you are using rest API services and authentication for your App, make sure you do this to track it.
以下场景可以帮助您根据用户和设备删除数据:如果您正在为您的应用程序使用 rest API 服务和身份验证,请确保您执行此操作以对其进行跟踪。
- Make sure you store all your user Data by using combination of user id and device identifier as primary key.
- Consider a bool value for each device identifier for each user.
- When user logins to the app, make a service call and set bool to true for that device identifier and user id on server.
- When user logouts of the app, make a service call and set bool to false for that device identifier and user id. Delete all the user specific data( from device and backend) while logging out(Depends on your business logic).
- Now, if the user logins again and uninstalls the app without logging out, the bool will be left true and all the corresponding user and device specific data will not be deleted.
- When user logins on a device, check for that bool value before updating it to true. If it is already true, it means that the same user has uninstalled this app on that particular device and installed it again on the same device.
- 确保使用用户 ID 和设备标识符的组合作为主键来存储所有用户数据。
- 考虑每个用户的每个设备标识符的 bool 值。
- 当用户登录到应用程序时,进行服务调用并将服务器上的该设备标识符和用户 ID 的 bool 设置为 true。
- 当用户注销应用程序时,进行服务调用并将该设备标识符和用户 ID 的 bool 设置为 false。注销时删除所有用户特定数据(来自设备和后端)(取决于您的业务逻辑)。
- 现在,如果用户再次登录并在不退出的情况下卸载应用程序,则布尔值将保留为真,并且不会删除所有相应的用户和设备特定数据。
- 当用户登录设备时,在将其更新为 true 之前检查该 bool 值。如果它已经为真,则表示同一用户已在该特定设备上卸载了此应用程序,并在同一设备上再次安装了该应用程序。
Please note that this logic works only if there are service calls in your app and there is some authentication initially. Also, we can know this only if same user tries to login into same device. Uninstallation of application in other use cases can't be known with this logic.
请注意,此逻辑仅在您的应用程序中有服务调用并且最初有一些身份验证时才有效。此外,只有当同一用户尝试登录同一设备时,我们才能知道这一点。使用此逻辑无法知道其他用例中应用程序的卸载。
Hoping that this kind of logic may help someone as we are using this logic now. I am a newbie..please guide if I am wrong.
希望这种逻辑可以帮助某人,因为我们现在正在使用这种逻辑。我是新手..如果我错了,请指导。
回答by Stephen Darlington
I'm afraid that there is no such notification. When your apps isn't running there's no way it canbe notified of changes!
恐怕没有这样的通知。当您的应用程序未运行时,无法通知更改!
Instead you need to save any state when your user presses the home button, i.e., when it "resigns active." (There's a callback in the UIApplicationDelegate
and you can also listen for notifications.) In general I would not recommend listening for UIApplicationWillTerminateNotification
since it is rarely called on iOS4 where multi-tasking is supported.
相反,当您的用户按下主页按钮时,您需要保存任何状态,即,当它“退出活动”时。(在 中有一个回调,UIApplicationDelegate
你也可以监听通知。)总的来说,我不建议监听,UIApplicationWillTerminateNotification
因为它在支持多任务的 iOS4 上很少被调用。