M2US52D6AA.com.yourcompany.yourapp M2US52D6AA.com.yourcompany.*
M2US52D6AA.com.yourcompany.yourapp com.yourcompany.yourapp
InAppPurchaseManager.h #import <StoreKit/StoreKit.h> #define kinapppurchasemanagerproductsfetchednotification @"kinapppurchasemanagerproductsfetchednotification"
@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate> SKProduct *proupgradeproduct; SKProductsRequest *productsrequest; InAppPurchaseManager.m - (void)requestproupgradeproductdata NSSet *productidentifiers = [NSSet setwithobject:@"com.runmonster.runmonsterfree.upgrade topro" ]; productsrequest = [[SKProductsRequest alloc] initwithproductidentifiers:productidentifiers]; productsrequest.delegate = self; [productsrequest start]; we will release the request object in the delegate callback #pragma mark - #pragma mark SKProductsRequestDelegate methods (void)productsrequest:(skproductsrequest *)request didreceiveresponse:(skproductsresponse *)response NSArray *products = response.products; proupgradeproduct = [products count] == 1? [[products firstobject] retain] : nil; if (proupgradeproduct) NSLog(@"Product title: %@", proupgradeproduct.localizedtitle); NSLog(@"Product description: %@", proupgradeproduct.localizeddescription); NSLog(@"Product price: %@", proupgradeproduct.price); NSLog(@"Product id: %@", proupgradeproduct.productidentifier);
for (NSString *invalidproductid in response.invalidproductidentifiers) NSLog(@"Invalid product id: %@", invalidproductid); finally release the reqest we alloc/init ed in requestproupgradeproductdata [productsrequest release]; [[NSNotificationCenter defaultcenter] postnotificationname:kinapppurchasemanagerproductsf etchednotification object:self userinfo:nil]; SKProduct+LocalizedPrice.h #import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @interface SKProduct (LocalizedPrice) @property (nonatomic, readonly) NSString *localizedprice; @end SKProduct+LocalizedPrice.m #import "SKProduct+LocalizedPrice.h" @implementation SKProduct (LocalizedPrice) - (NSString *)localizedprice
NSNumberFormatter *numberformatter = [[NSNumberFormatter alloc] init]; [numberformatter setformatterbehavior:nsnumberformatterbehavior10_4]; [numberformatter setnumberstyle:nsnumberformattercurrencystyle]; [numberformatter setlocale:self.pricelocale]; NSString *formattedstring = [numberformatter stringfromnumber:self.price]; [numberformatter release]; return formattedstring; @end SKProduct
StoreKit InAppPurchaseManager.h add a couple notifications sent out when the transaction completes #define kiaptransactionfailednotification @"kiaptransactionfailednotification" #define kiaptransactionsucceedednotification @"kiaptransactionsucceedednotification" @interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver> public methods - (void)loadstore; - (BOOL)canMakePurchases; - (void)purchaseproupgrade; @end InAppPurchaseManager.m #define kiapproupgradeproductid @"com.yourcompany.yourapp.product"
#pragma - #pragma Public methods call this method once on startup - (void)loadstore restarts any purchases if they were interrupted last time the app was open [[SKPaymentQueue defaultqueue] addtransactionobserver:self]; get the product description (defined in early sections) [self requestproupgradeproductdata]; call this before making a purchase - (BOOL)canMakePurchases return [SKPaymentQueue canmakepayments]; kick off the upgrade transaction - (void)purchaseproupgrade SKPayment *payment = [SKPayment paymentwithproductidentifier:kiapproupgradeproductid] ; [[SKPaymentQueue defaultqueue] addpayment:payment]; #pragma - #pragma Purchase helpers
saves a record of the transaction by storing the receipt to disk - (void)recordtransaction:(skpaymenttransaction *)transaction if ([transaction.payment.productidentifier isequaltostring:kiapproupgradeproductid]) save the transaction receipt to disk [[NSUserDefaults standarduserdefaults] setvalue:transaction.transactionreceipt forkey:@"proupgradetransactionreceipt" ]; [[NSUserDefaults standarduserdefaults] synchronize]; enable pro features - (void)providecontent:(nsstring *)productid if ([productid isequaltostring:kiapproupgradeproductid]) enable the pro features [[NSUserDefaults standarduserdefaults] setbool:yes forkey:@"isproupgradepurchased" ]; [[NSUserDefaults standarduserdefaults] synchronize]; removes the transaction from the queue and posts a notification with the transaction result - (void)finishtransaction:(skpaymenttransaction *)transaction wassuccessful:(bool)wassuccessful
remove the transaction from the payment queue. [[SKPaymentQueue defaultqueue] finishtransaction:transaction]; NSDictionary *userinfo = [NSDictionary dictionarywithobjectsandkeys:transaction, @"transaction", nil]; if (wassuccessful) send out a notification that we ve finished the transaction [[NSNotificationCenter defaultcenter] postnotificationname:kiaptransactionsucceedednotifica tion object:self userinfo:userinfo]; else send out a notification for the failed transaction [[NSNotificationCenter defaultcenter] postnotificationname:kiaptransactionfailednotificatio n object:self userinfo:userinfo]; called when the transaction was successful - (void)completetransaction:(skpaymenttransaction *)transaction [self recordtransaction:transaction]; [self providecontent:transaction.payment.productidentifier] ; [self finishtransaction:transaction wassuccessful:yes];
called when a transaction has been restored and and successfully completed - (void)restoretransaction:(skpaymenttransaction *)transaction [self recordtransaction:transaction.originaltransaction]; [self providecontent:transaction.originaltransaction.paymen t.productidentifier]; [self finishtransaction:transaction wassuccessful:yes]; called when a transaction has failed - (void)failedtransaction:(skpaymenttransaction *)transaction if (transaction.error.code!= SKErrorPaymentCancelled) error! [self finishtransaction:transaction wassuccessful:no]; else this is fine, the user just cancelled, so don t notify [[SKPaymentQueue defaultqueue] finishtransaction:transaction]; #pragma mark - #pragma mark SKPaymentTransactionObserver methods
called when the transaction status is updated - (void)paymentqueue:(skpaymentqueue *)queue updatedtransactions:(nsarray *)transactions for (SKPaymentTransaction *transaction in transactions) switch (transaction.transactionstate) case SKPaymentTransactionStatePurchased: [self completetransaction:transaction]; break; case SKPaymentTransactionStateFailed: [self failedtransaction:transaction]; break; case SKPaymentTransactionStateRestored: [self restoretransaction:transaction]; break; default: break;