将用户数据保存到内部存储 – Apple TV Swift

huangapple go评论72阅读模式
英文:

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")

huangapple
  • 本文由 发表于 2023年6月5日 23:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定