Last active
April 5, 2019 04:06
-
-
Save fx-studio/37d98c9aa052fadcd89c7aa0a075258c to your computer and use it in GitHub Desktop.
Example - Custom UIViewController with StoreKit & SwiftyStoreKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BaseViewController.InApp.swift | |
// CrazyMath | |
// | |
// Created by Tien Le P. on 4/4/19. | |
// Copyright © 2019 Fx Studio All rights reserved. | |
// | |
import StoreKit | |
import SwiftyStoreKit | |
import UIKit | |
import SVProgressHUD | |
// MARK: User facing alerts | |
extension BaseViewController { | |
//MARK: Get Infor Products | |
func getInforAllProducts(identifiers: Set<String>, completed: @escaping (Bool, Set<SKProduct>?) -> ()) { | |
SVProgressHUD.show() | |
SwiftyStoreKit.retrieveProductsInfo(identifiers) { result in | |
SVProgressHUD.dismiss() | |
if let error = result.error { | |
completed(false, nil) | |
self.showAlert(message: error.localizedDescription) | |
} else { | |
completed(true, result.retrievedProducts) | |
} | |
} | |
} | |
func getInfor(productIdentifier: String, completed: @escaping (Bool, SKProduct?) -> ()) { | |
SVProgressHUD.show() | |
SwiftyStoreKit.retrieveProductsInfo([productIdentifier]) { result in | |
SVProgressHUD.dismiss() | |
if let product = result.retrievedProducts.first { | |
completed(true, product) | |
} | |
else if let invalidProductId = result.invalidProductIDs.first { | |
completed(false, nil) | |
self.showAlert(message: "Invalid product identifier: \(invalidProductId)") | |
} | |
else { | |
completed(false, nil) | |
self.showAlert(message: result.error!.localizedDescription) | |
} | |
} | |
} | |
//MARK: Purchase | |
func buyProduct(productIdentifier: String, completed: @escaping (Bool) -> ()) { | |
SVProgressHUD.show() | |
SwiftyStoreKit.purchaseProduct(productIdentifier, quantity: 1, atomically: true) { result in | |
SVProgressHUD.dismiss() | |
switch result { | |
case .success(let purchase): | |
print("♦️♦️ Purchase Success: \(purchase.productId)") | |
completed(true) | |
case .error(let error): | |
completed(false) | |
switch error.code { | |
case .unknown: | |
self.showAlert(message:"Unknown error. Please contact support") | |
case .clientInvalid: | |
self.showAlert(message:"Not allowed to make the payment") | |
case .paymentCancelled: break | |
case .paymentInvalid: | |
self.showAlert(message:"The purchase identifier was invalid") | |
case .paymentNotAllowed: | |
self.showAlert(message:"The device is not allowed to make the payment") | |
case .storeProductNotAvailable: | |
self.showAlert(message:"The product is not available in the current storefront") | |
case .cloudServicePermissionDenied: | |
self.showAlert(message:"Access to cloud service information is not allowed") | |
case .cloudServiceNetworkConnectionFailed: | |
self.showAlert(message:"Could not connect to the network") | |
case .cloudServiceRevoked: | |
self.showAlert(message:"User has revoked permission to use this cloud service") | |
default: | |
self.showAlert(message:(error as NSError).localizedDescription) | |
} | |
} | |
} | |
} | |
//MARK: Restore | |
func restorePurchases(completed: @escaping (Bool, [String]?) -> ()) { | |
SVProgressHUD.show() | |
SwiftyStoreKit.restorePurchases(atomically: true) { results in | |
SVProgressHUD.dismiss() | |
if results.restoreFailedPurchases.count > 0 { | |
completed(false, nil) | |
print("♦️♦️ Restore Failed: \(results.restoreFailedPurchases)") | |
self.showAlert(message: "Restore Failed") | |
} | |
else if results.restoredPurchases.count > 0 { | |
print("♦️♦️ Restore Success: \(results.restoredPurchases)") | |
var productIDs: [String] = [] | |
for purchase in results.restoredPurchases { | |
productIDs.append(purchase.productId) | |
} | |
completed(true, productIDs) | |
} | |
else { | |
completed(false, nil) | |
print("♦️♦️ Nothing to Restore") | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get info purchase | |
self.getInfor(productIdentifier: "YOUR_PRODUCT_IDENTIFIER") { (done, product) in | |
// your code | |
} | |
// get info of array products | |
self.getInforAllProducts(identifiers: ["YOUR_PRODUCT_IDENTIFIER_1", | |
"YOUR_PRODUCT_IDENTIFIER_2", | |
"YOUR_PRODUCT_IDENTIFIER_3"]) { (done, products) in | |
// your code | |
} | |
// buy product | |
self.buyProduct(productIdentifier: "YOUR_PRODUCT_IDENTIFIER") { (done) in | |
// your code | |
} | |
// restore product | |
self.restorePurchases { (done, productIDs) in | |
// your code | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pod 'SwiftyStoreKit' | |
pod 'SVProgressHUD' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment