在我的应用程序中,用户可以购买 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/