1. Explain the background

I am a solo developer, and my app needs to be made for ios, my users want to make it for Apple, and my Swift sucks, so I have to learn Flutter. However, it is a pity that no local advertising alliance has launched flutter SDK. So had to collect data to make a wave of Flutter display native View.

2. Technical disclosure

You need to use the Flutter plugin mechanism, so familiarize yourself with UiKitView, MethodChannel, and PlatformView.

3. The renderings

4. On the ios implementation

Since I am using Swift, the GENERAL SDK is written in OC, so I need to write the classes used in the bridge file

//Runner-Bridging-Header.h
#import "GeneratedPluginRegistrant.h"
#import "GDTNativeExpressAdView.h"
#import "GDTMobBannerView.h"
#import "GDTMobInterstitial.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeAd.h"
#import "GDTSplashAd.h"
#import "GDTSDKConfig.h"
#import "GDTUnifiedBannerView.h"

Copy the code

Used in appdelegate.swift

import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate{ override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) - > Bool {GeneratedPluginRegistrant. Register (with: self) / / Banner need to use to the controllerletcontroller = window? .rootViewControllerif! hasPlugin("BannerPlugin") && controller ! . = nil {/ / registered plug-in BannerPlugin registerWithRegistrar (registar: the registrar (forPlugin: "BannerPlugin"), controller: controller!)
    }
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
Copy the code

BannerPlugin.swift

import Foundation
class BannerPlugin {
    static func registerWithRegistrar(registar: FlutterPluginRegistrar, controller: UIViewController){
        registar.register(BannerViewFactory(controller: controller), withId: "banner"); }}Copy the code

BannerViewFactory.swift

import Foundation
class BannerViewFactory : NSObject, FlutterPlatformViewFactory {
    let controller: UIViewController
    
    init(controller: UIViewController) {
        self.controller = controller
    }
    
    public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
        return Banner(withFrame:frame, viewId: viewId, args: args, controller: controller)
    }
    
    func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
        return FlutterStandardMessageCodec.sharedInstance()
    }
}
Copy the code

Banner.swift

import Foundation
class Banner : NSObject, FlutterPlatformView, GDTUnifiedBannerViewDelegate{
    let viewId:Int64
    let args: NSDictionary
    let withFrame:CGRect
    letcontroller: UIViewController init(withFrame: CGRect, viewId: Int64, args: Any? , controller: UIViewController) {self.viewId = viewId // This is the argument that is passed when a view is created inside a flutter. NSDictionary self.withFrame = withFrame self.controller = controller } public func view() -> UIView {let banner = GDTUnifiedBannerView.init(frame: withFrame,
            appId: args.object(forKey: "appid") as! String,
            placementId: args.object(forKey: "posId") as! String,
            viewController: controller)
        banner.delegate = self
        banner.loadAdAndShow()
        return banner;
    }
    
    func unifiedBannerViewFailed(toLoad unifiedBannerView: GDTUnifiedBannerView, error: Error) {
        print(error)
    }
}

Copy the code

Calling from flutter is simple.

 UiKitView(
                                  viewType: "banner",
                                  creationParams: <String, dynamic>{"appid": "1105344611"."posId": "1080958885885321"},
                                  creationParamsCodec: const StandardMessageCodec(),
                                  onPlatformViewCreated: (id) {
                                    print(id);
                                  },
                                ),
                          height: 64,
Copy the code

It’s basically UiKitView, the banner is the viewID registered on IOS. This article is to record my process of learning flutter to add banner ads. Don’t spray if you don’t like it. Thank you. The process is very simple. Go straight to the code, simple and crude.