使用ADMob Native SwiftUI插件时,需要从Bundle中加载nib文件。以下是加载nib文件的示例代码:
import SwiftUI
import GoogleMobileAds
import UIKit
struct AdView: UIViewRepresentable {
let adType: GADAdLoaderAdType
let adUnitID: String
func makeUIView(context: UIViewRepresentableContext) -> GADUnifiedNativeAdView {
return GADUnifiedNativeAdView()
}
func updateUIView(_ uiView: GADUnifiedNativeAdView, context: UIViewRepresentableContext) {
let adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: UIApplication.shared.windows.first?.rootViewController, adTypes: [adType], options: nil)
adLoader.delegate = uiView
adLoader.load(GADRequest())
}
static func createAdView(nibNamed nibName: String) -> GADUnifiedNativeAdView? {
guard let nibObjects = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil),
let adView = nibObjects.first as? GADUnifiedNativeAdView else {
return nil
}
return adView
}
}
在该示例代码中,createAdView方法在获取Bundle中的nib文件时进行了调用。在加载nib文件时,我们需要传入nib的名称。这样,我们就可以使用ADMob Native SwiftUI插件并从Bundle中加载nib文件,以便在应用程序中显示广告。
下一篇:ADMob内存占用增加