在程序中,我们可以使用IOS内置库StoreKit.framework里提供的Api实现In-App Purchases产品的购买功能。但如果你不想根据文档再自己写purchase功能,那么有一个第三方的库ECPurchase会适合你。 ECPurchase库封装了purchase的内在逻辑,并且提供了几种验证方式(用于防止iap破解),调用简单方便。ECPurchase库可在文章后面我提供的例子里获得。ECPurchase提供了下面的接口需要开发者自己完成:
1.在App
Delegate中添加Observer
- [[ECPurchase shared] addTransactionObserver];
2.设置ECPurchase的product
delegate(产品列表代理),transaction delegate(购买结果代理),验证方式
- [[ECPurchase shared] setProductDelegate:self];
- [[ECPurchase shared] setTransactionDelegate:self];
- [[ECPurchase shared] setVerifyRecepitMode:ECVerifyRecepitModeiPhone];
3.请求商品列表
- [[ECPurchase shared] requestProductData:identifiers];
实现代理函数绘制UI
- [[ECPurchase shared] requestProductData:identifiers];
4.购买商品
- [[ECPurchase shared] addPayment:proIdentifier];
5.确认结果
如果不需要收据认证实现代理函数:
- -(void)didFailedTransaction:(NSString *)proIdentifier;
- -(void)didRestoreTransaction:(NSString *)proIdentifier;
- -(void)didCompleteTransaction:(NSString *)proIdentifier;
否则实现代理函数:
- -(void)didCompleteTransactionAndVerifySucceed:(NSString *)proIdentifier;
- -(void)didCompleteTransactionAndVerifyFailed:(NSString *)proIdentifier withError:(NSString *)error;