2016-02-19 99 views
1

我想在應用內購買,以實現在Swift應用程序,耗材應用程序內購買的,與產品ID productID不符合協議SKPaymentTransactionObserver

研究

使用this answer,我學會了關於應用程序購買的基本模式,並試圖對其進行調整,就像這樣。我不需要使用NSUserDefaults方法讓應用程序記住是否進行了購買,因爲我正在使用不同的解決方案來處理它。

代碼

class ViewController: UICollectionViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { 
    viewDidLoad() { 
     //Unimportant stuff left out 
      product_id = "productID" 
      SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
     } 

     @IBAction func thisISTheButtonToTriggerTheInAppPurchases(sender: AnyObject) { 

      if (SKPaymentQueue.canMakePayments()) { 
      var productID:NSSet = NSSet(object: self.product_id!); 
      var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>); 
      productsRequest.delegate = self; 
      productsRequest.start(); 
      print("Fetching Products"); 
     }else{ 
      print("can't make purchases"); 
     } 

     } 
    func buyProduct(product: SKProduct){ 
     print("Sending the Payment Request to Apple"); 
     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) { 
       print(validProduct.localizedTitle) 
       print(validProduct.localizedDescription) 

       print(validProduct.price) 
       buyProduct(validProduct); 
      } else { 
       print(validProduct.productIdentifier) 
      } 
     } else { 
      print("nothing") 
     } 
    } 


    func request(request: SKRequest!, didFailWithError error: NSError!) { 
     print("Error Fetching product information"); 
    } 

    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) { 
     print("Received Payment Transaction Response from Apple"); 

     for transaction:AnyObject in transactions { 
      if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ 


     switch trans.transactionState { 
      case .Purchased: 
       print("Product Purchased"); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       break; 
      case .Failed: 
       print("Purchased Failed"); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       break; 



      case .Restored: 
       print("Already Purchased"); 
       SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 


      default: 
       break; 
      } 
     } 
    } 

} 
} 

錯誤

類型 '的ViewController' 不符合協議 'SKPaymentTransactionObserver'

我調查此錯誤位,和來源像this建議su錯誤是由於沒有必需的功能。

According to ApplepaymentQueue:updatedTransactions:是唯一需要的,但我似乎正在實施。我的一個可能的理論是,我沒有正確實施 - 如果屬實,我該如何解決?

預期行爲

該應用程序呈現,並且如果給定的良好的用戶輸入,與該ID productID執行IAP。

感謝您的幫助,並讓我知道您是否需要更多信息!

回答

6

這很簡單。該錯誤是關於SKPaymentTransactionObserver。它唯一的required method是:

func paymentQueue(queue: SKPaymentQueue, 
    updatedTransactions transactions: [SKPaymentTransaction]) 

的簽名方法不匹配:

func paymentQueue(queue: SKPaymentQueue!, 
    updatedTransactions transactions: [AnyObject]!) 

...所以它是不一樣的方法,所以你不符合。