程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 SwiftUI 项目中实现 AdMob 原生广告大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 SwiftUI 项目中实现 AdMob 原生广告?

开发过程中遇到在 SwiftUI 项目中实现 AdMob 原生广告的问题如何解决?下面主要结合日常开发的经验,给出你关于在 SwiftUI 项目中实现 AdMob 原生广告的解决方法建议,希望对你解决在 SwiftUI 项目中实现 AdMob 原生广告有所启发或帮助;

我正在尝试将原生广告实施到 SwiftUI 项目中。我正在使用这些类加载广告:

final class NativeMediaVIEw: UIVIEwRepresentable {

var mediaVIEw: GADMediaContent

init(mediaVIEw: GADMediaContent){
    self.mediaVIEw = mediaVIEw
}

typealias UIVIEwType = GADMediaVIEw

func makeUIVIEw(context: Context) -> GADMediaVIEw {
    let vIEw = GADMediaVIEw(frame: .zero)
    return vIEw
}

func updateUIVIEw(_ uiVIEw: GADMediaVIEw,context: Context) {
    uiVIEw.mediaContent = self.mediaVIEw
    uiVIEw.frame(forAlignmentRect: CGRect(origin: .zero,size: CGSize(wIDth: 250,height: 150)))
}
}

final class NativeVIEwController: UIVIEwControllerRepresentable {

let adUnitID: String
@Binding var adStatus: AdStatus

var adLoader: GADAdLoader?

init(adUnitID: String,adStatus: Binding<AdStatus>){
    self.adUnitID = adUnitID
    self._adStatus = adStatus
}

func makeCoordinator() -> Coordinator {
    Coordinator(nativeVIEwController: self)
}

func makeUIVIEwController(context: Context) -> UIVIEwController {
    let vIEwController = UIVIEwController()
    
    let multipleAdsOptions = GADMultipleAdsAdLoaderOptions()
    multipleAdsOptions.numberOfAds = 1
    
    adLoader = GADAdLoader(adUnitID: self.adUnitID,rootVIEwController: vIEwController,adTypes: [GADAdLoaderAdType.native],options: [multipleAdsOptions])
    adLoader?.delegate = context.coordinator
    adLoader?.load(GADrequest())
    
    let testLabel = GADNativeAdVIEw()
    vIEwController.vIEw.addSubvIEw(testLabel)
    
    return vIEwController
}

func imageOfStars(from starraTing: NSdecimalnumber?) -> UIImage? {
    guard let raTing = starraTing?.doubleValue else {
        return nil
    }
    if raTing >= 5 {
        return UIImage(named: "stars_5")
    } else if raTing >= 4.5 {
        return UIImage(named: "stars_4_5")
    } else if raTing >= 4 {
        return UIImage(named: "stars_4")
    } else if raTing >= 3.5 {
        return UIImage(named: "stars_3_5")
    } else {
        return nil
    }
}

func updateUIVIEwController(_ uiVIEwController: UIVIEwController,context: Context) {
    
    print("STA => \(self.adStatus)")
    if self.adStatus == .success {
        let nativeAd = context.coordinator.nativeAd
        
        let nativeAdVIEw = GADNativeAdVIEw()
        (nativeAdVIEw.headlineVIEw as? UILabel)?.text = nativeAd?.headline
        
        (nativeAdVIEw.bodyVIEw as? UILabel)?.text = nativeAd?.body
        nativeAdVIEw.bodyVIEw?.isHIDden = nativeAd?.body == nil
        
        (nativeAdVIEw.callToActionVIEw as? UIbutton)?.settitle(nativeAd?.callToAction,for: .normal)
        nativeAdVIEw.callToActionVIEw?.isHIDden = nativeAd?.callToAction == nil
        
        (nativeAdVIEw.iconVIEw as? UIImageVIEw)?.image = nativeAd?.icon?.image
        nativeAdVIEw.iconVIEw?.isHIDden = nativeAd?.icon == nil
        
        (nativeAdVIEw.advertiserviEw as? UILabel)?.text = nativeAd?.advertiser
        nativeAdVIEw.advertiserviEw?.isHIDden = nativeAd?.advertiser == nil
        nativeAdVIEw.callToActionVIEw?.isUserInteractionEnabled = false
        nativeAdVIEw.nativeAd = nativeAd
        nativeAdVIEw.translatesautoresizingMaskIntoConsTraints = false
        
        uiVIEwController.removeFromParent()
        uiVIEwController.vIEw.addSubvIEw(nativeAdVIEw)

    }
    
}

class Coordinator: NSObject,GADNativeAdLoaderDelegate {
    
    var nativeVIEwController: NativeVIEwController
    var nativeAd: GADNativeAd?
    
    init(nativeVIEwController: NativeVIEwController){
        self.nativeVIEwController = nativeVIEwController
    }
    
    func adLoader(_ adLoader: GADAdLoader,dIDReceive nativeAd: GADNativeAd) {
        self.nativeAd = nativeAd
        self.nativeVIEwController.adStatus = .success
    }
    
    func adLoader(_ adLoader: GADAdLoader,dIDFailToReceiveADWithError error: Error) {
        self.nativeVIEwController.adStatus = .failure
    }
}
}

final class NativeVIEwController2: NSObject,GADNativeAdLoaderDelegate {
@Binding var adStatus: AdStatus

var adLoader: GADAdLoader?
var nativeAd: GADNativeAd?

init(adStatus: Binding<AdStatus> = .constant(AdStatus.loading)){
    self._adStatus = adStatus
}

func loadAd(adStatus: Binding<AdStatus>){
    self._adStatus = adStatus
    
    let options = GADNativeAdMediaAdLoaderOptions()
    options.mediaAspectRatio = .square
    
    let root = UIApplication.shared.windows.first?.rootVIEwController
    
    adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511",rootVIEwController: root!,options: [options])
    adLoader?.delegate = self
    adLoader?.load(GADrequest())
}

func adLoader(_ adLoader: GADAdLoader,dIDReceive nativeAd: GADNativeAd) {
    self.nativeAd = nativeAd
    self.adStatus = .success
}

func adLoader(_ adLoader: GADAdLoader,dIDFailToReceiveADWithError error: Error) {
    self.adStatus = .failure
}

func nativeAds() -> GADNativeAd? {
    self.nativeAd
}

以及显示广告的这些结构:

struct NativeVIEw: VIEw {
@State var adStatus: AdStatus = .loading

var nativeVIEwController = NativeVIEwController2()

var body: some VIEw {
    vstack {
        if adStatus == .success {
            if let nativeAd = nativeVIEwController.nativeAds() {
                NativeAdVIEw(nativeAd: nativeAd)
                    .frame(maxWIDth: 375)
            }
        }
        else {
            ProgressvIEw(value: /*@START_MENU_TOKEN@*/0.5/*@END_MENU_TOKEN@*/)
                .progressvIEwStyle(CircularProgressvIEwStyle())
            
            Text("Loading ad")
                .foregroundcolor(.secondary)
        }
    }
    .onAppear {
        nativeVIEwController.loadAd(adStatus: $adStatus)
    }
}
}

struct NativeAdVIEw: VIEw {

var nativeAd: GADNativeAd

var body: some VIEw {
    vstack {
        HStack {
            Text("Advertisement")
                .Font(.system(size: 12,weight: .semibold,design: .rounded))
                .padding(.horizontal,5)
                .foregroundcolor(.whitE)
                .BACkground(
                    RoundedRectangle(cornerRadius: 5)
                        .fill(color(#colorliteral(red: 0.9607843161,green: 0.7058823705,blue: 0.200000003,Alpha: 1)))
                )
            
        }
        .padding(.horizontal,15)
        .padding(.top,10)
        
        HStack {
            Image(uiImage: (nativeAd.icon?.imagE)!)
                .resizable()
                .scaledToFill()
                .frame(wIDth: 60,height: 60)
                .mask(Circle())
            
            vstack(alignment: .leading) {
                Text(nativeAd.headline!)
                    .FontWeight(.semibold)
                    .fixedSize(horizontal: false,vertical: truE)
                    .Font(.title3)
                    .linelimit(2)
                    .multilineTextAlignment(/*@START_MENU_TOKEN@*/.leading/*@END_MENU_TOKEN@*/)
                    .linespacing(1)
                
                Text(nativead.advertiser ?? "Advert")
                    .Font(.subheadlinE)
            }
            Spacer()
        }
        .padding(.horizontal,15)
        
        HStack {
            Text(nativeAd.body!)
                .multilineTextAlignment(.leading)
            Spacer()
        }
        .padding(.horizontal,15)
        
        HStack(spacing: 15) {
            Spacer()
            Text(nativeAd.price ?? "")
                .multilineTextAlignment(.leading)
            
            HStack {
                ForEach(0..<Int(truncaTing: nativeAd.starraTing ?? 0)) { _ in
                    Image(systemname: "star")
                }
            }
            
            if let cTA = nativeAd.callToAction {
                button(action: {
                    
                },label: {
                    Text(cTA)
                        .FontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
                        .padding(.horizontal,10)
                        .padding(.vertical,5)
                        .BACkground(
                            RoundedRectangle(cornerRadius: 10)
                                .fill(color(.systemBluE))
                        )
                        .foregroundcolor(.whitE)
                })
            }
        }
        .padding([.horizontal,.bottom])
        
    }.frame(maxWIDth: 375)
    .BACkground(
        RoundedRectangle(cornerRadius: /*@START_MENU_TOKEN@*/25.0/*@END_MENU_TOKEN@*/)
            .fill((color.whitE))
            .shadow(color: color(#colorliteral(red: 0.8039215803,green: 0.8039215803,blue: 0.8039215803,Alpha: 0.76)),radius: 5,x: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/,y: 0)
    )
    .padding()
}
}

我的问题有两个:首先,为什么adchoice按钮显示不正确?它应该显示为自动,但它不存在。那么,如何管理标注按钮?

我从这里得到了代码,但我编辑了它。

展示的广告:https://i.stack.imgur.com/WXU35.png

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的在 SwiftUI 项目中实现 AdMob 原生广告全部内容,希望文章能够帮你解决在 SwiftUI 项目中实现 AdMob 原生广告所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。