IAP最佳實踐方法
該文檔是蘋果8月5號發布的新Technical Note--In-App Purchase Best Practices,最要描述了iOS 和 OS X 應用程序中的IAP的最佳實踐。
以下是推薦給開發者的IAP最佳實踐列表。
在應用啟動時添加一個交易隊列觀察者
應用程序調用StoreKit把觀察者鏈接到payment queue。
- [SKPaymentQueue defaultQueue] addTransactionObserver:your_observer];
在恢復或者運行你的app應用時,如果支付隊列的內容發生了變化,StoreKit則會自動通知你(注冊的)觀察者 在應用啟動時添加觀察者確保它在所有app啟動時都會存在,這將允許你的應用能接收到所有的payment queue提醒。
考慮應用程序這樣一個情況,在向隊列(如表1)添加支付請求前,應用的 DetailViewController 類創建了一個觀察者。這個觀察者的存在時間和 DetailViewController 實例一樣長。如果出現中斷情況,比如網絡失敗,那么app將不能完成購買流程,而相關的交易仍在支付隊列中。當app正常恢復后,它將沒有觀察者存在,因 為在應用被發送至后臺時,上述觀察者就已經被解除了。因此,你的應用將不會收到隊列中的交易通知。
列表 1.不遵循實現交易觀察者最佳實踐:當用戶嘗試購買產品時,應用為 payment queue 添加觀察者:
- @implementation DetailViewController
- ....
- // Called when a customer attempts to purchase a product
- - (IBAction)purchase:(id)sender
- {
- // Register an observer to the payment queue
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- // Create a payment request
- SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:self.product];
- // Submit the payment request to the payment queue
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- }
- ....
- @end
列表 2.遵循注冊交易觀察者的最佳實踐
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Attach an observer to the payment queue
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- return YES;
- }
- // Called when the application is about to terminate
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- // Remove the observer
- [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
- }
- ....
- @end
StoreKit 在app調用時從payment queue移除觀察者:
同樣,如果沒有從 payment queue 移除觀察者,StoreKit 將會試圖通知上述觀察者,從而導致應用崩潰,好像觀察者已經不復存在了。
在展示應用內商店UI之前向App Store詢問產品信息
在決定在用戶界面中展示可購買商品之前,你的應用必須首先向App Store發送一個產品請求。發送產品請求可讓你確定產品是否可在App Store中出售,從而阻止展示不能購買的產品。可查看 Retrieving Product Information 學習如何創建一個產品請求。App Store使用 SKResponse 對象響應產品請求,使用其 products 屬性來更新你的UI,以確保你的用戶只能看到App Store中可供銷售的產品。
列表 3.不遵循IAP產品展示最佳實踐:在展示可銷售產品后, APP向App Store詢問相關產品信息。
- // App first displays a product for sale, then queries the App Store about it when a customer attempts to purchase it
- - (IBAction)purchase:(id)sender
- {
- // Create a set for your product identifier
- NSSet *productSet = [NSSet setWithObject:@"your_product_identifier"];
- // Create a product request object and initialize it with the above set
- SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:productSet];
- request.delegate = self;
- // Send the request to the App Store
- [request start];
- }
- // Get the App Store's response
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
- {
- // No purchase will take place if there are no products available for sale.
- // As a result, StoreKit won't prompt your customer to authenticate their purchase.
- if ([response.products count] > 0)
- {
- SKProduct *product = (SKProduct *)[response.products objectAtIndex:0];
- // The product is available, let's submit a payment request to the queue
- SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- }
- }
列表 4. 遵循IAP產品展示最佳實踐
- -(void)fetchProductInformationForIds:(NSArray *)productIds
- {
- // Create a set for your product identifier
- NSSet *mySet = [NSSet setWithObject:your_product_identifier];
- // Create a product request object and initialize it with the above set
- SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:mySet];
- request.delegate = self;
- // Send the request to the App Store
- [request start];
- }
- //Get the App Store's response
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
- {
- if ([response.products count] > 0)
- {
- // Use availableProducts to populate your UI
- NSArray *availableProducts = response.products;
- }
- }
為restoring products提供一個UI
如果你的應用出售 non-consumable、auto-renewable subscription 或者 non-renewing subscription產品,那你必須提供一個允許恢復它們的UI。可以查看 Differences Between Product Types 和 Restoring Purchased Products 獲得更多信息。
處理交易
調用 StoreKit 為 payment queue 添加支付請求:
- [[SKPaymentQueue defaultQueue] addPayment:your_payment];
隊列創建交易對象來處理這個請求。當交易狀態改變時,StoreKit通過調用 paymentQueue: updatedTransactions: 來通知你的觀察者。
In-App Purchase Programming Guide> Delivering Products> Table 4-1 Transaction statuses and corresponding actions 列出了每個交易可能存在的4種交易狀態。要確保觀察者的 paymentQueue: updatedTransactions: 可以在任何時間響應這些狀態。如果IAP產品是由蘋果托管的,那么需在在觀察者上實現 paymentQueue:updatedDownloads: 方法。
提供付費內容
當收到一個狀態是 SKPaymentTransactionStatePurchased 或者 SKPaymentTransactionStateRestored 的交易時,應用程序將會向用戶交付內容或者解鎖app的功能。這些狀態表明已經接收到可售產品的付款。用戶也希望應用能提供付費內容。
如果你的購買產品包括App Store托管內容,要確保調用 SKPaymentQueue's startDownloads: 下載內容。可查看 Unlocking App Functionality 和 Delivering Associated Content 獲得更多信息。
完成交付
交易將會保存在支付隊列中直到它們被移除。每次啟動應用或者從后臺恢復時,StoreKit將會調用觀察者的 paymentQueue: updatedTransactions: 直到它們被移除。大意是你的用戶可能反復請求驗證它們的購買,或者被阻止購買你的產品。
調用 finishTransaction: 從隊列中移除交易。完成的交易是不可恢復的,因此你務必提供內容,下載所有蘋果托管的產品內容,或者在完成交易前完成你的購買流程。查看 Finishing the Transaction 獲得更多信息。
測試IAP的實現
要確保在把應用提交審核之前徹底測試IAP的實現。可在 Suggested Testing Steps 查看多測試場景,在 Frequently Asked Questions 查看各種疑難解答。
參考:
In-App Purchase Programming Guide
Adding In-App Purchase to your iOS and OS X Applications
WWDC 2012: Selling Products with Store Kit
WWDC 2012: Managing Subscriptions with In-App Purchase
WWDC 2013: Using Store Kit for In-App Purchases
WWDC 2014: Optimizing In-App Purchases
本文鏈接:http://www.cocoachina.com/applenews/devnews/2014/0818/9407.html