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

1.交代背景

我是我的開發者, 而後個人app須要製做ios端, 個人用戶要求出蘋果版, 而後個人swift學得渣, 因此只有學學flutter了. 然而很遺憾的是國內沒有任何一家廣告聯盟出了flutter SDK. 因此不得不收集資料搞一波flutter顯示原生View.ios

2.技術交底

搜索了下, 須要使用到Flutter的插件機制, 因此請自行熟悉下UiKitView, MethodChannel, PlatformView.swift

3.效果圖

4.ios上實現

因爲我是用的swift, 廣點通的sdk是oc寫得, 須要把用到的類寫在橋接文件裏面bash

//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中使用app

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.swiftide

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.swiftui

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裏面調用很簡單.spa

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

相關文章
相關標籤/搜索