https://github.com/wilddylan/apkit
IAP framework!
https://github.com/wilddylan/apkit
iap
Last synced: about 1 year ago
JSON representation
IAP framework!
- Host: GitHub
- URL: https://github.com/wilddylan/apkit
- Owner: wilddylan
- License: mit
- Created: 2015-09-24T04:11:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-10-22T03:08:58.000Z (over 7 years ago)
- Last Synced: 2025-05-13T01:16:00.731Z (about 1 year ago)
- Topics: iap
- Language: Objective-C
- Homepage:
- Size: 564 KB
- Stars: 159
- Watchers: 3
- Forks: 23
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
##APKit
[](https://opensource.org/licenses/mit-license.php) [](https://app.fossa.io/projects/git%2Bgithub.com%2FWildDylan%2FAPKit?ref=badge_shield)
###### How to use
The under code block only for OS X:
```objective-c
if ( ![NSData dataWithContentsOfURL:[NSBundle mainBundle].appStoreReceiptURL] ) {
exit(173);
}
```
Can be used in Objective-C or swift:
```ruby
pod 'APKit', '~> 0.3.1'
```
run command `pod update --no-repo-update`.
In`AppDelegate.m`:
```objective-c
#import
#import
```
```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#warning Add transaction observer
[[SKPaymentQueue defaultQueue] addTransactionObserver:[APStoreObserver sharedInstance]];
return YES;
}
```
```objective-c
- (void)applicationWillTerminate:(UIApplication *)application {
#warning Remove transaction observer
[[SKPaymentQueue defaultQueue] removeTransactionObserver: [APStoreObserver sharedInstance]];
}
```
Set result listener:
```objective-c
- (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`will be fired when get response for product.
`handlePurchasesNotification` will be fired when get response for purchase.
Request product with identifier:
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *productIdentifiers = @[
@"1994101101",
@"1994101102",
@"1994101103"
];
APProductManager *productManager = [APProductManager sharedInstance];
[productManager
fetchProductInformationForIds:productIdentifiers];
}
```
```objective-c
-(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 is an invalid product identifier.
Purchase:
```objective-c
NSArray *productArray = productRequestNotification.availableProducts;
if ( productArray.count > 0 ) {
SKProduct *product_1 = productArray.firstObject;
APStoreObserver *storeObs = [APStoreObserver sharedInstance];
[storeObs buy:product_1];
}
```
```objective-c
#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;
}
}
```
Watch for line 12, ` [self verifyReceipts];` it's important.
Verify receipt:
If you get some error, try to use [SKReceiptRefreshRequest](https://developer.apple.com/reference/storekit/skreceiptrefreshrequest)。
```objective-c
NSURL *localReceiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *data = [NSData dataWithContentsOfURL:localReceiptURL];
NSString *receiptStr = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
```
send `receiptStr`to your server.
About local receipt verify [local verify your receipt](https://github.com/WildDylan/iap-local-receipt).
or use [go-iap-verify-receipt](https://github.com/awa/go-iap)
###### Release note
- 0.3.2: Update comments and add some documents, update license, remove unused files and folders.
- 0.3.0, 0.3.1: Clean workspace, format code with 2 indent.
- 0.2.0: Download Hosted content.
- 0.1.0: basic features develope, initialized repo.
###### ReadMe in cdn
[APKit introduction](http://blog.devdylan.cn/APKit/)
###### License
MIT.
[](https://app.fossa.io/projects/git%2Bgithub.com%2FWildDylan%2FAPKit?ref=badge_large)