paymentQueue在执行代码之前等待解雇

时间:2018-06-04 20:34:05

标签: ios swift in-app-purchase

我有一个问题,我添加了一个应用程序内购买,一切正常,但购买的最后一部分有一些奇怪的行为...当用户用用户/密码填写所有输入时,应用程序会显示警告:“你们都已经完成了”和关闭按钮。我的问题是应用程序在用户关闭警报之前开始获取PRO内容。问题是我想在获取数据时显示警报,在用户关闭“全部设置”警报之前我无法显示此警报......任何想法?

这是我的IAPHelper,购买完成后我发送通知

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  for transaction in transactions {
    switch (transaction.transactionState) {
    case .purchased:
      complete(transaction: transaction)
      break
    case .failed:
      fail(transaction: transaction)
      break
    case .restored:
      restore(transaction: transaction)
      break
    case .deferred:
      break
    case .purchasing:
      break
    }
  }
}
private func complete(transaction: SKPaymentTransaction) {
  print("complete...")
  deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
  SKPaymentQueue.default().finishTransaction(transaction)
}
private func deliverPurchaseNotificationFor(identifier: String?) {    
  guard let identifier = identifier else { return }
  purchasedProductIdentifiers.insert(identifier)
  NotificationCenter.default.post(name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier)
}

这里我有我所有应用内购买的清单,购买完成后我运行updateToProDb()

    func handlePurchaseNotification(_ notification: Notification) {
      guard let productID = notification.object as? String else { return }
      if productID == iFormularioProducts.SKU_DB {      
        DispatchQueue.main.async(execute:{
          self.updateToProDb()
        })
      }
    }
    func updateToProDb(){
      Utils.showAlertOnCompletition(
        targetVC: self,
        title: "Wait",
        message: "Updating database...",
        positive: nil,
        neutral: nil,
        negative: nil,
        positiveHandler: { (UIAlertAction) in},
        neutralHandler: { (UIAlertAction) in},
        negativeHandler: { (UIAlertAction) in},
        completion: { () in

            let webDataSource = WebDataSource();
            webDataSource.didPostRequest(params: [...]]) { data in

                self.dismiss(animated: true, completion: {
                    self.getInApps()
                })
            }
        }
      )
    }

Utils,这里我有一些共同的功能,不太重要

static func showAlertOnCompletition(targetVC: UIViewController,
                    title :String,
                    message :String,
                    positive :String?,
                    neutral :String?,
                    negative :String?,
                    positiveHandler :@escaping (UIAlertAction) -> Void,
                    neutralHandler :@escaping (UIAlertAction) -> Void,
                    negativeHandler :@escaping (UIAlertAction) -> Void,
                    completion: @escaping () -> Void) {

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

if (positive != nil){
    alert.addAction((UIAlertAction(title: positive, style: .default, handler: positiveHandler)))
}
if (neutral != nil){
    alert.addAction((UIAlertAction(title: neutral, style: .default, handler: neutralHandler)))
}
if (negative != nil){
    alert.addAction((UIAlertAction(title: negative, style: .default, handler: negativeHandler)))
}
targetVC.present(alert, animated: true, completion: completion)
}

0 个答案:

没有答案