swift - 如何从应用程序中删除用户默认值?
问题描述
我已经创建了登录页面。当我输入电子邮件和密码时,从 Json 返回用户 ID,然后,我将 ID 保存在用户默认值中,以便能够在我的应用程序的不同页面中使用它,但是当我在另一个页面中添加注销按钮时。注销按钮的功能是当我按下它时从用户默认值中删除 id 然后将我带到 root uiviewcontroller 但是当我注销并使用另一个帐户登录时窃取保存旧 ID。
//save id to the user default by using login button
let id = UserDefaults.standard
id.set(data.id , forKey : "ID")
id.synchronize
//using user id with this code in diferent pages
let user_id = UserDefaults.standard.integer(forKey : "ID")
//delete id from user default by using logout button
UserDefaults.standard.removeObject(forKey : "ID")
userDefaults.synchronize
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainScreen = UIStoryboard.init(name : "Main" , bundle : nil).instantiateViewController(withIdentifier :"BG")
appDelegate.window?.rootViewController = mainScreen
解决方案
我们有两种选择,一种是设置为空或任何其他值对应的键,否则您需要删除所有用户默认值意味着使用“removePersistentDomain”键。示例
让 appDomain = Bundle.main.bundleIdentifier!UserDefaults.standard.removePersistentDomain(forName: appDomain)
推荐阅读
- c++ - 在类中指定但未在 C++ 中实现的私有默认构造函数
- canvas - 放大背景和放大 Fabric JS 对象
- php - 创建一个对象数组,然后在不丢失类的情况下获取对象
- c# - 通过 MONO 从流中获取字节数组 linux 上的高 CPU 使用率
- javascript - 拆分 textarea 并存储在数组中,然后将每个元素发布到 firestore
- c++ - cmake中带有target_link_libraries的自定义目标
- python - Pygame - 如何在表面中找到某种颜色
- reactjs - 使用vscode格式化jsx代码导致代码混乱
- javascript - 如何在输入点击事件时停止 onClick 调用
- powershell - 带有-ArgumentList 类型命令的`Start-Process PowerShell` 不会调用命令