Coder Social home page Coder Social logo

iaphelper's Introduction

IAP helper for Apple in app purchases. It uses ARC and blocks for ease of use. Ready to use with newsstand subscriptions.

##Require

  • StoreKit
  • iOS 5 or later
  • ARC

##How to use

  • Add IAPHelper folder to your project.
  • Add Storekit framework

Cocoapod

pod 'IAPHelper'

Initialize

if(![IAPShare sharedHelper].iap) {

    NSSet* dataSet = [[NSSet alloc] initWithObjects:@"com.comquas.iap.test", nil];

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

}

Production Mode On/Off

[IAPShare sharedHelper].iap.production = NO;

Request Products

[[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
     {

     }];

Purchase

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

Check Receipt with shared secret

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

}];

Check Receipt without shared secret

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

}];

Saving Product Identifier

[[IAPShare sharedHelper].iap provideContent:productIdentifier];

Check for Previous Purchase

if([[IAPShare sharedHelper].iap isPurchasedProductsIdentifier:@"com.comquas.iap.test"]
	{
		// require saving product identifier first
	}

Purchased Products

NSLog(@"%@",[IAPShare sharedHelper].iap.purchasedProducts);

Clear Purchases

[[IAPShare sharedHelper].iap clearSavedPurchasedProducts];
[[IAPShare sharedHelper].iap clearSavedPurchasedProductByID:@"com.myproduct.id"];

Restore Purchase

[[IAPShare sharedHelper].iap restoreProductsWithCompletion:^(SKPaymentQueue *payment, NSError *error) {

		//check with SKPaymentQueue

		// number of restore count
		int numberOfTransactions = payment.transactions.count;

		for (SKPaymentTransaction *transaction in payment.transactions)
		{
            NSString *purchased = transaction.payment.productIdentifier;
	        if([purchased isEqualToString:@"com.myproductType.product"])
        	{
				//enable the prodcut here
	        }
    	}

}];

Example

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
       }
}];

iaphelper's People

Contributors

saturngod avatar streeter avatar

Watchers

James Cloos avatar raybon.lee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.