不符合协议SKPaymentTransactionObserver

时间:2016-02-19 21:34:08

标签: swift in-app-purchase storekit

我正在尝试使用产品ID Swift

在应用内购买的productID应用中实施应用内购买

研究

使用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;
            }
        }
    }

}
}

错误

  

键入&#39; ViewController&#39;不符合协议&#39; SKPaymentTransactionObserver&#39;

我稍微研究了这个错误,像this这样的来源表明这样的错误是由于没有必要的功能。

According to ApplepaymentQueue:updatedTransactions:是唯一需要的,但我似乎正在实施。我的一个可能的理论是我没有正确实现它 - 如果是真的,我该如何解决?

预期行为

应用程序显示,如果有良好的用户输入,则执行ID为productID的IAP。

感谢您的帮助,如果您需要更多信息,请与我们联系!

1 个答案:

答案 0 :(得分:6)

这非常简单。错误是关于SKPaymentTransactionObserver。它唯一的required method是:

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

您的方法的签名与以下内容不匹配:

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

...所以它不是同一种方法,所以你不能遵守。