本文介绍了成功时应用内购买通话功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个购买产品的按钮,该产品应隐藏背景视图,以防止用户使用应用程序的扩展程序。按下按钮时,购买产品的代码来自外部文件。因此,我知道在购买成功时隐藏视图的方法。

I have a button which purchases a product which should hide the background view shielding the user from the using the extension to the app. When the button is pressed, the code to buy the product is from an external file. Because of this, I have know way of hiding the view when the purchase comes back successful.

来自其他文件的代码

for transaction: AnyObject in transactions {
        if let trans: SKPaymentTransaction = transaction as? SKPaymentTransaction {
            switch trans.transactionState {
            case .purchased:
                print("Product Purchased")
                let purchased = UserDefaults.standard.bool(forKey: 
 "Analytics")
                UserDefaults.standard.set(true, forKey: "Analytics")

是否可以从外部swift文件中将函数调用到视图控制器类?您可以采用哪种其他方式解决问题?

Is it possible to call a function to a view controller class from an external swift file? What other way could you approach the problem?

推荐答案

您可以使用块或代理来传回信息。

You can use blocks or delegates to pass information back.

PaymentClass 子类中声明一个委托

protocol PaymentDelegate {
  func payment(completed: Bool)
}

class YourExternalPaymentClass {
  weak var delegate: PaymentDelegate?
}

现在位于 ViewController 您在哪里显示叠加层配置此代理

Now in the ViewController where you are showing the overlay configure this delegate

在活动完成的付款类中,调用您的方法

Inside the payment class where the event is completed, call your method

   self.delegate?.payment(completed: true)

这样可以将数据传递回ViewController,你可以隐藏或显示你想要的任何内容。

this way the data can be passed back to the ViewController and you can hide or show whatever you want.

这篇关于成功时应用内购买通话功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 17:05