iOS 中未购买的用户按下恢复购买按钮会发生什么?
What should happen when non-purchased user press the restore purchase button in iOS?
我的沙盒测试账号可以购买非消耗品并恢复。一切正常。但是,如果该帐户以前没有购买过该项目,则按恢复按钮不会执行任何操作。我在调试面板中什么也看不到。我期待 iOS 检测某个用户是否购买了该商品,如果没有,则显示一条消息,要求他们购买。它是那样工作还是当前的行为是完全可以接受的?
这是连接到主情节提要内按钮的恢复购买代码(Swift):
@IBAction func restoreButtonPressed(sender: UIButton) {
statusLabel.text = "Status: Restoring Purchase"
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
其他实现的方法包括:
正常购买有效
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {}
适用于正常恢复
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {}
以前从未见过来自此方法的消息
func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) {}
谢谢!
您可以检查队列中是否有任何返回的交易,如果没有则表示没有要恢复的购买:
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
if queue.transactions.count == 0 {
let alert = UIAlertView()
alert.title = "Oops"
alert.message = "There are no purchases to restore, please buy one"
alert.addButtonWithTitle("Buy")
alert.addButtonWithTitle("Cancel")
alert.show()
}
}
你可以试试这个:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing: print("Payment is being processed")
case .purchased: print("USER PURCHASED YOUR PRODUCT")
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
if transaction.original?.transactionIdentifier != nil { // this identifier uniquely identifies a completed tansaction.
// queue.restoreCompletedTransactions()
print("THE BUYER BOUGHT THIS PRODUCT BEFORE")
} else {
/*
display UIAlertController showing an error
there is no receipts, user never purchased this product.
*/
}
case .failed:
self.dismiss(animated: true, completion: nil)
SKPaymentQueue.default().finishTransaction(transaction)
case .deferred:
print("pending")
@unknown default:
break
}
}
}
我的沙盒测试账号可以购买非消耗品并恢复。一切正常。但是,如果该帐户以前没有购买过该项目,则按恢复按钮不会执行任何操作。我在调试面板中什么也看不到。我期待 iOS 检测某个用户是否购买了该商品,如果没有,则显示一条消息,要求他们购买。它是那样工作还是当前的行为是完全可以接受的?
这是连接到主情节提要内按钮的恢复购买代码(Swift):
@IBAction func restoreButtonPressed(sender: UIButton) {
statusLabel.text = "Status: Restoring Purchase"
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
其他实现的方法包括:
正常购买有效
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {}
适用于正常恢复
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {}
以前从未见过来自此方法的消息
func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) {}
谢谢!
您可以检查队列中是否有任何返回的交易,如果没有则表示没有要恢复的购买:
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
if queue.transactions.count == 0 {
let alert = UIAlertView()
alert.title = "Oops"
alert.message = "There are no purchases to restore, please buy one"
alert.addButtonWithTitle("Buy")
alert.addButtonWithTitle("Cancel")
alert.show()
}
}
你可以试试这个:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing: print("Payment is being processed")
case .purchased: print("USER PURCHASED YOUR PRODUCT")
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
if transaction.original?.transactionIdentifier != nil { // this identifier uniquely identifies a completed tansaction.
// queue.restoreCompletedTransactions()
print("THE BUYER BOUGHT THIS PRODUCT BEFORE")
} else {
/*
display UIAlertController showing an error
there is no receipts, user never purchased this product.
*/
}
case .failed:
self.dismiss(animated: true, completion: nil)
SKPaymentQueue.default().finishTransaction(transaction)
case .deferred:
print("pending")
@unknown default:
break
}
}
}