Snippets Collections
Purchases.shared.delegate = self

extension YourClass: PurchasesDelegate {
    func purchases(_ purchases: Purchases, receivedUpdated customerInfo: Purchases.CustomerInfo) {
        if let entitlement = customerInfo.entitlements["your_entitlement_id"] {
            if entitlement.isActive == false {
                // subscription has expired
                print("Subscription has expired")
            } else {
                // subscription is active
                print("Subscription is active")
            }
        }
    }
}
//Here is signleton - that means you can create an object of a class outside it

class singleton{
  
  static let shared = singleton()
  init(){} //(Optional to do it or not)
  
  let temp = 5

}

//Usage

viewDidLoad(){
  let num = singleton.shared.temp
  
  //but also you can create object
  let obj = singleton()
  let num = obj.temp
}


//Here is Signleton - that means you cannot create an object of a class outside it

class Singleton{
  
  static let shared = singleton()
  private init(){} //(Optional to do it or not)
  
  let temp = 5

}

//Usage

viewDidLoad(){
  let num = singleton.shared.temp
  
  //but you cannot create object
  //let obj = singleton()
  //let num = obj.temp
}
import StoreKit

//inside viewController
let productId = "your_product_id"


//Action where you want to trigger the purchase
@IBAction func btn_OneTimeButtonClicked(_ sender: UIButton) {
	if SKPaymentQueue.canMakePayments(){
		let paymentRequest = SKMutablePayment()
		paymentRequest.productIdentifier = productId
		SKPaymentQueue.default().add(paymentRequest)
	}
}

//extension to handle the transaction states

extension your_View_Controller: SKPaymentTransactionObserver{
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        transactions.forEach { transaction in
            switch transaction.transactionState{
            case .purchasing:
                print("purchasing now ...")
            case .purchased:
                print("purchase successful !!!")
            case .failed:
                print("purchase failed !!!")
            case .restored:
                print("purchase restored !!!")
            case .deferred:
                print("purchase deferred !!!")
            @unknown default:
                print("Unknown error")
            }
        }
    }
}

// don't forgot to add storekit configuration file in your app if you are testing your app in simulator

star

Fri Apr 12 2024 14:08:58 GMT+0000 (Coordinated Universal Time)

#swift #inapppurchase
star

Tue Apr 09 2024 07:24:08 GMT+0000 (Coordinated Universal Time)

#swift #inapppurchase
star

Mon Apr 08 2024 11:48:34 GMT+0000 (Coordinated Universal Time)

#swift #inapppurchase

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension