iOS SwiftUI AdMob Native ads convenience class [closed]

我的未来我决定 提交于 2020-02-25 06:57:16

问题


Continue to iOS dev publishing same clustering convenience class as before for Android Android AdMob Native ads convenience class:

let adUnitID = "ca-app-pub-blah"

final class AdmobData: NSObject, ObservableObject, GADAdLoaderDelegate, GADUnifiedNativeAdLoaderDelegate {
    let loadSize = 3
    let minSize = 2
    var adLoader: GADAdLoader? = nil

    @Published var adsMapping: [Int: GADUnifiedNativeAd] = [:]
    var adsBacklog = Set<Int>()
    var adsCache: [GADUnifiedNativeAd] = []

    func flush() {
        for (_, nativeAd) in adsMapping {
            adsCache.append(nativeAd)
        }
        adsMapping.removeAll()
    }

    func setAd(hash: Int) {
        if adsMapping[hash] == nil {
            if (adsCache.count < minSize || adsBacklog.count > 0) {
                load();
            }
            if adsCache.isEmpty {
                // If no ads in cache wait for
                // it and put task in backlog
                adsBacklog.insert(hash);
            } else {
                // otherwise get ad from cache
                adsMapping[hash] = adsCache.popLast()
            }
        }
    }

    func load() {
        if adLoader == nil {
            let multipleAdsOptions = GADMultipleAdsAdLoaderOptions()
            multipleAdsOptions.numberOfAds = loadSize
            adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: nil,
                                       adTypes: [ .unifiedNative ], options: [multipleAdsOptions])
        }
        adLoader?.delegate = self
//        DispatchQueue.global(qos: .userInitiated).async { [weak self] in
        if !(adLoader?.isLoading ?? false) {
            adLoader?.load(GADRequest())
        }
//        }
    }

    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
//        print("adLoader")
        if adsBacklog.isEmpty {
            adsCache.append(nativeAd);
        } else {
            // POP
            let hash = adsBacklog.popFirst()
            if hash != nil {
                self.adsMapping[hash!] = nativeAd
            }
            /*
            if (!adLoader.isLoading()) {
                load();
            }
            */
        }
    }

    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        print("\(adLoader) failed with error: \(error.localizedDescription)")
    }
}

struct AdmobNativeAd: View {
    @EnvironmentObject var admobData: AdmobData
    let id: Int

    var body: some View {
        HStack {
            if self.admobData.adsMapping[id]?.icon?.image != nil {
                Image(uiImage: (self.admobData.adsMapping[id]?.icon?.image!)!)
                    .aspectRatio(contentMode: .fit)
                    .frame(maxHeight: 80)
                    .cornerRadius(10)
            }
            Text(self.admobData.adsMapping[id]?.headline ?? "")
        }
        .frame(minHeight: 100)
        .onAppear() {
            self.admobData.setAd(hash: self.id)
        }
    }
}

struct AdmobNativeAd_Previews: PreviewProvider {
    static var previews: some View {
        AdmobNativeAd(id: 0)
    }
}

What people think about it? Is it useful or better to use google's example? Used it in anonymous chat app https://apps.apple.com/us/app/lonje-anonymous-chat/id1215525783?ls=1 and it looks natural versus google native example.

来源:https://stackoverflow.com/questions/60200602/ios-swiftui-admob-native-ads-convenience-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!