Flutter ios原生开发添加广点通开屏广告,进入flutter后无法加载插件?
程序员文章站
2022-03-12 17:10:34
...
想要给项目添加开屏广告,但是没有广点通的相关插件,所以只能从原生那里入手。
想法是在进入flutter界面前,添加一个开屏,开屏结束后进入flutter
根据 https://github.com/gdtmobsdk/GDTMobSDK 配置好sdk后
仿照 https://github.com/gdtmobsdk/GDTMobSDK/blob/v4.11.1/GDTMobSample-Swift/AppDelegate.swift 写了自己的AppDelegate.swift 如下
出现的问题是,能够正常显示开屏广告,显示完毕后也能进入flutter,但是flutter所有的第三方插件全部失效,报错是没有找到插件。
AppDelegate.swift
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
var splash: GDTSplashAd!
var flutterViewController: FlutterViewController?
var flutterEngine : FlutterEngine?;
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
self.flutterViewController = FlutterViewController()
window = UIWindow.init(frame: UIScreen.main.bounds)
let rootViewController = GDTNavigationController.init(rootViewController: self.flutterViewController!)
rootViewController.navigationBar.isHidden = true
rootViewController.navigationBar.isTranslucent = true
window?.rootViewController = rootViewController
window.makeKeyAndVisible()
if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone {
splash = GDTSplashAd.init(appId: Constant.appID, placementId: Constant.placementID)
splash.delegate = self as? GDTSplashAdDelegate
var splashImage = UIImage(named: "SplashNormal-swift")
if Util.isIphoneX() {
splashImage = UIImage(named: "SplashX-swift")
} else if Util.isSmallIphone() {
splashImage = UIImage(named: "SplashSmall-swift")
}
splash.backgroundImage = splashImage
splash.fetchDelay = 3
splash.loadAndShow(in: window)
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
为了方便查看贴上图片代码:
p.s flutter正常生成的代码(未改造前的):
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}