Giter VIP home page Giter VIP logo

iaphelper's Introduction

What's IAPHelper

IAPHelper simply wraps the API of Apple's In-App Purchase using Swift. Very lightweight and easy to use.

IAPHelper Usage

Request Product List

var productIdentifiers = Set<ProductIdentifier>()
productIdentifiers.insert("product_id_1")
productIdentifiers.insert("product_id_2")

IAP.requestProducts(productIdentifiers) { (response, error) in
	if let products = response?.products where !products.isEmpty {
		// Get the valid products
	   
	} else if let invalidProductIdentifiers = response?.invalidProductIdentifiers {
		// Some products id are invalid
	   
	} else {
		// Some error happened
	}
}

Purchase Product

 IAP.purchaseProduct(productIdentifier, handler: { (productIdentifier, error) in
	if let identifier = productIdentifier {
		// The product of 'productIdentifier' purchased.
	     
	} else if let error = error as? NSError {
      if error.code == SKError.Code.paymentCancelled.rawValue {
		// User cancelled
        
      } else {
		// Some error happened
      }
    }
})

Restore

 IAP.restorePurchases { (productIdentifiers, error) in
	 if !productIdentifiers.isEmpty {
	 	// Products restored
	   
	 } else if let error = error as? NSError {
       if error.code == SKError.Code.paymentCancelled.rawValue {
	  	// User cancelled
        
       } else {
	  	// Some error happened
       }
      
    } else {
      // No previous purchases were found.
    }
}

Validate Receipt

IAP.validateReceipt(Constants.IAPSharedSecret) { (statusCode, products) in
	if statusCode == ReceiptStatus.NoRecipt.rawValue {
		// No Receipt in main bundle
	} else {
		// Get products with their expire date.
	}
})

Note: IAPHelper directly validate with Apple's server. It's simple, but has risk. You decide to use your own server or not. Here's what Apple suggested:

Use a trusted server to communicate with the App Store. Using your own server lets you design your app to recognize and trust only your server, and lets you ensure that your server connects with the App Store server. It is not possible to build a trusted connection between a user’s device and the App Store directly because you don’t control either end of that connection.

Integrate IAPHelper in Your Project

Just copy IAPHelper.swift to your project, and use it as the demo shows.

IAPHelper Demo

NOTE: You need to change the app bundle id and product id to your own. And also set your shared secret in Constants.swift.

Apps using IAPHelper

It would be great to show your apps using IAPHelper here. Pull requests welcome :)

Note

This library can't help you understand the basic concepts for IAP. For it, please refer to these documents.

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.