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



我正在尝试在我的 Flutter 插件中展示来自 Swift 原生代码的 Adcolony 广告,这就是我的 swift 代码的样子——

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

我的代码运行良好,但我的视图控制器从不显示插页式广告。谁能帮我,因为我不知道我应该通过哪个视图控制器来展示我的广告。文档说要使用self但我的插件不是视图控制器,因此它不起作用。

试试这个:

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

这段代码对我有用。引用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
}

最新更新