http://github.com/dreamengineinteractive/DEStoreKitManager
DEStoreKitManager
是一个 MIT 许可证的库,用于简化 iOS 内购。它可以自动处理内购的样板代码,让您专注于送出应用和制造更优质的产品。
DEStoreKitManager
被设计得尽可能灵活和轻量级,使用起来非常方便。您可以使用 blocks 或 delegates 从 App Store 获取产品并执行购买交易。
使用 blocks,它可以让管理 StoreKit 的交互变得从来没有那么简单:您可以把所有逻辑封装在一部分代码中。
如果您想使用 delegates,DEStoreKitManager
也旨在使这一点变得更容易。对于每个产品获取或购买,您都可以使用不同的 delegate,并且每个 delegate 只会通知其发起的获取/购买。
默认情况下,DEStoreKitManager
也自动将您收到的产品缓存到内存中,因此您无需跟踪单个 SKProducts
。如果您想自己跟踪产品,DEStoreKitManager
也允许您这样做。
要获取产品列表,请在 DEStoreKitManager
中调用 fetchProductsWithIdentifiers:onSuccess:onFailure:
。当获取成功或失败时,您的 blocks 将会被调用。
如果您不想使用自动的产品缓存功能,那么您可以使用 fetchProductsWithIdentifiers:onSuccess:onFailure:cacheResult:
并将最后一个参数设为 NO
。然后,当产品要购买时,您只需要使用 purchaseProduct:onSuccess:onRestore:onFailure:onCancel:onVerify:
而不是 purchaseProductWithIdentifier:…
。
NSSet *productIdentifiers = [NSSet setWithObjects:@"com.example.removeads", @"com.example.upgrade", nil];
[[DEStoreKitManager sharedManager] fetchProductsWithIdentifiers:productIdentifiers
onSuccess: ^(NSArray *products, NSArray *invalidIdentifiers) {
// handle successful product fetch
}
onFailure: ^(NSError *error) {
// handle failure here.
}
];
要购买产品,只需使用 purchaseProduct:onSuccess:onRestore:onFailure:onCancel:onVerify:
。如果您正在利用自动产品缓存功能,则可以改用 purchaseProductWithIdentifier:…
,这样如果缓存中有匹配的 SKProduct
,它将返回 YES
并启动交易;否则返回 NO
,表示没有缓存的与提供的标识符匹配的产品,因此不会发生交易。
DEStoreKitManager
不会强迫您遵循特定的收据验证程序。相反,verify
块负责验证。一旦您的程序确定了收据/交易的合法性,您就需要在 DEStoreKitManager
中调用 transaction:didVerify:
,然后它将根据传递给 DEStoreKitManager
的内容以及交易本身的状况调用您的 success
、restore
或 failure
块。
DEStoreKitManager
如果您选择将某些块传递为 nil
,它会内置一些便利功能。
verify
块传递了 nil
,则 DEStoreKitManager
将不会尝试验证您的收据,而是会自动完成交易。restore
块传递了 nil
,则当交易需要恢复时,DEStoreKitManager
将调用您的 success
块。cancel
块传递了 nil
,则如果交易被取消,DEStoreKitManager
将调用您的 failure
块。BOOL willAttemptPurchase = [[DEStoreKitManager sharedManager] purchaseProductWithIdentifier: @"com.example.removeads"
onSuccess: ^(SKPaymentTransaction *transaction) {
// record the purchase here
}
onRestore: ^(SKPaymentTransaction *transaction) {
// record the purchase here
}
onFailure: ^(SKPaymentTransaction *transaction) {
// handle failure here
}
onCancel: ^(SKPaymentTransaction *transaction) {
// handle cancel here
}
onVerify: ^(SKPaymentTransaction *transaction) {
// verify the receipt here. when validity has been determined, make sure to call [[DEStoreKitManager sharedManager] transaction:transaction didVerify:isValid];
}];
要获取产品列表,请确保您的委托对象遵循 DEStoreKitManagerDelegate
协议,并实现了以下回调:productsFetched:invalidIdentifiers:
和 productsFetchedFailed:
。然后,只需调用 fetchProductsWithIdentifier:delegate:
并等待回调消息即可。
您可以同时进行任意数量产品获取(尽管我们建议每次获取更多的产品标识符以进行更少的获取)。
如果您不希望使用自动产品缓存,则可以使用 fetchProductsWithIdentifier:delegate:cacheResult:
并将最后一个参数传递为 NO
。然后,当产品要购买时,只需使用 purchaseProduct:delegate:
而不是 purchaseProductWithIdentifier:delegate:
。
#import "DEStoreKitManager.h"
@interface MyViewController: UIViewController <DEStoreKitManagerDelegate>
…
@implementation MyViewController
…
-(void) viewDidLoad {
[super viewDidLoad];
[self fetchProducts];
}
-(void) fetchProducts {
NSSet *productIdentifiers = [NSSet setWithObjects:@"com.example.removeads", @"com.example.upgrade", nil];
[[DEStoreKitManager sharedManager] fetchProductsWithIdentifiers: productIdentifiers
delegate: self];
}
-(void)productsFetched: (NSArray *)products
invalidIdentifiers: (NSArray *)invalidIdentifiers {
// If you specified DEStoreKitManager not to cache, then you are responsible for holding onto the products array.
// Otherwise, you can record that the products have been fetched here.
}
-(void) productsFetchFailed:(NSError *)error {
// Handle the failure here
}
…
要购买产品,请确保您的委托实现了以下方法:transactionSucceeded:
、transactionRestored:
(可选)、transactionFailed:
和 transactionCanceled:
(可选)。然后,使用 DEStoreKitManager
启动交易。与产品检索类似,您可以同时进行任意数量的交易。
如果您的委托中没有实现 transactionRestored:
,则恢复购买会被发送到 transactionSucceeded:
。同样,如果未实现 transactionCanceled:
,则在用户取消购买的情况下会调用 transactionFailed:
。
如果您依赖 DEStoreKitManager
来缓存产品,可以调用 purchaseProductWithProductIdentifier:delegate:
以启动购买流程。如果产品尚未获取并缓存到 DEStoreKitManager
中,则此方法将返回 NO
,这意味着不会尝试购买。否则,如果 DEStoreKitManager
找到一个具有匹配产品标识符的 SKProduct
,此方法将返回 YES
,表明购买交易已经开始。
如果您在 DEStoreKitManager
之外跟踪 SKProducts
,则应使用 purchaseProduct:delegate:
,这将立即开始购买交易。
-(IBAction)buyButtonTapped:(id)sender {
[[DEStoreKitManager sharedManager] purchaseProductWithProductIdentifier:@"com.example.removeads" delegate:self];
[self.activityIndicator startAnimating];
}
-(void) transactionSucceeded:(SKPaymentTransaction *)transaction {
// record the purchase here
}
-(void) transactionFailed:(SKPaymentTransaction *)transaction {
// handle failed transaction here
}
DEStoreKitManager
不会强迫您遵循特定的收据验证程序。实际上,虽然我们推荐进行收据验证,但 DEStoreKitManager
并不要求任何验证。
相反,您可以选择通过让您的代理实现 transactionNeedsVerification:
方法来验证收据。然后,您的代理将负责确定交易是否有效。
在确定有效性后,代理负责通过调用 transaction:didVerify:
并通知它收据是否有效来通知 DEStoreKitManager
。
一旦这样做了,如果收据有效,DEStoreKitManager
将继续调用 transactionSucceeded:/transactionRestored:
;如果收据无效,则调用 transactionFailed:
。