IAP Best Practices

Source: Internet
Author: User

IAP Best Practices

This document is Apple's new Technical Note -- In-App Purchase Best Practices released In August 5. It describes IAP Best Practices In iOS and OS X applications.

The following is a list of IAP best practices recommended to developers.

Add a transaction queue observer when the application starts

The application calls StoreKit to link the observer to the payment queue.

 
 
  1. [SKPaymentQueue defaultQueue] addTransactionObserver:your_observer];  

When you resume or run your app, StoreKit automatically notifies you of the changes to the content of the payment queue) the observer adds an observer when the application starts to ensure that it exists when all apps start. This will allow your application to receive all the payment queue reminders.

Considering the application, an observer is created for the DetailViewController class of the application before adding a payment request to the queue, such as table 1. The existence time of this observer is the same as that of the DetailViewController instance. If a network failure occurs, for example, the app cannot complete the purchase process, and the related transactions are still in the payment queue. After the app recovers, it does not have an observer because the observer is removed when the app is sent to the background. Therefore, your application will not receive transaction notifications in the queue.

List 1. Do not follow the best practices for implementing transaction observer: when a user attempts to buy a product, the application adds an observer for the payment queue:

 
 
  1. @implementation DetailViewController  
  2.                   ....  
  3.    
  4. // Called when a customer attempts to purchase a product  
  5. - (IBAction)purchase:(id)sender  
  6. {  
  7.     // Register an observer to the payment queue  
  8.     [[SKPaymentQueue defaultQueue] addTransactionObserver:self];  
  9.    
  10.     // Create a payment request  
  11.     SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:self.product];  
  12.    
  13.     // Submit the payment request to the payment queue  
  14.     [[SKPaymentQueue defaultQueue] addPayment:payment];  
  15. }  
  16.              ....  
  17. @end  

List 2. Follow the best practices for registering transaction observers

 
 
  1. @implementation AppDelegate  
  2.    
  3. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  4. {  
  5.     // Attach an observer to the payment queue  
  6.     [[SKPaymentQueue defaultQueue] addTransactionObserver:self];  
  7.     return YES;  
  8. }  
  9.    
  10. // Called when the application is about to terminate  
  11. - (void)applicationWillTerminate:(UIApplication *)application  
  12. {  
  13.      // Remove the observer  
  14.     [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];  
  15. }  
  16.    
  17.             ....  
  18. @end  

StoreKit removes the observer from the payment queue when calling the app:

Similarly, if the observer is not removed from the payment queue, StoreKit will attempt to notify the above observer, resulting in the application crash, as if the observer no longer exists.

Ask the App Store for product information before displaying the App Store UI.

Before deciding to display purchased items on the user interface, your application must first send a product request to the App Store. Sending a product request allows you to determine whether the product can be sold in the App Store to prevent the display of products that cannot be purchased. You can view Retrieving Product Information to learn how to create a Product request. App Store uses the SKResponse object to respond to product requests, and uses its products attribute to update your UI, so that your users can only see products available for sale in App Store.

List 3. Do not follow IAP product display best practices: after displaying a salable product, the APP asks the App Store for related product information.

 
 
  1. // App first displays a product for sale, then queries the App Store about it when a customer attempts to purchase it  
  2. - (IBAction)purchase:(id)sender  
  3. {  
  4.     // Create a set for your product identifier  
  5.     NSSet *productSet = [NSSet setWithObject:@"your_product_identifier"];  
  6.     // Create a product request object and initialize it with the above set  
  7.     SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:productSet];  
  8.    
  9.     request.delegate = self;  
  10.     // Send the request to the App Store  
  11.     [request start];  
  12. }  
  13.    
  14.    
  15.    
  16. // Get the App Store's response  
  17. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  
  18. {  
  19.    // No purchase will take place if there are no products available for sale.  
  20.   // As a result, StoreKit won't prompt your customer to authenticate their purchase.  
  21.    if ([response.products count] > 0)  
  22.    {  
  23.         SKProduct *product = (SKProduct *)[response.products objectAtIndex:0];  
  24.    
  25.         // The product is available, let's submit a payment request to the queue  
  26.         SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];  
  27.         [[SKPaymentQueue defaultQueue] addPayment:payment];  
  28.     }  
  29. }  

List 4. Best practices for IAP Product Display

 
 
  1. -(void)fetchProductInformationForIds:(NSArray *)productIds  
  2. {  
  3.     // Create a set for your product identifier  
  4.     NSSet *mySet = [NSSet setWithObject:your_product_identifier];  
  5.    
  6.     // Create a product request object and initialize it with the above set  
  7.     SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:mySet];  
  8.    
  9.     request.delegate = self;  
  10.    
  11.     // Send the request to the App Store  
  12.     [request start];  
  13. }  
  14.    
  15.    
  16. //Get the App Store's response  
  17. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  
  18. {  
  19.     if ([response.products count] > 0)  
  20.     {  
  21.         // Use availableProducts to populate your UI  
  22.         NSArray *availableProducts = response.products;  
  23.     }  
  24. }  

Provides a UI for restoring products
If your applications sell non-consumable, auto-renewable subgraphs, or non-renewing subgraphs, you must provide a UI that allows them to be restored. You can view Differences Between Product Types and Restoring Purchased Products for more information.
Transaction Processing
Call StoreKit to add a payment request for the payment queue:

 
 
  1. [[SKPaymentQueue defaultQueue] addPayment:your_payment];  

The queue creates a transaction object to process this request. When the transaction status changes, StoreKit notifies your observer by calling paymentQueue: updatedTransactions.

In-App Purchase Programming Guide> Delivering Products> Table 4-1 Transaction statuses and corresponding actions list four possible Transaction States for each Transaction. Make sure that the observer's paymentQueue: updatedTransactions: can respond to these statuses at any time. If the IAP product is hosted by Apple, You need to implement paymentQueue: updatedDownloads: Method on the observer.

Provides paid content

When you receive a transaction in the SKPaymentTransactionStatePurchased or SKPaymentTransactionStateRestored state, the application will deliver content to the user or unlock the app function. These statuses indicate that you have received payment for the available products. Users also want the application to provide paid content.

If your purchased product includes App Store-hosted content, make sure to call SKPaymentQueue's startDownloads: download content. You can view Unlocking App Functionality and Delivering Associated Content for more information.

Complete delivery

Transactions are stored in the payment queue until they are removed. Each time an application is started or recovered from the background, StoreKit will call the observer's paymentQueue: updatedTransactions: until they are removed. Your users may repeatedly request to verify their purchases or be prevented from purchasing your products.

Call finishTransaction: remove the transaction from the queue. The completed transaction cannot be recovered. Therefore, you must provide the content, download all the content of apple-hosted products, or complete your purchase process before completing the transaction. View Finishing the Transaction for more information.

Test IAP implementation

Ensure that the IAP implementation is thoroughly tested before the application is submitted for review. You can view multiple test scenarios in Suggested Testing Steps and find various troubleshooting methods in Frequently Asked Questions.

Refer:

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

Link: http://www.cocoachina.com/applenews/devnews/2014/0818/9407.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.