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
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
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
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
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
// | |
// InAppManager.swift | |
// | |
// Created by Ellina Kuznetcova on 12/10/2016. | |
// Copyright © 2016 Flatstack. All rights reserved. | |
// | |
import Foundation | |
import StoreKit |
NewerOlder