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
// | |
// InAppManager.swift | |
// | |
// Created by Ellina Kuznetcova on 12/10/2016. | |
// Copyright © 2016 Flatstack. All rights reserved. | |
// | |
import Foundation | |
import StoreKit |
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
extension InAppManager: SKPaymentTransactionObserver { | |
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { | |
for transaction in transactions { | |
guard let productType = ProductType(rawValue: transaction.payment.productIdentifier) else {fatalError()} | |
switch transaction.transactionState { | |
case .purchasing: | |
case .purchased: | |
case .failed: | |
case .restored: | |
case .deferred: |
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
protocol InAppManagerDelegate: class { | |
func inAppLoadingStarted() | |
func inAppLoadingSucceded(productType: ProductType) | |
func inAppLoadingFailed(error: Swift.Error?) | |
func subscriptionStatusUpdated(value: Bool) | |
} |
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
enum InAppErrors: Swift.Error { | |
case noSubscriptionPurchased | |
case noProductsAvailable | |
var localizedDescription: String { | |
switch self { | |
case .noSubscriptionPurchased: | |
return "No subscription purchased" | |
case .noProductsAvailable: | |
return "No products available" |
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
var isSubscriptionAvailable: Bool = true | |
{ | |
didSet(value) { | |
self.delegate?.subscriptionStatusUpdated(value: value) | |
} | |
} |
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
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { | |
for transaction in transactions { | |
guard let productType = ProductType(rawValue: transaction.payment.productIdentifier) else {fatalError()} | |
switch transaction.transactionState { | |
case .purchasing: | |
self.delegate?.inAppLoadingStarted() | |
case .purchased: | |
SKPaymentQueue.default().finishTransaction(transaction) | |
self.updateSubscriptionStatus() | |
self.isSubscriptionAvailable = true |
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
class InAppManager: NSObject { | |
static let shared = InAppManager() | |
} | |
extension InAppManager: SKPaymentTransactionObserver {} | |
extension InAppManager: SKProductsRequestDelegate {} |
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
enum ProductType: String { | |
case weekly = "com.example.app.weekly" | |
case monthly = "com.example.app.monthly" | |
case yearly = "com.example.app.yearly" | |
static var all: [ProductType] { | |
return [.weekly, .monthly, .yearly] | |
} | |
} |
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
func loadProducts() { | |
let productIdentifiers = Set<String>(ProductType.all.map({$0.rawValue})) | |
let request = SKProductsRequest(productIdentifiers: productIdentifiers) | |
request.delegate = self | |
request.start() | |
} |
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
extension InAppManager: SKProductsRequestDelegate { | |
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { | |
self.products = response.products | |
} | |
} |
OlderNewer