我正在尝试使用带有真实设备的沙盒测试用户购买一些自动续订订阅,并且一切正常,当时我可以看到这个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}
有什么方法可以解决此错误吗?
通过创建新的沙盒测试用户并使用它,我可以弄清楚。