2015-08-31 58 views
1

我在Swift中創建了一個SpriteKit遊戲,我試圖在應用程序購買中實現。在SKScene的應用程序購買不工作?

我跟着這個問題在這裏:in app purchase in SKScene

這裏是我的代碼:

在didMoveToView:didMoveToView的

// Set IAPS 
     if(SKPaymentQueue.canMakePayments()) { 
      println("IAP is enabled, loading") 
      var productID:NSSet = NSSet(objects: "GameOverSaveSavior") 
      var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>) 
      request.delegate = self 
      request.start() 
     } else { 
      println("please enable IAPS") 
     } 

之外,但在GameScene

//IN APP PURCHASE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

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

    func purchaseMade() { 
     println("they bought it!") 
    } 

    func buyProduct() { 
     println("buy" + p.productIdentifier) 

     var pay = SKPayment(product: p) 
     SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
     SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment) 
    } 
    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) 

      productList.append(product as! SKProduct) 
     } 
    } 

    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 "GameOverSaveSavior": 

       purchaseMade() 

       //Right here is where you should put the function that you want to execute when your in app purchase is complete 
      default: 
       println("IAP not setup") 
      } 

     } 

     var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored. You may have to restart the app before banner ads are removed.", delegate: nil, cancelButtonTitle: "OK") 
     alert.show() 
    } 


    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, .Restored: 
       println("buy, ok unlock iap here") 
       println(p.productIdentifier) 

       let prodID = p.productIdentifier as String 
       switch prodID { 
       case "GameOverSaveSavior": 

        //Here you should put the function you want to execute when the purchase is complete 
        var alert = UIAlertView(title: "Thank You", message: "You may have to restart the app before the banner ads are removed.", delegate: nil, cancelButtonTitle: "OK") 
        alert.show() 
       default: 
        println("IAP not setup") 
       } 

       queue.finishTransaction(trans) 
       break; 
      case .Failed: 
       println("buy error") 
       queue.finishTransaction(trans) 
       break; 
      default: 
       println("default") 
       break; 

      } 
     } 
    } 

    func finishTransaction(trans:SKPaymentTransaction) 
    { 
     println("finish trans") 
    } 
    func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!) 
    { 
     println("remove trans"); 
    } 

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

在touchesBegan當在應用程序購買SE節點感動:

//In app purchase 
      if touchedNode == saveMeBtn { 

       println("button touched!") 
       for product in productList { 
        var prodID = product.productIdentifier 
        if(prodID == "GameOverSaveSavior") { 
         p = product 
         buyProduct() //This is one of the functions we added earlier 
         break; 
        } 
       } 

我所有的代碼是直接從上面的問題,並且代碼編譯沒有任何錯誤和

IAP啓動,加載

產品請求

在遊戲開始時被打印到控制檯。

當在應用程序內購買按鈕按下時,

按鈕感動!

被打印到控制檯,但沒有其他事情發生。它不會要求用戶購買任何東西。

我確保Xcode中的包ID與iTunes Connect中的包ID相同,並且應用內購買ID相同。我在這裏做錯了什麼?

回答

1

經過好幾個星期的工作,我發現你必須讓你的付費應用程序協議填寫完整並被接受,以便讓你的代碼和產品工作。審批可能需要幾天的時間,那麼你就清楚了。