在 Swift 中恢复购买
Posted
技术标签:
【中文标题】在 Swift 中恢复购买【英文标题】:Restore Purchase in Swift 【发布时间】:2015-04-05 21:06:57 【问题描述】:我正在尝试向我的应用程序添加一个恢复按钮,我有一个非消耗品 inApp 购买并且购买部分工作正常。我有一个调用这两种方法的按钮:
product_id = "RemoveAdsIAP";
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
这是购买代码:
func buyConsumable()
succssessIAP = false
if (SKPaymentQueue.canMakePayments())
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
else
displayErrorAlert()
func buyProduct(product: SKProduct)
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse)
var count : Int = response.products.count
if (count>0)
var validProducts = response.products
var validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id)
buyProduct(validProduct);
else
else
displayErrorAlert()
func request(request: SKRequest!, didFailWithError error: NSError!)
activityIndicatorShop.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
displayErrorAlert()
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)
succssessIAP = false
for transaction:AnyObject in transactions
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction
switch trans.transactionState
case .Purchased, .Restored:
if (product_id == "GetNextFactIAP" && succssessIAP == false)
startController.getData()
startController.hideContainerView()
succssessIAP = true
if (product_id == "RemoveAdsIAP" && succssessIAP == false)
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "viewAdsBool")
NSUserDefaults.standardUserDefaults().synchronize()
startController.removeOnce()
disableAds(true)
succssessIAP = true
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
case .Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
activityIndicatorShop.stopAnimating()
displayErrorAlert()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
break;
default:
break;
当我按下按钮时没有任何反应,我添加了一条日志语句来检查是否调用了 updatedTransactions 方法,但没有,有什么问题?
【问题讨论】:
您的问题需要更具体。哪个代码被调用,哪个不是?按钮代码? updatedTransactions 方法?等等。代表设置了吗?你实施失败的方法吗?那些叫?如果是这样,传递了什么错误? updatedTransactions 没有被调用,我会添加完整的代码,因为我不知道缺少什么。 【参考方案1】:您的 products_id 格式错误。 应该更像
product_id = "com.xxxxxxxxx.RemoveAdsIAP"
【讨论】:
以上是关于在 Swift 中恢复购买的主要内容,如果未能解决你的问题,请参考以下文章
检查用户是不是真的为应用内购买付费 [iOS, Swift]