ios - 如何在钥匙串(keychain)中保存应用购买状态

标签 ios swift in-app-purchase keychain

在我的应用程序中,用户可以购买 non-consumable product .我现在的问题是我不太清楚如何正确存储信息,无论用户是否购买了产品。我读了User Defaults不是最安全的方法,所以我用 Keychain 进行了尝试.为此,我使用 this助手库。
现在这就是我尝试实现它的方式:

keychain["isPremium"] = "true"             // set the state
let token = try? keychain.get("isPremium") // get the state to check if it worked
print(token as Any) 
这是打印“真实”。但是,如果我注销用户并使用另一个帐户登录,我只调用它:
let token = try? keychain.get("isPremium") 
即使第二个用户没有购买该商品,它仍然会打印出“true”。那么如何在我的应用程序中存储 IAP 的状态呢?

最佳答案

这完全取决于您的应用程序逻辑,钥匙串(keychain)按预期工作:您将某些内容保存到其中,然后再次读出(未更改)。 Keychain 无法知道您的应用程序中发生了哪些其他类型的逻辑。
也就是说,您可以做一些简单的事情,例如根据某种用户标识符在钥匙串(keychain)中使用不同的 key 存储每个用户的解锁状态。

// holds the current logged in user ID
// this needs to be persisted across the user's session,
// so store this somewhere like UserDefaults or the Keychain 
// (depending on your app's security model)
//
// I just use a simple variable for this example
var currentUserID = ""

// user 1 logs in, set:
currentUserID = "user1_"

keychain[currentUserID + "isPremium"] = "true"   
let token = try? keychain.get(currentUserID + "isPremium")
print(token as Any) 

// user 2 logs in, set:
currentUserID = "user2_"

keychain[currentUserID + "isPremium"] = "true"
let token = try? keychain.get(currentUserID + "isPremium")
print(token as Any) 

关于ios - 如何在钥匙串(keychain)中保存应用购买状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63885240/

相关文章:

ios - 是否可以发出不打开应用程序的推送通知

ios - 将两个核心数据结果加入 1 个 NSDictionary

ios - 异步获取完成 : Is cell still being displayed?

ios - 我仍然可以恢复已从 iTunes Connect 禁用的应用内购买吗?

ios - 如何为自动续订订阅 (iOS) 授予免费月份?

android - 如何从一个 channel 中获得所有YouTube视频?

ios - 未显示iOS自动续订订阅免费试用版

ios - 如何在 Swift 中从文件扩展名中拆分文件名?

swift - 通过label查找UITableViewCell的indexPath

iOS IAP 恢复不起作用