0

我正在测试我的应用程序自动续订订阅,但SKPaymentTransaction.transactionState从未更改为.purchased并且始终为.restored. 一切正常,但.restored自从我第一次打电话以来,我什么也得不到SKPaymentQueue.default().restoreCompletedTransactions()。我尝试从我的 iPhone 中删除该应用程序,重新登录到我的沙盒帐户,创建一个新的沙盒帐户,重新启动 Xcode,清理构建文件夹,......我做错了什么?请在下面找到我的代码:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        switch transaction.transactionState {
        case .purchased:
            SKPaymentQueue.default().finishTransaction(transaction)
            print(".purchased")
        case .failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            print(".failed")
        case .restored:
            SKPaymentQueue.default().finishTransaction(transaction)
            print(".restored")
        default:
            break
        }
    }
}

@IBAction func restore(_ sender: UIBarButtonItem) {
    SKPaymentQueue.default().restoreCompletedTransactions()
}

func subscribe() {
    if SKPaymentQueue.canMakePayments() {
        let payment = SKPayment(product: availableProducts[indexPath.row])
        SKPaymentQueue.default().add(payment)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    SKPaymentQueue.default().add(self)
}
4

3 回答 3

0

可能,它总是.restored,因为它还transaction没有完成。


通常我们SKPaymentQueue.default().add(observer)放入AppDelegate,

这样交易将始终得到处理。

 class AppDelegate{

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            SKPaymentQueue.default().add(self)
      }

}

extension AppDelegate: SKPaymentTransactionObserver {

      func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
            // ...
      }
}

当你放入SKPaymentQueue.default().add(observer)ViewController,因为ViewController可能会被推和弹出。

也就是说,ViewController实例被分配和释放。

由于事务处理是异步的,ViewController其事务对应的实例可能不存在,或者不是同一个。

所以它的行为很灵活。

于 2020-05-12T06:37:22.913 回答
0

You should check result of queue

let transactions = queue.transactions
    if transactions.count > 0 {
        for transaction in queue.transactions {
            if (transaction.transactionState == SKPaymentTransactionState.failed) {
                //possibly handle the error
                purchaseStatusBlock?(.failed)
            } else if (transaction.transactionState == SKPaymentTransactionState.purchased) {
                //deliver the content to the user
                purchaseStatusBlock?(.purchased)
            } else if (transaction.transactionState == SKPaymentTransactionState.restored) {
                //deliver the content to the user
                purchaseStatusBlock?(.restored)
            } else {
                //handle other transaction states
            }
        }
    } else {
        purchaseStatusBlock?(.failed)
    }
于 2021-05-08T09:06:43.000 回答
0

我在这里发现:Clearing SKPAymentsQueue : Forcing Unfinished Transactions To Finish我的问题是我有一些未完成的待处理事务,所以我将其添加到我viewDidLoad的“强制”完成所有事务中。我应该永久包含它以避免它再次发生吗?

let currentQueue: SKPaymentQueue = SKPaymentQueue.default();
for transaction in currentQueue.transactions {
    currentQueue.finishTransaction(transaction);
}
于 2020-05-12T12:26:55.967 回答