iOS 15介绍了StoreKit 2,我正在查看它是否能在我现有的应用程序中采用,而我不知道如何使用它。特别是,我不知道如何实现所需的恢复功能(例如,如果用户删除了我的应用程序)。
我想我们应该使用Transaction.latest(for:)
?是那么回事吗?
但是在我的测试中,如果用户已经使用StoreKit 1完成了购买,该调用将返回nil
。这是真的吗?或者如果我做错了,从StoreKit 1迁移到StoreKit 2的正确方法是什么,以及如何处理恢复?
发布于 2021-10-27 03:52:38
您可以在Transaction.currentEntitlements
上进行迭代,以获得所有活动订阅和以前购买的非消耗性产品。
如果您在启动时检查此集合,您可以在没有用户交互的情况下静默地恢复以前的任何购买。
发布于 2021-10-27 14:28:05
为了扩展Paulw11 11的正确答案,我将演示我最终得到的实际代码。
StoreKit 2真的很简单。与商店或商店信息交互主要是打开连续信封的问题。我只有一个应用程序内购买,而且它是不可消耗的,所以当用户要求购买它时,我就是这样做的:
if let purchase = try? await product.purchase() {
if case let .success(result) = purchase {
if case let .verified(trans) = result {
if trans.productID == IAPUtilities.productid {
IAPUtilities.signalPurchaseSuccess()
await trans.finish()
return
}
}
}
}
// but if we get here, we must have failed
IAPUtilities.signalPurchaseFailure()
同样,当用户请求恢复购买时,我就是这样做的:
for await result in Transaction.currentEntitlements {
if case let .verified(trans) = result {
if trans.productID == IAPUtilities.productid {
IAPUtilities.signalPurchaseSuccess()
withUnsafeCurrentTask { task in
task?.cancel()
}
await trans.finish()
return
}
}
}
// but if we get here, we must have failed
IAPUtilities.signalRestorationFailure()
从我的测试来看,权利信息似乎以某种方式存储在磁盘上。因此,如果这不是一个新安装的应用程序,恢复是能够确认购买,而不做任何联网。但是,如果是新安装的应用程序,则应享权利信息是通过网络与商店对话获得的。您似乎不需要在这类事务上调用finish
,但无论如何,我都在这样做,因为它似乎没有坏处。
发布于 2022-03-24 21:11:54
在苹果的SKDemo中,他们做这
Button("Restore Purchases", action: {
async {
//This call displays a system prompt that asks users to authenticate with their App Store credentials.
//Call this function only in response to an explicit user action, such as tapping a button.
try? await AppStore.sync()
}
})
我个人想知道它是否成功,所以我用一种方法来包装它。
func restore() async -> Bool {
return ((try? await AppStore.sync()) != nil)
}
并相应地发出警报。
https://stackoverflow.com/questions/69728711
复制相似问题