我正在尝试使用带有真实设备的沙盒测试用户购买一些自动更新订阅,并且一切正常,当时我可以看到这个 SKPaymentTransactionObserver 方法也可以成功运行:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction:AnyObject in transactions {
if let trans = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
print("Success")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break
case .failed:
print("Fail")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break
case .restored:
print("restored")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break
default:
print("Called .purchasing .deferred state")
break
}
}
}
}
我还添加了另外两个可选的 SKPaymentTransactionObserver 方法,当用户要恢复购买时,两个使用它们,这里是这些方法:
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("Restore completed")
let transactionCount = queue.transactions.count
if transactionCount == 0{
print("No previous transactions found")
}
}
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
print(error)
}
这是我的恢复购买调用方法:
func restorePurchasedProducts(){
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
但是当我要恢复购买时,它从未调用过func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {}
或func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {}
但总是打印此错误:
Error Domain=NSURLErrorDomain Code=-1001 "Cannot connect to iTunes Store" UserInfo={NSErrorFailingURLStringKey=https://sandbox.itunes.apple.com/WebObjects/MZFinance.woa/wa/inAppRegrantPurchaseHistory, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <D0FBF51A-C6B2-4CBF-99F1-110EBA5E9589>.<31>, NSErrorFailingURLKey=https://sandbox.itunes.apple.com/WebObjects/MZFinance.woa/wa/inAppRegrantPurchaseHistory, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <D0FBF51A-C6B2-4CBF-99F1-110EBA5E9589>.<31>"
), NSLocalizedDescription=Cannot connect to iTunes Store, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x2835f73f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _kCFStreamErrorCodeKey=-2102}
有没有办法解决这个错误?