Ios 自动续费订阅的恢复购买问题
我随后为我的应用程序添加了自动续费订阅。我的应用已经在应用商店上发布,但我有一个非常奇怪的问题,即如果用户点击“恢复购买”按钮,它将在不购买的情况下解锁该功能!。我与IAPs合作过,以前从未发生过这种情况!这是我的代码,在Ios 自动续费订阅的恢复购买问题,ios,swift,storekit,Ios,Swift,Storekit,我随后为我的应用程序添加了自动续费订阅。我的应用已经在应用商店上发布,但我有一个非常奇怪的问题,即如果用户点击“恢复购买”按钮,它将在不购买的情况下解锁该功能!。我与IAPs合作过,以前从未发生过这种情况!这是我的代码,在IAPManager.swift中,我添加了两行代码,在购买/恢复完成时触发一个方法 private func productPurchaseCompleted(identifier: ProductID?) { guard let identifier = ide
IAPManager.swift
中,我添加了两行代码,在购买/恢复完成时触发一个方法
private func productPurchaseCompleted(identifier: ProductID?) {
guard let identifier = identifier else { return }
purchasedProductIDs.insert(identifier)
UserDefaults.standard.set(true, forKey: identifier)
productPurchaseCompletionHandler?(true, identifier)
clearHandler()
//Added by me
appDefaults.isDirectory(purchased: true)
NotificationCenter.default.post(name: NSNotification.Name("completePurchase"), object: nil)
}
在“我的购买”视图控制器中:
override func viewWillAppear(_ animated: Bool) {
//Register notifications
NotificationCenter.default.addObserver(self, selector: #selector(completePurchase), name: NSNotification.Name("completePurchase"), object: nil)
}
购买和恢复按钮:
//MARK: - Purchase IAP and its methods
@IBAction func purchaseDirectory(_ sender: Any) {
purchaseItemIndex(index: 0)
loading.alpha = 1
}
@IBAction func restorePurchase(_ sender: Any) {
IAPProducts.store.restorePurchases()
loading.alpha = 1
}
private func purchaseItemIndex(index: Int) {
IAPProducts.store.buyProduct(products[index]) { [weak self] success, productId in
guard let self = self else { return }
guard success else {
let alertController = UIAlertController(title: "Failed to purchase product",
message: "Check logs for details",
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
return
}
}
}
最后完成采购功能:
@objc func completePurchase() {
let alert = UIAlertController(title: "Thank You!", message: "You can now access to directory", preferredStyle: .alert)
let action0 = UIAlertAction(title: "OK", style: .cancel, handler: { (click) in
self.removeAllViewControllers()
self.presentViewControlleryStoryboard(id: "HOME")
})
alert.addAction(action0)
present(alert, animated: true, completion: nil)
loading.alpha = 0
}
有人知道为什么会这样吗?例如,非消耗品IAP和自动续费订阅之间是否有任何区别
我必须确认restore purchase在sandbox环境中按预期工作我不知道您的
IAPProducts
是什么,但既然您标记了storekit,您应该按照苹果的要求来实现它
苹果详细介绍了如何做到这一点:
具体来说,这就是你想要做的
/// Called when tapping the "Restore" button in the UI.
@IBAction func restore(_ sender: UIBarButtonItem) {
// Calls StoreObserver to restore all restorable purchases.
StoreObserver.shared.restore()
}
具体地说,您需要利用它来恢复自动续费订阅。然后,如果这是真的,您将希望将appDefaults.isDirectory(购买:true)
设置为真
最后,如果你想了解更多关于IAP(应用内购买)资源的信息,请访问苹果。我使用了SwiftyStoreKit并解决了我的问题。