gpt4 book ai didi

swift - 不符合协议(protocol) SKPaymentTransactionObserver

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:35 30 4
gpt4 key购买 nike

我正在尝试在 Swift 应用中实现应用内购买,这是一种消耗品应用内购买,产品 ID 为 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;
}
}
}

}
}

错误

Type 'ViewController' does not conform to protocol 'SKPaymentTransactionObserver'

我稍微研究了一下这个错误,来源如 this提示此类错误是由于没有所需的功能。

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

预期行为

应用会呈现,如果用户输入有利,则执行 ID 为 productID 的 IAP。

感谢您的帮助,如果您需要更多信息,请告诉我!

最佳答案

非常简单。该错误与 SKPaymentTransactionObserver 有关。它只有required method是:

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

您的方法的签名不匹配:

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

...所以这不是同一种方法,所以你不符合。

关于swift - 不符合协议(protocol) SKPaymentTransactionObserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515464/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com