iosswiftin-app-purchaseitunes-storeauto-renewable

Restore InApp purchase is always failed with error in swift, iOS


I am trying to buy some Auto-Renewable Subscription using sandbox test user with real device and everything works fine and at that time I can see this SKPaymentTransactionObserver method also work successfully :

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
            }
        }
    }
}

I also added other two optional SKPaymentTransactionObserver methods two use them when user going to restore purchases, here are those methods :

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)
}

here is my restore purchase invoking method :

func restorePurchasedProducts(){
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().restoreCompletedTransactions()                
}

but when I am going to restore purchases it never called func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {} or func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {} but always print this error:

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}

is there any way to solve this error?


Solution

  • by creating new sandbox test user and using it I could be able to figure out.