我正在尝试使用Swift在iOS中实现应用内购买,但是Restore-Purchase功能带来了问题。每当我单击restore purchase按钮时,它运行良好,但它会一直将其添加到队列中。请帮忙。
下面是我的代码和输出。
在输出“IAP not setup method3”中,每当我单击Restore Purchase按钮时,“IAP not setup method3”就会持续增加一个。
import UIKit
import StoreKit
class PremiumUpgradeVC: UIViewController , SKProductsRequestDelegate, SKPaymentTransactionObserver{
@IBOutlet weak var uiScrollVw: UIScrollView!
@IBOutlet weak var btnPremiumPurchase: UIButton!
@IBOutlet weak var btnPrice: UIButton!
@IBOutlet weak var btnRestorePur: UIButton!
var removeAds : Bool = false
var enableCompilations : Bool = false
var premiumPurchasedKey : String = "isPremiumPurchased"
var list = [SKProduct]()
var p = SKProduct()
var IAPId = "com.creativecub.testme"
override func viewDidLoad() {
super.viewDidLoad()
if(SKPaymentQueue.canMakePayments()){
print("IAP is enabled, loading")
var productID:NSSet = NSSet(objects: IAPId)
var request :SKProductsRequest = SKProductsRequest(productIdentifiers : productID as! Set<String>)
request.delegate = self
request.start()
//loadingAlert.
}else{
print("please enable IAP")
}
}
@IBAction func actionGoPremium(sender: UIButton) {
print("on action button")
for product in list{
var prodID = product.productIdentifier
if(prodID == IAPId){
p = product
buyProduct()
break;
}
}
}
func goPremi(){
print("in method : gopremium")
// removeAds = true
// enableCompilations = true
HelperClass.setValSharedPerf(premiumPurchasedKey, value: "true")
print("ads removed and compilations enables .. Enjoy")
}
@IBAction func actionRestorePurchase(sender: UIButton) {
print("in method : 1")
SKPaymentQueue.defaultQueue().addTransactionObserver(self) SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func buyProduct() {
print("buy product -" + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
print("in method : 2")
}
//restored transaction function
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("in method : 3")
//print("transaction retsored")
var purchasedItemIDs = []
for transaction in queue.transactions{
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case IAPId:
print("premium Upgrade method 3")
goPremi()
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
default:
print("IAP not setup method3") // this keeps increasing
}
}
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("in method : 4")
print("product request")
var myProducts = response.products
for product in myProducts {
print("product added - details :" + product.productIdentifier + product.localizedTitle)
let price = priceStringForProduct(product)
btnPrice.setTitle("Buy for: "+price!, forState: UIControlState.Normal)
list.append(product)
}
}
//restore purchase
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
// print("added payment")
print("in method : 5")
for transaction:AnyObject in transactions{
let trans = transaction as! SKPaymentTransaction
//print(trans.error)
switch trans.transactionState {
case .Purchased: //switch1 case 1
print("buy ok, unlock IAP here")
let prodID = p.productIdentifier as String
switch prodID {
case IAPId:
print("premium Upgrade .. purchase restored")
goPremi()
queue.finishTransaction(trans)
default:
print("IAP not setup method5")
}
queue.finishTransaction(trans)
break;
case .Failed: //switch1 case 2
print("error in buy")
queue.finishTransaction(trans)
break;
default: //switch1 case 3
print("default case method 5")
break;
}
}
}
func finishTransaction(trans:SKPaymentTransaction){
print("finsh trans")
SKPaymentQueue.defaultQueue().finishTransaction(trans)
print("in method : 6")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("remove trnas")
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
print("in method : 7")
}
func priceStringForProduct(item: SKProduct) -> String? {
let numberFormatter = NSNumberFormatter()
let price = item.price
let locale = item.priceLocale
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.locale = locale
return numberFormatter.stringFromNumber(price)
}
func goBack()
{
self.dismissViewControllerAnimated(true, completion: nil);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(animated: Bool) {
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
print("disapeard")
}
}
这是输出:
in method : 1
in method : 5
default case method 5
default case method 5
in method : 3
IAP not setup method3 //this statement keep increasing on button click
IAP not setup method3
IAP not setup method3
IAP not setup method3
IAP not setup method3
premium Upgrade method 3
in method : gopremium
ads removed and compilations enables .. Enjoy
remove trnas
in method : 7
最佳答案
在paymentQueueRestoreCompletedTransactionsFinished中,除了在需要时更新用户界面外,您不应执行任何操作。调用此委托方法是为了指示还原过程已完成;仅此而已。
还原的事务将传递到更新的事务方法,状态为restored。你应该像处理购买状态一样处理它们
关于ios - 恢复购买工作,但不断循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39958491/