【问题标题】:Admob Native Ads in SwiftUISwiftUI 中的 Admob 原生广告
【发布时间】:2020-12-17 21:38:52
【问题描述】:

让 Admob Native Ads 与 SwiftUI 一起工作的运气不太好。我尝试使用此处找到的代码。 https://github.com/googleads/googleads-mobile-ios-examples/blob/master/Swift/admob/NativeAdvancedExample/NativeAdvancedExample/ViewController.swift 这是我最终得到的确切代码,因为该链接中的大部分代码都不起作用。

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {
    
    /// The view that holds the native ad.
    @IBOutlet var nativeAdPlaceholder: UIView!
    
    /// The height constraint applied to the ad view, where necessary.
    var heightConstraint: NSLayoutConstraint?
    
    /// The ad loader. You must keep a strong reference to the GADAdLoader during the ad loading
    /// process.
    var adLoader: GADAdLoader!
    
    /// The native ad view that is being presented.
    var nativeAdView: GADUnifiedNativeAdView!
    
    /// The ad unit ID.
    let adUnitID = "ca-app-pub-3940256099942544/3986624511"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard
            let nibObjects = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil),
            let adView = nibObjects.first as? GADUnifiedNativeAdView
            else {
                assert(false, "Could not load nib file for adView")
        }
        setAdView(adView)
        refreshAd(nil)
    }
    
    func setAdView(_ adView: GADUnifiedNativeAdView) {
        // Remove the previous ad view.
        nativeAdView = adView
        nativeAdPlaceholder = UIView()
        nativeAdPlaceholder.addSubview(nativeAdView)
        nativeAdView.translatesAutoresizingMaskIntoConstraints = false
        
        // Layout constraints for positioning the native ad view to stretch the entire width and height
        // of the nativeAdPlaceholder.
        let viewDictionary = ["_nativeAdView": nativeAdView!]
        nativeAdPlaceholder.addConstraints(
          NSLayoutConstraint.constraints(
            withVisualFormat: "H:|[_nativeAdView]|",
            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
        )
        nativeAdPlaceholder.addConstraints(
          NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[_nativeAdView]|",
            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
        )
        
    }
    
    // MARK: - Actions
    
    /// Refreshes the native ad.
    @IBAction func refreshAd(_ sender: AnyObject!) {
        adLoader = GADAdLoader(
            adUnitID: adUnitID, rootViewController: self,
            adTypes: [.unifiedNative], options: nil)
        adLoader.delegate = self
        adLoader.load(GADRequest())
    }
    
}


extension ViewController: GADUnifiedNativeAdLoaderDelegate {
    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        print("fail")
    }
    
    
    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
        
        // Set ourselves as the native ad delegate to be notified of native ad events.
        nativeAd.delegate = self
        
        // Deactivate the height constraint that was set when the previous video ad loaded.
        heightConstraint?.isActive = false
        
        // Populate the native ad view with the native ad assets.
        // The headline and mediaContent are guaranteed to be present in every native ad.
        (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
        nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
        
        
        // This app uses a fixed width for the GADMediaView and changes its height to match the aspect
        // ratio of the media it displays.
        if let mediaView = nativeAdView.mediaView, nativeAd.mediaContent.aspectRatio > 0 {
            heightConstraint = NSLayoutConstraint(
                item: mediaView,
                attribute: .height,
                relatedBy: .equal,
                toItem: mediaView,
                attribute: .width,
                multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
                constant: 0)
            heightConstraint?.isActive = true
        }
        
        // These assets are not guaranteed to be present. Check that they are before
        // showing or hiding them.
        (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.starRatingView?.isHidden = true
        
        (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
        nativeAdView.storeView?.isHidden = nativeAd.store == nil
        
        (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
        nativeAdView.priceView?.isHidden = nativeAd.price == nil
        
        (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
        nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
        
        // In order for the SDK to process touch events properly, user interaction should be disabled.
        nativeAdView.callToActionView?.isUserInteractionEnabled = false
        
        // Associate the native ad view with the native ad object. This is
        // required to make the ad clickable.
        // Note: this should always be done after populating the ad views.
        nativeAdView.nativeAd = nativeAd
        
    }
}

// MARK: - GADUnifiedNativeAdDelegate implementation
extension ViewController: GADUnifiedNativeAdDelegate {
    
    func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
    
    func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
    
    func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
    
    func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
    
    func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
    
    func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
}

我做了这个结构。

import SwiftUI
import UIKit

struct NativeViewController: UIViewControllerRepresentable {

func makeUIViewController(context: Context) -> UIViewController {
    let picker = ViewController()
    return picker
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    
}

}

但是当我像这样将 taht NativeViewController 放在正文中时

var body: some View {
    NativeViewController()
}

我没有收到任何错误,但也没有显示广告。

任何方向都将不胜感激。

【问题讨论】:

    标签: ios swift swiftui admob


    【解决方案1】:

    这个问题的答案是使用 self.view 而不是占位符代码

        // Remove the previous ad view.
        nativeAdView = adView
        self.view.addSubview(nativeAdView)
        nativeAdView.translatesAutoresizingMaskIntoConstraints = false
        
        // Layout constraints for positioning the native ad view to stretch the entire width and height
        // of the nativeAdPlaceholder.
        let viewDictionary = ["_nativeAdView": nativeAdView!]
        self.view.addConstraints(
          NSLayoutConstraint.constraints(
            withVisualFormat: "H:|[_nativeAdView]|",
            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
        )
        self.view.addConstraints(
          NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[_nativeAdView]|",
            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
        )
    

    【讨论】:

    • 您好,我遇到了类似的广告未显示的问题(使用测试原生广告)。涉及 SwiftUI 的文档/示例并不多。你是怎么让它出现的?我尝试完全使用您的代码,除了一个更改,因为我不使用情节提要,我只是做了: let nibView = GADUnifiedNativeAdView() setAdView(nibView) refreshAd(nil) 我看到右上角的小谷歌广告徽标,但什么也没有出现。我在 Xcode 控制台中看到:调用了 nativeAdDidRecordImpression(_:),所以看起来代码正在运行,但广告没有出现。有什么想法吗?
    • @user3129120 如果您不使用 Google 示例中的情节提要,则必须创建自己的 UIView 类来扩展 GADUnifiedNativeAdView。如果您只是简单地使用 let nibView = GADUnifiedNativeAdView() 而没有故事板集成,那么像 nativeAdView.bodyView 这样的属性应该为零。
    • @user3129120 你解决了吗?我有完全相同的问题...
    • 嘿,我没有真正解决它。但我意识到我不需要原生广告。我只需要 SwiftUI 横幅广告,而且效果很好。有那些准备使用的例子。比如这样:medium.com/@michaelbarneyjr/…
    • @user3129120 谢谢,如果您或其他任何人仍然对如何使原生广告有效,特别是 NIB 部分感兴趣,我今天找到了这个工作示例:github.com/sakurabird/iOS-Admob-SwiftUI-example/
    【解决方案2】:

    最好的方法是创建一个委托类,其实例位于将显示 AD 的视图中。

    创建一个委托(这里是虚拟测试广告):

    let interstitialID:String = "/6499/example/interstitial"
    
    final class InterstitialDelegate: NSObject, GADInterstitialDelegate {
        
        var interstitial: DFPInterstitial!
        
        override init() {
            super.init()
            interstitial = createAndLoadInterstitial()
        }
        
        func createAndLoadInterstitial() -> DFPInterstitial {
            interstitial = DFPInterstitial(adUnitID: interstitialID)
            interstitial.delegate = self
            interstitial.load(DFPRequest())
            return interstitial
        }
        
        func showAd() {
            print("Request to show AD")
            if self.interstitial.isReady {
                let root = UIApplication.shared.windows.first?.rootViewController
                self.interstitial.present(fromRootViewController: root!)
                print("AD presented")
            } else {
                print("AD not ready")
            }
        }
        
        private func interstitialDidDismissScreen(_ ad: DFPInterstitial) {
            interstitial = createAndLoadInterstitial()
        }
    }
    

    然后在应该显示广告的视图中创建上述委托的实例:

    struct MainView: View {
            
        var interstitial : InterstitialDelegate
    
        init() {
            interstitial = InterstitialDelegate()
        }
    
    }
    

    然后在此视图中通过调用请求广告

    interstitial.showAd() 
    

    例如在用户听完音乐之后。

    贷方:https://medium.com/@michaelbarneyjr/how-to-integrate-admob-ads-in-swiftui-fbfd3d774c50@pawello2222 的回答中也提到了他,然后我用来自Google's Mobile Ads SDK 的信息更新了它。

    【讨论】:

    • OP 要求提供原生广告!
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2018-07-09
    • 2020-07-11
    • 2016-10-29
    • 2021-04-24
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多