在 Swift 中恢复购买
Posted
技术标签:
【中文标题】在 Swift 中恢复购买【英文标题】:Restore Purchase in Swift 【发布时间】:2015-04-06 14:18:58 【问题描述】:我正在尝试向我的应用程序添加一个恢复按钮,我有一个非消耗品 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 方法而没有,这是什么问题?
【问题讨论】:
【参考方案1】:您没有将按钮添加为 IBAction。
阅读本文...
http://rshankar.com/different-ways-to-connect-ibaction-to-uibutton/
【讨论】:
【参考方案2】:试试这个代码
import UIKit
import StoreKit
class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver
var list = [SKProduct]()
var p = SKProduct()
// 2
func buyProduct()
println("buy " + p.productIdentifier)
var pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
//3
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!)
println("product request")
var myProduct = response.products
for product in myProduct
println("product added")
println(product.productIdentifier)
println(product.localizedTitle)
println(product.localizedDescription)
println(product.price)
list.append(product as SKProduct)
outRemoveAds.enabled = true
outAddCoins.enabled = true
// 4
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!)
println("transactions restored")
var purchasedItemIDS = []
for transaction in queue.transactions
var t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID
case "seemu.iap.removeads":
println("remove ads")
removeAds()
case "seemu.iap.addcoins":
println("add coins to account")
addCoins()
default:
println("IAP not setup")
// 5
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)
println("add paymnet")
for transaction:AnyObject in transactions
var trans = transaction as SKPaymentTransaction
println(trans.error)
switch trans.transactionState
case .Purchased:
println("buy, ok unlock iap here")
println(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID
case "seemu.iap.removeads":
println("remove ads")
removeAds()
case "seemu.iap.addcoins":
println("add coins to account")
addCoins()
default:
println("IAP not setup")
queue.finishTransaction(trans)
break;
case .Failed:
println("buy error")
queue.finishTransaction(trans)
break;
default:
println("default")
break;
// 6
func finishTransaction(trans:SKPaymentTransaction)
println("finish trans")
//7
func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
println("remove trans");
【讨论】:
当用户想要恢复购买时会发生什么?以上是关于在 Swift 中恢复购买的主要内容,如果未能解决你的问题,请参考以下文章
检查用户是不是真的为应用内购买付费 [iOS, Swift]