Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // IAPHandler.swift
- //
- //
- // Created by Jonathan Wukitsch on 9/17/19.
- // Copyright © 2016-2019 Jonathan Wukitsch. All rights reserved.
- //
- import UIKit
- import StoreKit
- enum IAPHandlerAlertType {
- case setProductIds
- case disabled
- case restored
- case purchased
- var message: String {
- switch self {
- case .setProductIds: return "Product IDs not set. Call setProductIDs method."
- case .disabled: return "Purchases are disabled on this device."
- case .restored: return "Your purchases have successfully been restored."
- case .purchased: return "Your purchase was successful."
- }
- }
- }
- class IAPHandler: NSObject {
- // MARK: - Shared Object
- // MARK: -
- static let shared = IAPHandler()
- private override init() {}
- // MARK: - Properties
- // MARK: - Private
- fileprivate var productIds = [String]()
- fileprivate var productID = ""
- fileprivate var productsRequest = SKProductsRequest()
- fileprivate var fetchProductCompletion: (([SKProduct]) -> Void)?
- fileprivate var productToPurchase: SKProduct?
- fileprivate var purchaseProductCompletion: ((IAPHandlerAlertType, SKProduct?, SKPaymentTransaction?) -> Void)?
- // MARK: - Public
- var isLogEnabled: Bool = true
- // MARK: - Methods
- // MARK: - Public
- // Set product IDs
- func setProductIds(ids: [String]) {
- self.productIds = ids
- }
- // Make a purchase
- func canMakePurchases() -> Bool { return SKPaymentQueue.canMakePayments() }
- func purchase(product: SKProduct, completion: @escaping ((IAPHandlerAlertType, SKProduct?, SKPaymentTransaction?) -> Void)) {
- self.purchaseProductCompletion = completion
- self.productToPurchase = product
- if self.canMakePurchases() {
- let payment = SKPayment(product: product)
- SKPaymentQueue.default().add(self)
- SKPaymentQueue.default().add(payment)
- log("Product to purchase: \(product.productIdentifier)")
- productID = product.productIdentifier
- } else {
- completion(IAPHandlerAlertType.disabled, nil, nil)
- }
- }
- // Restore purchases
- func restorePurchase() {
- SKPaymentQueue.default().add(self)
- SKPaymentQueue.default().restoreCompletedTransactions()
- }
- // Fetch available IAP products
- func fetchAvailableProducts(completion: @escaping (([SKProduct]) -> Void)) {
- self.fetchProductCompletion = completion
- if self.productIds.isEmpty {
- log(IAPHandlerAlertType.setProductIds.message)
- fatalError(IAPHandlerAlertType.setProductIds.message)
- } else {
- productsRequest = SKProductsRequest(productIdentifiers: Set(self.productIds))
- productsRequest.delegate = self
- productsRequest.start()
- }
- }
- // MARK: - Private
- fileprivate func log <T> (_ object: T) {
- if isLogEnabled {
- NSLog("\(object)")
- }
- }
- }
- // MARK: - Product Request Delegate and Payment Transaction Methods
- // MARK: -
- extension IAPHandler: SKProductsRequestDelegate, SKPaymentTransactionObserver {
- // Request IAP products
- func productsRequest(_ request:SKProductsRequest, didReceive response: SKProductsResponse) {
- if (response.products.count > 0) {
- if let completion = self.fetchProductCompletion {
- completion(response.products)
- }
- }
- }
- func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
- if let completion = self.purchaseProductCompletion {
- completion(IAPHandlerAlertType.restored, nil, nil)
- }
- }
- // IAP payment queue
- func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
- for transaction:AnyObject in transactions {
- if let trans = transaction as? SKPaymentTransaction {
- switch trans.transactionState {
- case .purchased:
- log("Product purchase completed")
- SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
- if let completion = self.purchaseProductCompletion {
- completion(IAPHandlerAlertType.purchased, self.productToPurchase, trans)
- }
- break
- case .failed:
- log("Product purchase failed")
- SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
- break
- case .restored:
- log("Product restored")
- SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
- break
- default:
- break
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement