Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

ios - Restore Purchase : Non-Consumable

I have completed a small app where I have a non-consumable purchase option. It is on the App Store.

The purchase of the product runs OK. It's my Restore Purchase function that seems to do nothing.

I have added this code for the Restore Purchase @IBAction:

@IBAction func restorePurchases(sender: AnyObject) { 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

But nothing happens when I hit the restore purchase button.

I think I have to add a function that checks if the restore was successful or not. Am planning to amend code to the following:

@IBAction func restorePurchases(sender: AnyObject) { 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {

for transaction:AnyObject in transactions {
    if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
        switch trans.transactionState {
        case .Restored:
            SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
        var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
            break;

        case .Failed:
            SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
        var alert = UIAlertView(title: "Sorry", message: "Your purchase(s) could not be restored.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        break;

        default:
        break;
        }
    }
}    

Will this do the trick?

I have been through every thread in relation to effecting Restore Purchase transactions, and my research has led me to the above. So I don't think this is a duplicate of a question, but perhaps may clarify how to successfully restore purchases for others facing my similar situation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your codes looks pretty fine for the most part, although some parts seem to be from older tutorials . There is some changes you should make, one of them is that you need to call your unlockProduct function again.

This is the code I use (Swift 3).

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

    for transaction in transactions {
         switch transaction.transactionState {

        case .purchasing:
            // Transaction is being added to the server queue.

        case .purchased:
            // Transaction is in queue, user has been charged.  Client should complete the transaction.

            defer {
                queue.finishTransaction(transaction)
            }

            let productIdentifier = transaction.payment.productIdentifier

            unlockProduct(withIdentifier: productIdentifier)

        case .failed:
            // Transaction was cancelled or failed before being added to the server queue.

            defer {
                queue.finishTransaction(transaction)
            }

            let errorCode = (transaction.error as? SKError)?.code

            if errorCode == .paymentCancelled {
                print("Transaction failed - user cancelled payment")
            } else if errorCode == .paymentNotAllowed { // Will show alert automatically
               print("Transaction failed - payments are not allowed")
            } else {
                print("Transaction failed - other error")
                // Show alert with localised error description 
            }

        case .restored:
            // Transaction was restored from user's purchase history.  Client should complete the transaction.

            defer {
                queue.finishTransaction(transaction)
            }

            if let productIdentifier = transaction.original?.payment.productIdentifier {
                unlockProduct(withIdentifier: productIdentifier)
            }

        case .deferred:
            // The transaction is in the queue, but its final status is pending external action 
            // e.g family member approval (FamilySharing). 
            // DO NOT freeze up app. Treate as if transaction has not started yet.
        }
    }
}

Than use the delegate methods to show the restore alert

/// Restore finished
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
    guard queue.transactions.count != 0 else {
        // showAlert that nothing restored
        return
    }

    // show restore successful alert 
}

/// Restore failed
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError) {

     /// handle the restore error if you need to. 
}

Unlock product is just a method I am sure you already have too.

  func unlockProduct(withIdentifier productIdentifier: String) {
       switch productIdentifier {
         /// unlock product for correct ID
     }
  }

As a side note, you should move this line

 SKPaymentQueue.default().add(self)

out of your restore and buy function and put it in viewDidLoad.

Apple recommends you add the transaction observer as soon as your app launches and only remove it when your app is closed. A lot of tutorials unfortunately dont teach you this correctly. This way you unsure that any incomplete transactions e.g due to network error, will always resume correctly.

https://developer.apple.com/library/content/technotes/tn2387/_index.html

In my real projects my code for IAPs is in a Singleton class so I would actually using delegation to forward the unlockProduct method to my class that handles gameData. I can than also make sure the observer is added at app launch.

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...