在应用内购买中按下“购买”按钮时如何检测和执行代码

时间:2019-04-16 12:59:10

标签: ios swift in-app-purchase uiactivityindicatorview

我想在用户按下iTunes购买按钮以最终购买订阅时显示activityIndicator。花费了一些时间处理购买,完成后,我想清除activityIndicator

我尝试使用applicationWillResignActive来检测其中一种,使用不同的解决方案来检测iTunes弹出窗口,并且在要求用户输入密码的地方可以检测到此问题。当产品被请求但仍然是同一件事时,我也发布了通知。有没有办法知道何时按下iTunes购买按钮。下面是我的代码。

extension Notification.Name {
    public static let AppDelegatePurchasesIsProccessing = NSNotification.Name("AppDelegatePurchasesIsProccessing")
}

func applicationWillResignActive(_ application: UIApplication) {
        NotificationCenter.default.post(name: .AppDelegatePurchasesIsProccessing, object: self)
      }
}

为购买而显示的叠加层

override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      self.notifyForUserIsPurchasingProduct {
          self.activityIndicator.startAnimating()
     }
 }

override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      self.notifyForUserHasPurchasedProduct {
          self.activityIndicator.stopAnimating()
          self.dismiss(animated: true)
     }
}
func notifyForUserHasPurchasedProduct(_ action: @escaping () -> Void) {
     NotificationCenter.default.addObserver(forName: .AppDelegateUserHasPurchasedProductNotification, object: nil, queue: nil) { (_) in
          action()
    }
}

func notifyForUserIsPurchasingProduct(_ action: @escaping () -> Void) {
      NotificationCenter.default.addObserver(forName: .AppDelegatePurchasesIsProccessing, object: nil, queue: nil) { (_) in
          action()
     }
}

0 个答案:

没有答案