iOS in app purchase (iap) objective-c
iOS 2015. 8. 13. 00:41//
// ViewController.h
// iaptest
//
// Created by smilejsu on 2015. 8. 12..
// Copyright (c) 2015년 smilejsu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Storekit/StoreKit.h>
@interface ViewController : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver>{
}
@property int coinAmount;
@property (strong, nonatomic) SKProduct *product;
@property (weak, nonatomic) IBOutlet UILabel *labelCoinAmount;
- (IBAction)BuyCoin:(id)sender;
- (void) getProductInfo;
- (void) validateReceipt;
- (void) increaseCoin;
@end
//
// ViewController.m
// iaptest
//
// Created by smilejsu on 2015. 8. 12..
// Copyright (c) 2015년 smilejsu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self getProductInfo];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
if( response.products.count != 0 ){
_product = (SKProduct*)response.products[0];
}
}
-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for( SKPaymentTransaction *transaction in transactions ){
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self increaseCoin];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
break;
case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
- (IBAction)BuyCoin:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void) getProductInfo {
if( [SKPaymentQueue canMakePayments] ){
NSSet *productID = [NSSet setWithObject:@"smilejsu.iap.coinpack"];
SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers: productID];
request.delegate = self;
[request start];
}
}
-(void) increaseCoin {
_coinAmount += 50;
_labelCoinAmount.text = [NSString stringWithFormat:@"%d", _coinAmount];
[self validateReceipt];
}
-(void) validateReceipt {
// Load the receipt from the app bundle.
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if (!receipt) { /* No local receipt -- handle the error. */ }
/* ... Send the receipt data to your server ... */
NSLog(@"Send the receipt data to your server");
// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = @{
@"receipt-data": [receipt base64EncodedStringWithOptions:0]
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
if (!requestData) { /* ... Handle error ... */ }
// Create a POST request with the receipt data.
// https://sandbox.itunes.apple.com/verifyReceipt , https://buy.itunes.apple.com/verifyReceipt
NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!jsonResponse) { /* ... Handle error ...*/ }
/* ... Send a response back to the device ... */
NSLog(@"%@", jsonResponse);
}
}];
}
@end
'iOS' 카테고리의 다른 글
verify receipt send to server (0) | 2015.08.14 |
---|---|
swift IAP (0) | 2015.08.11 |
Linking is broken for static library (since Xcode6) (0) | 2015.07.30 |
[Swift] Class 1-1 (inheritance, referance copy) (0) | 2015.07.25 |
[Swift] function 1-4 (0) | 2015.07.23 |