我遵循了此tutorial,为我的应用添加了自动更新的订阅。我的应用程序已在应用程序商店中发布,但是我有一个非常奇怪的问题,即如果用户点击“恢复购买”按钮,它将解锁该功能而不进行购买!我曾与IAP合作过,从来没有发生过!这是我的代码,在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和自动续订之间有什么区别吗?
我必须确认还原购买可以在沙盒环境中按预期进行
答案 0 :(得分:0)
我不知道您的IAPProducts
是什么,但是由于您标记了storekit,因此应该按照Apple要求的方式实现它。
Here is a tutorial详细介绍了如何执行此操作:
具体地说,这就是您想要做的。
/// Called when tapping the "Restore" button in the UI.
@IBAction func restore(_ sender: UIBarButtonItem) {
// Calls StoreObserver to restore all restorable purchases.
StoreObserver.shared.restore()
}
具体来说,您将需要利用restoreCompletedTransactions()来恢复自动更新的订阅。然后,如果是这样,您将需要将appDefaults.isDirectory(purchased: true)
设置为true。
最后,如果您想了解更多信息,请向Apple this is the best guide发出有关IAP资源的信息(应用程序购买)