How to add iAP with iAPHelper and CocoaPods?

| | August 11, 2015

I’ve tried to add in app purchase to my app to provide users the option to buy the app without iAd. So I searched for a good cocoa pod because the implementation of in app purchase is very heavy. Cause iaphelper seems to be the best I’ve tried to implement it in my project like this but as you see that doesn’t work that well. Anybody an idea how to make this work?

// .h

@interface MenuViewController : UIViewController <ADBannerViewDelegate, SKPaymentTransactionObserver, SKProductsRequestDelegate>

-(IBAction)removeAds:(UIButton *)sender;

-(void)AdsRemoved;

@end

//.m

-(IBAction)removeAds:(UIButton *)sender {
}

-(void)Purchased {
    if(![IAPShare sharedHelper].iap) {
        NSSet* dataSet = [[NSSet alloc] initWithObjects:@"com.comquas.iap.test", nil];

        [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
    }

    [IAPShare sharedHelper].iap.production = NO;

    [[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
     {
         if(response > 0 ) {
             SKProduct* product =[[IAPShare sharedHelper].iap.products objectAtIndex:0];

             [[IAPShare sharedHelper].iap buyProduct:product
                                        onCompletion:^(SKPaymentTransaction* trans){

                                            if(trans.error)
                                            {
                                                NSLog(@"Fail %@",[trans.error localizedDescription]);
                                            }
                                            else if(trans.transactionState == SKPaymentTransactionStatePurchased) {

                                                [[IAPShare sharedHelper].iap checkReceipt:trans.transactionReceipt AndSharedSecret:@"your sharesecret" onCompletion:^(NSString *response, NSError *error) {

                                                    //Convert JSON String to NSDictionary
                                                    NSDictionary* rec = [IAPShare toJSON:response];

                                                    if([rec[@"status"] integerValue]==0)
                                                    {
                                                        NSString *productIdentifier = trans.payment.productIdentifier;
                                                        [[IAPShare sharedHelper].iap provideContent:productIdentifier];
                                                        NSLog(@"SUCCESS %@",response);
                                                        NSLog(@"Pruchases %@",[IAPShare sharedHelper].iap.purchasedProducts);
                                                    }
                                                    else {
                                                        NSLog(@"Fail");
                                                    }
                                                }];
                                            }
                                            else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                                                NSLog(@"Fail");
                                            }
                                        }];//end of buy product
         }
     }];
}

One Response to “How to add iAP with iAPHelper and CocoaPods?”

  1. There is a really helpful tutorial on In App Purchase with useful classes which will help to integrate In App Purchase less than half an hour.
    http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial

    If you find any problem afterward, you can visit this blog.

Leave a Reply