现在的位置: 首页 > 综合 > 正文

IAP 程序内购

2013年12月13日 ⁄ 综合 ⁄ 共 10427字 ⁄ 字号 评论关闭

最近用到IAP内置购买,阅读官方文档,在网上找了些资料,在这里作下整理,以便日后查找和修改,主要流程方向确定,文档和相关转载内容截图不一一指出,google一堆。

1.查找官方文档,两张目录截图,对主要流程大致了解:

                             

官方文档:

https://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/Introduction/Introduction.html


2.In App Purchase Programming Guide

购买程序向导

Adding a Store to Your Application

向你的应用程序中添加商店

This chapter provides guided steps for adding a store to your application.

这个章节介绍了向你的应用程序中添加商店的详细步骤。

The Step-By-Step Process

When you set up the project, make sure to link toStoreKit.framework. You can then add a store by following these steps:

当安装工程时,确保链接到StoreKit.framework.,然后就可以按照下面的步骤添加一个商店到你的应用程序。

(1)Decide what products you wish to deliver with your application.

首先确定你想把哪个产品交付到你的应用程序
There are limitations on the types of features you can offer. Store Kit does not allow your application to patch itself or download additional code. Products must either work with existing code in your application or must be implemented using data files
delivered from a remote server. If you wish to add a feature that requires changes to your source code, you need to ship an updated version of your application.

在你能提供的功能的类型上有一些限制。商店包(Store Kit)不允许你的应用软件为自己补丁或者下载附加的源码。产品必须同在你的应用程序或者通过远程服务器下载的数据文件来运行。如果你想为产品加上一些特色,则需要更改你的源码,你需要为你的应用程序发送一个更新版本

(2)Register product information for each product with iTunes Connect.

每个和iTunes Connect产品注册产品信息。
You revisit this step every time you want to add a new product to your application’s store. Every product requires a unique product identifier string. The App Store uses this string to look up product information and to process payments. Product identifiers
are specific to your iTunes Connect account and are registered with iTunes Connect in a way similar to how you registered your application.

当你每一次向你的应用程序添加一个新的产品时你要重复这一步。每一个产品需要一个唯一的产品标识符字符串。App Store用这个字符串来查看产品信息和处理付款。产品标识符是特定于你的iTunes Connect帐户,并且它和iTunes Connect注册时类似于你注册你的应用程序。
The process to create and register product information is described in the iTunes Connect Developer Guide.

更新产品的过程需要生成并注册产品信息,这些信息在iTunes Connect Developer Guide.中有描述。

(3)Determine whether payments can be processed.

确定是否可以理付款。
A user can disable the ability to make purchases inside applications. Your application should check to see whether payments can be purchased before queuing new payment requests. Your application might do this before displaying a store to the user (as
shown here) or it may defer this check until the user actually attempts to purchase an item. The latter allows the user to see items that they could purchase when payments are enabled.

可以禁用在程序内购买这个功能。你的应用程序应该在新的付款请求排队之前确认付款是否可以购买。你的应用程序也可以在把商店显示给用户之前来确认是否可以购买或者在用户实际企图购买一个项目时确认是否可以购买,当付款被启用时后者可以使用户看见他们能购买的项目。

if ([[SKPaymentQueue defaultQueue] canMakePayments])

{

   ... // Display a store to the user.

       //显示一个商店给用户

}

else

{

   ... // Warn the user that purchases are disabled.

             //警告用不能交易

}


(4)Retrieve information about products.

检索有关产品的信息
Your application creates a SKProductsRequest object
and initializes it
with a set of product identifiers for the items you wish to sell, attaches adelegate to
the request, and then starts it. The response holds the localized product information for all valid product identifiers.

你的用程序创建了一个SKProductsRequest对象,并用你想卖的一系列项目的产品标识符来初始化这个SKProductsRequest对象。附加一个delegate到这个request,然后开始。响应信息中有所有的有效地产品标识符的本地化产品信息。

- (void) requestProductData

{

   SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: kMyFeatureIdentifier]];

   request.delegate = self;

   [request start];

}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

{

    NSArray *myProduct = response.products;

    // populate UI

    // 填充界面

    [request autorelease];

}

(5)Add a user interface that displays products to the user.

添加一个用户界面来显示产品给用户

Store Kit does not provide user interface classes. The look and feel of how you offer products to your customers is up to you!

商店包(Store Kit)不提供用户界面类。你提供的产品的界面风格由你来定制。

(6)Register a transaction observer with the payment queue.

用付费队列来注册一个交易观察者。
Your application should instantiate a transaction observer and add it as an observer of the payment queue.

你的用程序应实例化一个交易观察者,并把它以付费队列的观察者的身份添加上去。

MyStoreObserver *observer = [[MyStoreObserver alloc] init];

[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];


Your application should add the observer when your application launches. The App Store remembers queued transactions even if your application exited before completing all transactions. Adding an observer
during initialization ensures that all previously queued transactions are seen by your application.

你的用程序在启动时就应添加一个交易观察者。即使你的应用程序在完成所有的交易之前就退出,App Store也能够记住放在队列中的交易。在初始化的过程中就添加一个交易观察者,就能够保证你的应用程序能看见之前没有执行完的所有放在队列中的交易。

(7)Implement thepaymentQueue:updatedTransactions: method
on 
MyStoreObserver.

在MyStoreObserver上实现paymentQueue:updatedTransactions:
The observer’s paymentQueue:updatedTransactions: method
is called whenever new transactions are created or updated.

观察者的paymentQueue:updatedTransactions:方法在新的交易被创建或更新时就会被调用。

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{

    for (SKPaymentTransaction *transaction in transactions)

    {

        switch (transaction.transactionState)

        {

            case SKPaymentTransactionStatePurchased:

               [self completeTransaction:transaction];

               break;

            case SKPaymentTransactionStateFailed:

                [self failedTransaction:transaction];

                break;

            case SKPaymentTransactionStateRestored:

                [self restoreTransaction:transaction];

            default:

                break;

        }

    }

}

(8)Your observer provides the product when the user successfully purchases an item.

当用户成功的购买了一个项目时,你的观察者就会为你提供刚购买的产品。

- (void) completeTransaction: (SKPaymentTransaction *)transaction

{

// Your application should implement these two methods.

//    你的应用程序应该 实现这两个方法

    [self recordTransaction: transaction];

    [self provideContent: transaction.payment.productIdentifier];

// Remove the transaction from the payment queue.

//从付费队列中删除交易

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}


A successful transaction includes a transactionIdentifier property and atransactionReceipt property that record the details of the processed payment. Your application is not required to do anything with
this information. You may wish to record this information to establish an audit trail for the transaction. If your application uses a server to deliver content, the receipt can be sent to your server and validated by the App Store.

一个成功的交易应该包括一个transactionIdentifier属性和transactionReceipt属性,后者用于记录处理过的付费的详细细节。你的应用程序不要求对这个信息作任何操作。你可能希望通过建立一个交易的审计跟踪(audit trail)来记录这个信息。如果你的应用程序使用服务器来传输内容(content),收据(receipt)就会被发送到你的服务器,并被App
Store鉴定是否有效。

It is critical that your application take whatever steps are necessary to provide the product that the user purchased. Payment has already been collected, so the user expects to receive the new purchase. See“Feature
Delivery”
 for suggestions on how you might implement this.

你的用程序操作一些必要步骤,而这些步骤是可以提供用户购买过的产品的,这是很重要的。付费已经被收集起来,因此用户唯一期望的就是收到新的购买。你可以通过看“Feature Delivery””来了解你如何实现这些的建议。
Once you’ve delivered the product, your application must call finishTransaction: to
complete the transaction. When you callfinishTransaction:, the transaction is removed from the payment queue. To ensure that products are not lost, your application should deliver the product before callingfinishTransaction:.

一旦你已经发出了产品,那你的应用程序就必须调用finishTransaction:来完成交易。当你调用了finishTransaction:,交易就从付费队列中删除掉。为了确保产品不会丢失,你的应用程序应在调用finishTransaction:之前就把产品发出去。

(9)Finish the transaction for a restored purchase.

为一个恢复的购买请求完成交易

- (void) restoreTransaction: (SKPaymentTransaction *)transaction

{

    [self recordTransaction: transaction];

    [self provideContent: transaction.originalTransaction.payment.productIdentifier];

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}


This routine is similar to that for a purchased item. A restored purchase provides a new transaction, including a different transaction identifier and receipt. You can save this information separately as part of any audit trail if you desire. However,
when it comes time to complete the transaction, you’ll want to recover the original transaction that holds the actual payment object and use its product identifier.

这个过程购买一个项目类似。一个被恢复的购买提供了一个新的交易,它包括一个不同的交易标识符和收据(receipt)。你也可以把个信息独作跟踪审计的一部分存起来。尽管如此,当交易完成时,你要恢复持有实际支付对象和使用其产品标识符的原始的交易。

(10)Finish the transaction for a failed purchase.

完成一个购买的交易。

- (void) failedTransaction: (SKPaymentTransaction *)transaction

{

    if (transaction.error.code != SKErrorPaymentCancelled)

    {

        // Optionally, display an error here.

    }

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}


Usually a transaction fails because the user decided not to purchase the item. Your application can read theerror field on a failed transaction to learn exactly why the transaction failed.
通常由于用决定不购买这项目导致一个交易失。你的应用程序可以通过读取购买的交易中的error字段来了解交易失的原因。

The only requirement for a failed purchase is that your application remove it from the queue. If your application chooses to put up an dialog displaying the error to the user, you should avoid presenting
an error when the user cancels a purchase.

对于购买失败的唯一要求就是你的应用程序从队列中移除它。因此如果你的程序通过一个对话框来向用户显示错误,那么你应该避免当用户取消购买时提出一个错误。

(11)With all the infrastructure in place, you can finish the user interface. When the user selects an item in the store, create a payment object and add it to the payment queue.

你可以用所有提供的基础设施(infrastructure)完成用户界面。当用户选择了商店中的一个项目时,就创建一个交易对象,并把它加到交易队列中。

SKPayment *payment = [SKPayment paymentWithProductIdentifier:kMyFeatureIdentifier];

[[SKPaymentQueue defaultQueue] addPayment:payment];


If your store offers the ability to purchase more than one of a product, you can create a single payment and set the quantity property.

如果你的商店能够让用户购买多个商品的话,你就可以仅仅创建一个交易然后设置数量(quantity)属性就可以了

SKMutablePayment *payment = [SKMutablePayment paymentWithProductIdentifier:kMyFeatureIdentifier];

payment.quantity = 3;

[[SKPaymentQueue defaultQueue] addPayment:payment];


Where to Go Next

The code provided in these steps is best used for the built-in product model. If your application uses a server to deliver content, you are responsible for designing and implementing the protocols
used to communicate between your iPhone application and your server. Your server should also verify receipts before delivering products to your application.

这些过程中提供的代码能够很好的用于基于产品的模式。如果你的应用程序使用服务器发送内容,你就必须设计并实现用于在你的iPhone程序和你的服务器之间通信的协议。你的服务器也同样需要在把产品发送到你的应用程序之前对收据进行验证。



3.以下英文说明主要过程:

Create and Fetch a Product Description

  1. Create a unique App ID
  2. Generate and install a new provisioning profile
  3. Update the bundle ID and code signing profile in Xcode
  4. If you haven’t already, submit your application metadata in iTunes Connect
  5. If you haven’t already, submit your application binary in iTunes Connect
  6. Add a new product for in-app purchase
  7. Write code for fetching the product description  //待完
  8. Wait a few hours


4.IAP(In App Purchase)

IAP的全称是In App Purchase,应用内付费。这种业务模式允许用户免费下载试用,对应用内提供的商品选择消费,比如购买游戏道具,购买游戏等级等等。相比完全收费的应用而言,应用内付费给用户试用的机会,不会让优秀的应用因为缺乏用户的认知而丧失消费者;而且对于开发商,也不需要为了让用户试用而单独发布一款免费的精简版本。

商品(profuct):




使用未越狱的设备选择:
测试IAP的项目不能使用越狱的设备,否则会出现无法连接到应用商店的错误。恢复设备到未越狱的系统后,登陆Provisioning
Portal添加设备的UID。
使用没有通配符的App ID
在定义App的Bundle ID的时候,我曾经介绍过可以使用类似 com.jamesli.* 这样的值来覆盖多个应用的ID。这种定义方式不能用在打算使用IAP的应用上面,定义IAP的应用必须使用唯一的ID,如com.jamesli.ghostbride。如果正确定义了,应用的In-App
Purchase的功能是默认开启的,如下图:



iTunesConnect创建应用:

登陆iTunesConnect,创建一个新的应用,即使该应用尚未开发,也可以用一些假的文字和图片来代替,创建好之后切记要点击Ready
to Upload binary将应用的状态变为Waiting for upload。

抱歉!评论已关闭.