2013.10.31追記
古いので参考にはならないと思います。
「iPhone&Androidアプリ内課金プログラミング完全ガイド」など参考にしてみてください。
エラーチェックも何も無い省略しまくりソフトの概略です。枝葉は自分でちゃんと付け加えてください。
#コピペした時に漏れがあったみたいなので修正しました。2010.2.4
まずはStoreKitObserver
MyStoreKitObserver.h
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface MyStoreKitObserver : NSObject <SKPaymentTransactionObserver> {
}
@end
MyStoreKitObserver.m
#import "MyStoreKitObserver.h"
@implementation MyStoreKitObserver
-(id)init {
if (self = [super init]) {
}
return self;
}
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions {
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
case SKPaymentTransactionStateFailed:
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
@end
あとは適当にボタンを用意して
TestStoreViewController.h
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@class MyStoreKitObserver;
@interface TestStoreViewController : UIViewController <SKProductsRequestDelegate> {
MyStoreKitObserver *mySKObserver;
}
-(IBAction)test1Action:(id)sender;
@end
TestStoreViewController.m
#import "TestStoreViewController.h"
#import "MyStoreKitObserver.h"
@implementation TestStoreViewController
-(IBAction)test1Action:(id)sender {
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet
setWithObject:@"item1"]]; // 自分でつけたアイテムID
request.delegate = self;
[request start];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *myProduct = response.products;
if([myProduct count] > 0) { //1アイテムしか処理してません
SKProduct *product = [myProduct objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:product.productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
mySKObserver = [[MyStoreKitObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver: mySKObserver];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void)_applicationWillTerminate:(NSNotification*)notification
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver: mySKObserver];
[mySKObserver release];
}
一応動作確認はしましたが無保証です。