likes
comments
collection
share

Flutter中显示广点通Banner2广告之IOS端

作者站长头像
站长
· 阅读数 0

1.交代背景

我是个人开发者, 然后我的app需要制作ios端, 我的用户要求出苹果版, 然后我的swift学得渣, 所以只有学学flutter了. 然而很遗憾的是国内没有任何一家广告联盟出了flutter SDK. 所以不得不收集资料搞一波flutter显示原生View.

2.技术交底

搜索了下, 需要使用到Flutter的插件机制, 所以请自行熟悉下UiKitView, MethodChannel, PlatformView.

3.效果图

Flutter中显示广点通Banner2广告之IOS端

4.ios上实现

由于我是用的swift, 广点通的sdk是oc写得, 需要把用到的类写在桥接文件里面

//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"

在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需要使用到controller
    let controller = window?.rootViewController
    
    if !hasPlugin("BannerPlugin") && controller != nil {
        //注册插件
        BannerPlugin.registerWithRegistrar(registar: registrar(forPlugin: "BannerPlugin"), controller: controller!)
    }
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

BannerPlugin.swift

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

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()
    }
}

Banner.swift

import Foundation
class Banner : NSObject, FlutterPlatformView, GDTUnifiedBannerViewDelegate{
    let viewId:Int64
    let args: NSDictionary
    let withFrame:CGRect
    let  controller: UIViewController
    
    init(withFrame: CGRect, viewId: Int64, args: Any?, controller: UIViewController) {
        self.viewId = viewId
        //这是在flutter里面创建view的时候传入的参数
        self.args = args as! 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)
    }
}

在flutter里面调用很简单.

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

主要是UiKitView的使用, banner是IOS那边注册的viewID. 这篇文章是为了记录我在学习flutter添加banner广告的过程. 不喜勿喷, 谢谢 过程很简单, 就没有详细去讲述原理了. 直接上代码, 简单粗暴.