Flutter中顯示廣點通Banner2廣告之IOS端

純潔的壞蛋發表於2019-05-21

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廣告的過程. 不喜勿噴, 謝謝 過程很簡單, 就沒有詳細去講述原理了. 直接上程式碼, 簡單粗暴.

相關文章