2015-08-28 158 views
0

我正在使用IAPHelper實現應用內購買,當按下購買按鈕後轉到其他視圖控制器時發生錯誤。例如,當我按購買按鈕,然後轉到另一個視圖控制器,完成購買工作後出現錯誤。應用內購買Swift

class selectQuestion_ViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource{ 

let helper = IAPHelper(productIdentifiers: NSSet(object: "sppid") as Set<NSObject>) 
func purchase(sender: AnyObject) 
    { 
     startLoading("Purchasing ..") 
     self.helper.requestProductsWithCompletionHandler({ (success, products) -> Void in 

      if success { 
       self.endLoading() 
       println("wohooooo") 
       var sdad = self.helper.productsDict["sppid"] 
       self.helper.buyProduct(sdad!) 

      } else { 
       self.endLoading() 
       let alert = UIAlertController(title: "Error", message: "Cannot retrieve products list right now.", preferredStyle: .Alert) 
       alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
       self.presentViewController(alert, animated: true, completion: nil) 
      } 


     }) 
    } 

回答

1

我建議你不要使用這樣的模塊。這將是你和你的單邊行動計劃的理解是,你檢查本教程好得多,實現代碼和描述的方法有:https://www.youtube.com/watch?v=h1gQklbrgjc

一旦實現代碼,如果事情不工作(你可能已經忘記了行到某處),回到SO,我們很樂意幫助你。作爲快速幫助,這裏有一個答案,可以幫助你解決大部分錯誤,一旦你用IAP助手實現你的IAP代碼:My IAP isn't working. Bugs at func Paymentqueue

事實上,你可以觀看視頻並複製粘貼我的代碼題。我的IAP代碼有效;)會爲您節省時間。但觀看視頻瞭解如何實施IAP!

我希望這個答案幫助你;)

以下是完整的IAP代碼:

import UIKit 
import StoreKit 


class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{ 


    @IBOutlet var outRemoveAds: UIButton! 
    @IBOutlet var outRestorePurchases: UIButton! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 

     if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil { 
      print("Has a value.") 
      banner.removeFromSuperview() 
      bannerGoogle.removeFromSuperview() 
      outRemoveAds.removeFromSuperview() 
      outRestorePurchases.removeFromSuperview() 
      removeInterFrom = 1 
     } 
     else { 
      print("No Value.") 
     } 

     if(SKPaymentQueue.canMakePayments()){ 
      print("IAP is enabled, loading...") 
      let productID:NSSet = NSSet(objects:"IAP id") 
      let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>) 
      request.delegate = self 
      request.start() 
     } 
     else{ 
      print("Please enable IAPS") 

     } 

    } 


    //IAP Ads 

    @IBAction func removeAds(sender: UIButton) { 
     for product in list{ 
      let prodID = product.productIdentifier 
      if (prodID == "IAP id"){ 
       p = product 
       buyProduct() 
       break 
      } 
     } 
    } 
    @IBAction func restorePurchases(sender: UIButton) { 
     SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 
    } 


    //IAP Functions 

    var list = [SKProduct]() 
    var p = SKProduct() 

    func removeAds(){ 
     banner.removeFromSuperview() 
     bannerGoogle.removeFromSuperview() 
     outRemoveAds.removeFromSuperview() 
     outRestorePurchases.removeFromSuperview() 
     let theValue = 10 
     NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val") 
     NSUserDefaults.standardUserDefaults().synchronize() 
    } 

    func buyProduct(){ 
     print("Buy: "+p.productIdentifier) 
     let pay = SKPayment (product: p) 
     SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment) 
    } 

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) { 
     print("Product Request") 
     let myProduct = response.products 

     for product in myProduct{ 
      print("Product Added") 
      print(product.productIdentifier) 
      print(product.localizedTitle) 
      print(product.localizedDescription) 
      print(product.price) 

      list.append(product as SKProduct) 
     } 
    } 

    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
     print("Add Payment") 

     for transaction:AnyObject in transactions{ 
      let trans = transaction as! SKPaymentTransaction 
      print(trans.error) 
      switch trans.transactionState{ 
      case .Purchased: 
       print("IAP unlocked") 
       print(p.productIdentifier) 

       let prodID = p.productIdentifier as String 
       switch prodID{ 
       case "IAP id": 
        print("Remove Ads") 
        removeAds() 
       default: 
        print("IAP not setup") 
       } 
       queue.finishTransaction(trans) 
       break 
      case .Failed: 
       print ("Buy error") 
       queue.finishTransaction(trans) 
       break 
      default: 
       print("default: Error") 
       break 
      } 
     } 
    } 

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) { 
     print("Purchases Restored") 

     _ = [] 
     for transaction in queue.transactions { 
      let t: SKPaymentTransaction = transaction as SKPaymentTransaction 

      let prodID = t.payment.productIdentifier as String 
      switch prodID{ 
      case "IAP id": 
       print("Remove Ads") 
       removeAds() 
      default: 
       print("IAP not setup") 
      } 


     } 
    } 
    func finishTransaction(trans:SKPaymentTransaction){ 
     print("Finshed Transaction") 
    } 

    func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) { 
     print("Remove Transaction") 
    } 
} 

地說:

SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
在viewDidLoad中

或viewDidAppear

+0

雖然此鏈接可能回答這個問題,最好在這裏包含答案的重要部分,並提供參考鏈接ENCE。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/10400514) – Calimero

+0

真的,讓我這樣做。 – 2015-12-01 13:22:01