1. First of all , you should create your products in iTunes Connect. Login into the iTunes Connect,click Manage Your Applications, click the application which need in-app purchase, then in your application page,click manage in-app purchase, then click create new. I think this step is very easy for you.
2.second, create your own store observer.
MyStoreObserver.h
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
#import <StoreKit/SKPaymentTransaction.h>
@interface MyStoreObserver : NSObject< SKPaymentTransactionObserver >
{
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction;
- (void) completeTransaction: (SKPaymentTransaction *)transaction;
- (void) failedTransaction: (SKPaymentTransaction *)transaction;
- (void) restoreTransaction: (SKPaymentTransaction *)transaction;
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;
-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;
@end
MyStoreObserver.m
#import "MyStoreObserver.h"
#import<UIKit/UIKit.h>
#import<UIKit/UIAlert.h>
#import "JSON.h"
#import "UntitledViewController.h"
@implementation MyStoreObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"paymetnQueue");
for (SKPaymentTransaction* transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
NSLog(@"Complete Transaction");
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}
}
-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction
{
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
[transactions release];
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
// Your application should implement these two methods.
// [self recordTransaction: transaction];
// [self provideContent: transaction.payment.productIdentifier];
if (transaction.transactionState==SKPaymentTransactionStatePurchased)
{
[g_View RemoveAdsPurchased];
}
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
NSLog(@"Transaction complete");
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"faliedTransaction" object:nil];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
// [self recordTransaction: transaction];
// [self provideContent: transaction.originalTransaction.payment.productIdentifier];
// save data
// change ads state
[g_View RemoveAdsPurchased];
if (transaction.transactionState==SKPaymentTransactionStatePurchased
|| transaction.transactionState==SKPaymentTransactionStateRestored)
{
[g_View RemoveAdsPurchased];
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction{
}
-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
}
#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
//[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
@end
you should pay attention to this method : - (void) completeTransaction: (SKPaymentTransaction *)transaction. I added my own function to provide the product in my game.
if (transaction.transactionState==SKPaymentTransactionStatePurchased)
{
[g_View RemoveAdsPurchased];
}
when the users pay for products successfully ,the store observer will call updatedTransactions to change the state to SKPaymentTransactionStatePurchased. you can add your own code here. g_View is global variable for untitledControlView. RemoveAdsPurchased is my own function. I did two things in it.one is recode the state, the other is hide the ads in my game.
3. the third step. you should add four function in the untitledControlView.m
#pragma mark SotoreKit method
#define kMyFeatureIdentifier @"BallRemoveAds"
- (void) requestProductData
{
NSLog(@"requestProductData");
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: kMyFeatureIdentifier]];
request.delegate = self;
[request start];
}
#pragma mark request delegate
//!收到产品的消息
- (void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"ProductsRequest did receiveResponse");
NSArray *myProduct = response.products;
NSLog(@"the count of products is %d", [myProduct count]);
// populate UI
for(SKProduct *product in myProduct)
{
NSLog(@"%@", [product description]);
NSLog(@"%@", [product productIdentifier]);
}
SKPayment *payment = [SKPayment paymentWithProductIdentifier:kMyFeatureIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request autorelease];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Alert" message:[error localizedDescription]
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
[alerView show];
[alerView release];
}
- (void) PayforRemoveAds
{
[self requestProductData];
}
kMyFeatureIdentifier is the product id which you defined in the iTunes Connect.It's very important, so you should change it to your product id. you need not to make a change to the other three function, basically, they are just common for every apps.
4. the last step. You call PayforRemoveAds function when the user tap the button which you designed.the function name is not important, you can change it to your own.
then, when the user tap the purchase button, the app store UI will come out. That's all.
If you have any questions, you can post here followed this thread. I'll answer as quickly as I can.
Halley
from Miracrea Games.
The Miracrea Games