In-App Purchase Programming Guide----(七) ----Restoring Purchased Products

Restoring Purchased Products

Users restore transactions to maintain access to content they’ve already purchased. For example, when they upgrade to a new phone, they don’t lose all of the items they purchased on the old phone. Include some mechanism in your app to let the user restore their purchases, such as a Restore Purchases button. Restoring purchases prompts for the user’s App Store credentials, which interrupts the flow of your app: because of this, don’t automatically restore purchases, especially not every time your app is launched.html

用戶恢復交易來保持訪問他們已經購買過的內容。 好比,當他們換了一個新手機,他們不會丟失任何他們已經在舊手機上購買過的產品。包括一些應用中的機制讓用戶能夠恢復他們的購買記錄,好比一個恢復購買按鈕。恢復購買記錄是提示用戶的應用商店的憑據,從而中斷你的應用程序流程,所以不要自動恢復購買記錄,特別是在每次應用程序啓動時。ios

In most cases, all your app needs to do is refresh its receipt and deliver the products in its receipt. The refreshed receipt contains a record of the user’s purchases in this app, on this device or any other device. However, some apps need to take an alternate approach for one of the following reasons:app

在大多數狀況下,你的應用程序只須要刷新它的收據並在收據中傳遞產品。 刷新完的收據中包含了一個用戶在該應用中在該設備或其它設備中購買的記錄。然而,一些應用程序由於如下緣由須要採起另外一種方法:ide

  • If you use Apple-hosted content, restoring completed transactions gives your app the transaction objects it uses to download the content.post

    若是你使用蘋果託管內容,對於恢復已完成的交易,蘋果將給你的應用提供一些用來下載內容的交易對象。測試

  • If you need to support versions of iOS earlier than iOS 7, where the app receipt isn’t available, restore completed transactions instead.ui

    若是你須要支持iOS7之前的版本,須要存儲已完成的全部交易,由於之前的版本不支持應用收據功能。this

  • If your app uses non-renewing subscriptions, your app is responsible for the restoration process.url

    若是你的應用使用非更新訂閱,應用程序負責恢復過程。spa

Refreshing the receipt asks the App Store for the latest copy of the receipt. Refreshing a receipt does not create any new transactions. Although you should avoid refreshing multiple times in a row, this action would have same result as refreshing it just once.

刷新收據是請求應用商店中最近的收據副本。 刷新一個收據並不會建立任何新的交易。 儘管你應該避免在同一行刷新屢次,屢次刷新跟一次刷新是同樣的。

Restoring completed transactions creates a new transaction for every completed transaction the user made, essentially replaying history for your transaction queue observer. While transactions are being restored, your app maintains its own state to keep track of why it’s restoring completed transactions and how it needs to handle them. Restoring multiple times creates multiple restored transactions for each completed transaction.

恢復已完成的交易就是爲用戶作的每一個已完成的交易建立一個新的交易,本質上是重複你的交易隊列觀察者的歷史。 當交易被恢復時,應用程序維護本身的狀態來監控它爲何要恢復交易,以及它須要如何處理它們。屢次恢復將爲每一個已完成的交易建立多個恢復的交易。

Note: If the user attempts to purchase a product that’as already been purchased, rather than using your app’s restoration interface, the App Store creates a regular transaction instead of a restore transaction. The user isn’t charged again for the product. Treat these transactions the exact same way you treated the original transactions.

注意:若是用戶嘗試着從新購買已經購買過的產品,而不是使用應用程序中的恢復界面,應用商店就建立一個常規的交易而不是一個恢復交易。用戶不須要再次支付該產品。 對待這些交易跟原來的交易如出一轍。 

 

Give the user an appropriate level of control over what content is redownloaded. For example, don’t download three years worth of daily newpapers or hundreds of megabytes worth of game levels all at once.

適當控制用戶能夠從新下載的內容。 好比,不能一次下載三年量的日報,或者不能一次性下載10M大小的遊戲關卡。

Refreshing the App Receipt

1、刷新應用收據

Create a receipt refresh request, set a delegate, and start the request. The request supports optional properties for obtaining receipts in various states during testing such as expired receipts—for details, see the values for the initWithReceiptProperties: method of SKReceiptRefreshRequest.

建立一個收據刷新請求,設置一個委託,並開啓該請求。 請求在測試中支持不少可選特性來獲取各類狀態的收據,好比到期的收據---詳情請參見 SKReceiptRefreshRequest 類的initWithReceiptProperties: 方法的值:

request = [[SKReceiptRefreshRequest alloc] init];
request.delegate = self;
[request start];

After the receipt is refreshed, examine it and deliver any products that were added.

收據刷新了以後,檢查它並傳遞任何添加的產品。

Restoring Completed Transactions

2、恢復已完成的交易

Your app starts the process by calling the restoreCompletedTransactions method of SKPaymentQueue. This sends a request to the App Store to restore all of your app’s completed transactions. If your app sets a value for the applicationUsername property of its payment requests, as described in 「Detecting Irregular Activity,」 use the restoreCompletedTransactionsWithApplicationUsername: method to provide the same information when restoring transactions.

應用程序經過調用 SKPaymentQueue類的 restoreCompletedTransactions 方法來開始該過程。 這樣作就是給應用商店發送一個請求讓它來恢復或有應用程序中的已完成交易。 若是應用程序給它的支付請求(payment requests)的applicationUsername特性設置了一個值,正如In-App Purchase Programming Guide----(四) ----Requesting Payment 中的 「Detecting Irregular Activity,」 中所描述,在恢復交易時使用restoreCompletedTransactionsWithApplicationUsername:  方法來提供相同的信息。

The App Store generates a new transaction for each transaction that was previously completed. The restored transaction has a reference to the original transaction: instances of SKPaymentTransaction have a originalTransaction property, and the entries in the receipt have an Original Transaction Identifier field.

應用商店爲每一個已經購買完成的交易生產一個新的交易。 恢復的交易有一個指向最初交易的引用:SKPaymentTransaction 的實例有一個 originalTransaction  特性, 而且收據的項目(entries)中有一個初始交易識別碼字段。

Note: The date fields have slightly different meanings for restored purchases. For details, see the Purchase Date and Original Purchase Date fields in Receipt Validation Programming Guide.

注意:恢復的購買記錄中的日期字段跟初始交易記錄中的有稍微不一樣的意義。具體詳情,請看Receipt Validation Programming Guide 中的購買日期和初始購買日期字段。

 

Your transaction queue observer is called with a status of SKPaymentTransactionStateRestored for each restored transaction, as described in 「Waiting for the App Store to Process Transactions.」 The action you take at this point depends on the design of your app.

應用程序調用交易隊列觀察者,它帶有每一個恢復交易的SKPaymentTransactionStateRestored 狀態。詳情請看:「Waiting for the App Store to Process Transactions.」 這時你所須要作的操做取決於應用程序的設計。

  • If your app uses the app receipt and doesn’t have Apple-hosted content, this code isn’t needed because your app doesn’t restore completed transactions. Finish any restored transactions immediately.

     若是你的應用程序使用應用收據而且沒有蘋果託管內容,則不須要該代碼,由於你的應用程序沒有恢復已完成的交易。 當即結束任何的恢復交易。

  • If your app uses the app receipt and has Apple-hosted content, let the user select which products to restore before starting the restoration process. During restoration, re-download the user-selected content and finish any other transactions immediately.

     若是你的應用程序使用應用收據而且有蘋果託管內容,在開始恢復進程前讓用戶選擇恢復哪些產品。 在恢復過程當中,從新下載用戶選擇的內容並當即結束任何其它交易。

    NSMutableArray *productIDsToRestore = <# From the user #>;
    SKPaymentTransaction *transaction = <# Current transaction #>;
     
    if ([productIDsToRestore containsObject:transaction.transactionIdentifier]) {
        // Re-download the Apple-hosted content, then finish the transaction
        // and remove the product identifier from the array of product IDs.
    } else {
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
  • If your app doesn’t use the app receipt, it examines all completed transactions as they’re restored. It uses a similar code path to the original purchase logic to make the product available and then finishes the transaction.

     若是你的應用沒有使用應用收據,它將檢查全部的已完成交易並把它們做爲已恢復交易。 它使用跟初始購買邏輯類似的代碼路徑來激活產品,而後結束交易。

    Apps with more than a few products, especially products with associated content, let the user select which products to restore instead of restoring everything all at once. These apps keep track of which completed transactions need to be processed as they’re restored and which transactions can be ignored by finishing them immediately.

    帶有多個產品的應用程序,特別是跟內容相關的產品,讓用戶選擇恢復哪些產品,而不是一次性恢復全部交易。  這些應用保持監控哪些已完成交易須要被處理爲已經恢復,哪些交易能夠忽視並當即結束交易。 

相關文章
相關標籤/搜索