##APKit
如何使用
以下代码仅适用于 OS X
if ( ![NSData dataWithContentsOfURL:[NSBundle mainBundle].appStoreReceiptURL] ) {
exit(173);
}
可用于 Objective-C 或 Swift
pod 'APKit', '~> 0.3.1'
运行命令 pod update --no-repo-update
。
在AppDelegate.m
中:
#import <StoreKit/StoreKit.h>
#import <APKit/APKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#warning Add transaction observer
[[SKPaymentQueue defaultQueue] addTransactionObserver:[APStoreObserver sharedInstance]];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
#warning Remove transaction observer
[[SKPaymentQueue defaultQueue] removeTransactionObserver: [APStoreObserver sharedInstance]];
}
设置结果监听器:
- (instancetype)init {
self = [super init];
if ( self ) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleProductRequestNotification:)
name:APProductRequestNotification
object:[APProductManager sharedInstance]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handlePurchasesNotification:)
name:APPurchaseNotification
object:[APStoreObserver sharedInstance]];
}
return self;
}
handleProductRequestNotification
会在获取产品响应时触发。
handlePurchasesNotification
会在获取购买响应时触发。
请求具有标识符的产品
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *productIdentifiers = @[
@"1994101101",
@"1994101102",
@"1994101103"
];
APProductManager *productManager = [APProductManager sharedInstance];
[productManager
fetchProductInformationForIds:productIdentifiers];
}
-(void)handleProductRequestNotification: (NSNotification *)notification {
APProductManager *productRequestNotification = (APProductManager*)notification.object;
APProductRequestStatus result = (APProductRequestStatus)productRequestNotification.status;
if (result == APProductRequestSuccess) {
NSLog(@"VALID: %@", productRequestNotification.availableProducts);
NSLog(@"INVALID: %@", productRequestNotification.invalidProductIds);
}
}
1994101103 是一个无效的产品标识符。
购买:
NSArray *productArray = productRequestNotification.availableProducts;
if ( productArray.count > 0 ) {
SKProduct *product_1 = productArray.firstObject;
APStoreObserver *storeObs = [APStoreObserver sharedInstance];
[storeObs buy:product_1];
}
#pragma mark - Handle purchase notification
-(void)handlePurchasesNotification: (NSNotification *)notification {
APStoreObserver *purchasesNotification = (APStoreObserver *)notification.object;
APPurchaseStatus status = (APPurchaseStatus)purchasesNotification.status;
switch ( status ) {
#pragma - Purchase
case APPurchaseSucceeded: {
NSLog(@"Purchase-Success: %@", purchasesNotification.productsPurchased);
// Verify receipts step.
[self verifyReceipts];
break;
}
case APPurchaseFailed: {
NSLog(@"Purchase-Failed %@", purchasesNotification.errorMessage);
break;
}
case APPurchaseCancelled: {
NSLog(@"Purchase-Cancelled!");
break;
}
#pragma - Restore
case APRestoredSucceeded: {
NSLog(@"Restored-Success: %@", purchasesNotification.productsRestored);
break;
}
case APRestoredFailed: {
NSLog(@"Restored-Failed %@", purchasesNotification.errorMessage);
break;
}
case APRestoredCancelled: {
NSLog(@"Restored-Cancelled!");
break;
}
default:
break;
}
}
请留意第12行,[self verifyReceipts];
这是非常重要的。
验证收据:
如果您遇到了错误,请尝试使用SKReceiptRefreshRequest。
NSURL *localReceiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *data = [NSData dataWithContentsOfURL:localReceiptURL];
NSString *receiptStr = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
将 receiptStr
发送到您的服务器。
关于本地收据验证 本地验证收据。
发行说明
- 0.3.0, 0.3.1: 清理工作区,用2个缩进格式化代码。
- 0.2.0: 下载托管内容。
- 0.1.0: 开发基本功能,初始化仓库。
CDN 上的 ReadMe
MIT.