我目前正在尝试对非消耗性应用内购买实施“恢复购买”按钮。但是,尽管“恢复购买”按钮可以正常工作(恢复用户过去的购买),但该应用程序还会通过“确认您的应用内购买”警报来提示用户。这种类型的警报不应在还原购买期间发生,而应仅在实际购买期间发生。“确认您的购买”警报截图
请注意,用户可以在“确认购买”警报时按“取消”,并且仍将进行恢复购买。
下面是我正在使用的代码:
class RemoveAdsViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate {
@IBOutlet weak var removeAdsButton: UIButton!
@IBOutlet weak var restorePurchaseButton: UIButton!
var product: SKProduct?
var productID = "<INSERT_PRODUCT_ID>"
@IBAction func removeAds(_ sender: Any) {
let paymentRequest = SKMutablePayment()
paymentRequest.productIdentifier = productID
SKPaymentQueue.default().add(paymentRequest)
}
@IBAction func restorePurchase(_ sender: Any) {
SKPaymentQueue.default().restoreCompletedTransactions()
}
override func viewDidLoad() {
super.viewDidLoad()
removeAdsButton.isEnabled = false
restorePurchaseButton.isHidden = true
if (!removedAds) {
removeAdsButton.setTitle("Connecting to App Store...", for: .normal)
removeAdsButton.titleLabel?.font = removeAdsButton.titleLabel?.font.withSize(20)
SKPaymentQueue.default().add(self)
getPuchaseInfo()
} else {
removeAdsButton.setTitle("Purchased", for: .normal)
}
}
func getPuchaseInfo() {
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers: NSSet(object: self.productID) as! Set<String>)
request.delegate = self
request.start()
} else {
removeAdsButton.setTitle("Purchase Disabled", for: .normal)
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let products = response.products
if (products.count == 0) {
DispatchQueue.main.async {
self.removeAdsButton.setTitle("Purchase Not Found", for: .normal)
}
} else {
product = products[0]
DispatchQueue.main.async {
self.removeAdsButton.isEnabled = true
self.restorePurchaseButton.isHidden = false
self.removeAdsButton.setTitle("Remove Ads (\(self.product!.localizedPrice))", for: .normal)
self.removeAdsButton.titleLabel?.font = self.removeAdsButton.titleLabel?.font.withSize(28)
}
}
let invalids = response.invalidProductIdentifiers
for product in invalids {
print("product not found: \(product)")
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.purchased:
//user purchased remove ads
SKPaymentQueue.default().finishTransaction(transaction)
removeAdsButton.setTitle("Purchased", for: .normal)
removeAdsButton.isEnabled = false
restorePurchaseButton.isHidden = true
removedAds = true
UserDefaults.standard.set(true, forKey: "removedAds")
case SKPaymentTransactionState.restored:
//user restored purchase
SKPaymentQueue.default().finishTransaction(transaction)
removeAdsButton.setTitle("Purchased", for: .normal)
removeAdsButton.isEnabled = false
restorePurchaseButton.isHidden = true
removedAds = true
UserDefaults.standard.set(true, forKey: "removedAds")
case SKPaymentTransactionState.failed:
SKPaymentQueue.default().finishTransaction(transaction)
default:
break
}
}
}
}
extension SKProduct {
var localizedPrice: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
return formatter.string(from: price)!
}
}
如何进行设置,以便在恢复购买期间不会出现“确认您的应用内购买”警报?
该提示会在您尝试访问收据文件时出现,并且它不存在。听起来您可能会遇到的问题是,直到购买后,沙盒收据才真正在设备上不可用。
在生产中,下载该应用程序后会为Apple ID生成收据,因此用户可以随时还原,并且不会看到该提示。在沙盒中,收据在您安装应用程序时不可用-购买后会被添加到应用程序文件中。要全面测试沙箱中的还原,您需要购买商品,让其到期,然后触发还原-如果您在收据被清除的任何时候从XCode卸载或重建,则需要进行购买。这只是Apple沙箱的乐趣之一:)