In-App-Purchase 中文版

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 to StoreKit.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 a delegate 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 the paymentQueue: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 a transactionReceipt 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 call finishTransaction:, the transaction is removed from the payment queue. To ensure that products are not lost, your application should deliver the product before calling finishTransaction:.

一旦你已经发出了产品,那你的应用程序就必须调用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 the error 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程序和你的服务器之间通信的协议。你的服务器也同样需要在把产品发送到你的应用程序之前对收据进行验证。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值