在Storekit应用启动时添加事务队列观察器的最佳实践

时间:2018-08-17 14:44:03

标签: ios swift in-app-purchase storekit

我在尝试遵循Apple在didFinishLaunchingWithOptions中添加事务队列观察器的建议时遇到了麻烦。

具体地说,我正在尝试改编不执行此操作的Ray Wenderlich教程中的代码-仅在轻按“购买”按钮时才添加观察者。

调用buyProduct函数时,我的应用程序崩溃了:

public func buyProduct(_ product: SKProduct) {
    print("Buying \(product.productIdentifier)...")
    let payment = SKPayment(product: product)
    SKPaymentQueue.default().add(payment)
}

在我的日志中,我看到IAPHelper的init被调用了两次,因此调用了SKPaymentQueue.default().add(self)了两次。我敢肯定这就是问题所在,但是我很困惑如何解决。

这是我的代码...

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  IAPHelper.sharedInstance = IAPHelper() // creates the singleton for IAPHelper

  // other code here //

  return true
}

IAPHelper.swift:

import StoreKit

/// Notification that is generated when a product is purchased.
public let IAPHelperPurchaseNotification = "IAPHelperPurchaseNotification"

/// Notification that is generated when a transaction fails.
public let IAPHelperTransactionFailedNotification = "IAPHelperTransactionFailedNotification"

/// Notification that is generated when cannot retrieve IAPs from iTunes.
public let IAPHelperConnectionErrorNotification = "IAPHelperConnectionErrorNotification"

/// Notification that is generated when we need to stop the spinner.
public let IAPHelperStopSpinnerNotification = "IAPHelperStopSpinnerNotification"

/// Product identifiers are unique strings registered on the app store.
public typealias ProductIdentifier = String

/// Completion handler called when products are fetched.
public typealias ProductsRequestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> ()

open class IAPHelper : NSObject  {

  /// MARK: - User facing API
  fileprivate let productIdentifiers: Set<ProductIdentifier>
  fileprivate var purchasedProductIdentifiers = Set<ProductIdentifier>()
  fileprivate var productsRequest: SKProductsRequest?
  fileprivate var productsRequestCompletionHandler: ProductsRequestCompletionHandler?

  static var sharedInstance = IAPHelper()  //  singleton

  override init() {

    // Set up the list of productIdentifiers
    let PackOf4000Coins =  "com.xxx.xxx.4000Coins"   
    let PackOf10000Coins =  "com.xxx.xxx.10000Coins"
    let PackOf30000Coins =  "com.xxx.xxx.30000Coins"
    let PackOf75000Coins =  "com.xxx.xxx.75000Coins"
    let PackOf175000Coins = "com.xxx.xxx.175000Coins"
    let PackOf750000Coins = "com.xxx.xxx.750000Coins"
    let RemoveAds =  "com.xxx.xxx.RemoveAds"
    let PlayerEditor =  "com.xxx.xxx.PlayerEditor"

    self.productIdentifiers = [PackOf4000Coins, PackOf10000Coins, PackOf30000Coins, PackOf75000Coins, PackOf175000Coins, PackOf750000Coins, RemoveAds, PlayerEditor]

    for productIdentifier in self.productIdentifiers {
        let purchased = UserDefaults.standard.bool(forKey: productIdentifier)
        if purchased {
            purchasedProductIdentifiers.insert(productIdentifier)
            print("Previously purchased: \(productIdentifier)")
        } else {
            print("Not purchased: \(productIdentifier)")
        }
    }

    super.init()

    SKPaymentQueue.default().add(self)
  }
}

// MARK: - StoreKit API

extension IAPHelper {

  public func requestProducts(_ completionHandler: @escaping ProductsRequestCompletionHandler) {
    productsRequest?.cancel()
    productsRequestCompletionHandler = completionHandler

    productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
    productsRequest!.delegate = self
    productsRequest!.start()
  }

  public func buyProduct(_ product: SKProduct) {
    print("Buying \(product.productIdentifier)...")
    let payment = SKPayment(product: product)
    SKPaymentQueue.default().add(payment)
  }

  public func isProductPurchased(_ productIdentifier: ProductIdentifier) -> Bool {
      return purchasedProductIdentifiers.contains(productIdentifier)
  }

  public class func canMakePayments() -> Bool {
    return SKPaymentQueue.canMakePayments()
  }

  public func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()

  }

  public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
    print("Restore queue finished.")
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperStopSpinnerNotification), object: nil)
  }

  public func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
    print("Restore queue failed.")
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperConnectionErrorNotification), object: nil)
  }

}

// MARK: - SKProductsRequestDelegate

extension IAPHelper: SKProductsRequestDelegate {
  public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    print("Loaded list of products...")
    let products = response.products
    productsRequestCompletionHandler?(true, products)
    clearRequestAndHandler()

    for p in products {
        print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
    }
  }

  public func request(_ request: SKRequest, didFailWithError error: Error) {
    print("Failed to load list of products.")
    print("Error: \(error.localizedDescription)")
    productsRequestCompletionHandler?(false, nil)
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperConnectionErrorNotification), object: nil)
    clearRequestAndHandler()
  }

  fileprivate func clearRequestAndHandler() {
    productsRequest = nil
    productsRequestCompletionHandler = nil
  }
}

// MARK: - SKPaymentTransactionObserver

extension IAPHelper: SKPaymentTransactionObserver {

  public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        switch (transaction.transactionState) {
        case .purchased:
            completeTransaction(transaction)
            break
        case .failed:
            failedTransaction(transaction)
            break
        case .restored:
            restoreTransaction(transaction)
            break
        case .deferred:
            break
        case .purchasing:
            break
        }
    }
}

  fileprivate func completeTransaction(_ transaction: SKPaymentTransaction) {
    print("completeTransaction...")

 deliverPurchaseNotificationForIdentifier(
            transaction.payment.productIdentifier)

    SKPaymentQueue.default().finishTransaction(transaction)
  }

  fileprivate func restoreTransaction(_ transaction: SKPaymentTransaction) {
    guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

    print("restoreTransaction... \(productIdentifier)")
    deliverPurchaseNotificationForIdentifier(productIdentifier)
    SKPaymentQueue.default().finishTransaction(transaction)
}

fileprivate func failedTransaction(_ transaction: SKPaymentTransaction) {
    print("failedTransaction...")
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperStopSpinnerNotification), object: nil)
    if transaction.error!._code != SKError.paymentCancelled.rawValue {
        print("Transaction Error: \(String(describing: transaction.error?.localizedDescription))")
        NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperTransactionFailedNotification), object: nil)
    } else {
        print("Transaction Error else statement")
    }

    SKPaymentQueue.default().finishTransaction(transaction)
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperTransactionFailedNotification), object: nil)

}

fileprivate func deliverPurchaseNotificationForIdentifier(_ identifier: String?) {
    guard let identifier = identifier else { return }

    purchasedProductIdentifiers.insert(identifier)
    UserDefaults.standard.set(true, forKey: identifier)
    UserDefaults.standard.synchronize()
    NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperPurchaseNotification), object: identifier)
  }
}

GameStoreViewController.swift(仅相关代码):

@objc func tableView(_ tableView: UITableView!, didSelectRowAtIndexPath indexPath: IndexPath!) {
    if IAPHelper.canMakePayments() {
        activitySpinnerStart()
        let product = _coinProducts[(indexPath as NSIndexPath).row]
        IAPHelper.sharedInstance.buyProduct(product)  // Purchasing the product. Fires productPurchased(notification:)
    } else {
        showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoIAPAllowedMessage"))
    }
}

1 个答案:

答案 0 :(得分:1)

最后,我使用SwiftyStoreKit为我解决了这个问题。我强烈推荐它。

编辑以显示SwiftyStoreKit.completeTransactions进入appDelegate中的didFinishLaunchingWithOptions()unlockIAPContent()是我放置购买逻辑的地方,它将处理促销代码和未完成的交易:

    // This registers the transaction observer and listens for unfinished transactions
    SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
        for purchase in purchases {
            switch purchase.transaction.transactionState {
            case .purchased, .restored: 

                // Unlock content
                self.unlockIAPContent(productID: purchase.productId) 

                if purchase.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(purchase.transaction)
                }

            case .failed, .purchasing, .deferred:
                break // do nothing
            }
        }
    }