ios 我应该在Swift原生代码中为Flutter插件传递哪个视图控制器?

disbfnqx  于 2022-11-19  发布在  iOS
关注(0)|答案(2)|浏览(155)

我试图显示一个Adcolony广告从斯威夫特本机代码在我的Flutter插件,这是我的斯威夫特代码看起来像-

if let interstitial = self.interstitial, !interstitial.expired, let 
  rootViewController = UIApplication.shared.keyWindow?.rootViewController
  {
   interstitial.show(withPresenting: rootViewController)
   }

我的代码工作得很好,但是我的视图控制器从来不显示插播广告。有人能帮助我吗,因为我不知道我应该传递哪个视图控制器来显示我的广告。文档说要使用self,但是我的插件不是视图控制器,所以它不工作。

t2a7ltrp

t2a7ltrp1#

试试看:

if let vc = UIApplication.shared.delegate?.window??.rootViewController  as? FlutterViewController {
  interstitial.show(withPresenting: vc)
}
vc9ivgsu

vc9ivgsu2#

这段代码对我很有效。参考image_picker插件。

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "startTestActivity":
        let vc = YourViewController()
        vc.modalPresentationStyle = .fullScreen;
        viewController(with: nil)?.present(vc, animated: true, completion: nil)
        result(true)
    default:
        result(FlutterMethodNotImplemented)
    }
}

func viewController(with window: UIWindow?) -> UIViewController? {
    var windowToUse = window
    if windowToUse == nil {
        for window in UIApplication.shared.windows {
            if window.isKeyWindow {
                windowToUse = window
                break
            }
        }
    }
    
    var topController = windowToUse?.rootViewController
    while ((topController?.presentedViewController) != nil) {
        topController = topController?.presentedViewController
    }
    return topController
}

相关问题