英文:
Saving user data to internal memory - Apple TV Swift
问题
我试图将访问令牌保存在应用程序注册表中,并成功保存了,但在重新启动设备后,返回到设备注册表后会被清除,用户会被带回身份验证页面。
UserDefaults.standard.set(accessToken, forKey: "apiToken")
UserDefaults.standard.synchronize()
let apiToken = UserDefaults.standard.value(forKey: "apiToken")
英文:
I am trying to save access token with in the application registry and getting saved success fully, but after rebooting the device and coming back to the device registry is getting cleared and user is taken back to the authentication page.
UserDefaults.standard.set(accessToken, forKey: "apiToken")
UserDefaults.standard.synchronize()
let apiToken = UserDefaults.standard.value(forKey: "apiToken")
答案1
得分: 1
要将值保存到UserDefaults中,您需要创建一个值键。
除非您已经创建了某个扩展,该扩展向UserDefaults添加了一个额外的属性,例如"apiToken",并且您在这里没有分享,否则您需要像这样创建它:
UserDefaults.standard.set(accessToken, forKey: "apiToken")
然后,您可以从中检索它:
let apiToken = UserDefaults.standard.value(forKey: "apiToken")
英文:
To save a value to UserDefaults you need to create a value key
Unless you've already created some extension which is adding an additional property to UserDefaults.. "apiToken" that you've not shared here, you'll need to create it like this
UserDefaults.standard.set(accessToken, forKey: "apiToken")
You will then be able to retrieve this from
let apiToken = UserDefaults.standard.value(forKey: "apiToken")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论