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

3

我试图在Flutter插件的Swift本地代码中展示Adcolony广告,以下是我的swift代码样例 -

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

我的代码正常运行,但是我的视图控制器从来没有显示插页广告。有人能帮我吗?我不知道应该传递哪个视图控制器来显示我的广告。文档说要使用 self,但我的插件不是视图控制器,所以它不起作用。

2个回答

1

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


1

试试这个:

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

我从Swift中得到一个错误,上面写着“无法将类型为'AdColonyInterstitial'的值转换为预期的参数类型'UIViewController'”。 - Arnav
我不熟悉AdColony库,但是你需要有一个ViewController才能展示它。 - Sami Haddad
你可以尝试使用 interstitial.show(withPresenting: vc) 而不是 present - Sami Haddad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接