简体   繁体   English

无法在我的 iOS swift 应用程序中添加 Google 原生广告

[英]Not able to add Google native ads in my iOS swift app

I have an iOS app in swift language.我有一个 swift 语言的 iOS 应用程序。 I have included Google AdMob ads in my app.我在我的应用中加入了 Google AdMob 广告。 I have implemented banner ads and interstitial ads but I am not able to generate the Ad ID for Native Ads.我已经实施了横幅广告和插页式广告,但我无法为原生广告生成广告 ID。 I have found an Ad Sense custom search native ads but I don't know for what purpose these ads are used.我发现了一个 Ad Sense 自定义搜索原生广告,但我不知道这些广告的用途是什么。 Can I use AdSense native ads in my mobile app.我可以在我的移动应用中使用 AdSense 原生广告吗? Please suggest me what to do and how to progress?请建议我该怎么做以及如何进步?

Below are the steps that I always follow whenever it comes to adding Google Admob Ads.以下是我在添加 Google Admob 广告时始终遵循的步骤。 Do take note that the example below will implement Google Admob in a table view.请注意,下面的示例将在表格视图中实现 Google Admob。

  1. Install Google Admob Ads via pod pod 'Google-Mobile-Ads-SDK'通过 pod pod 'Google-Mobile-Ads-SDK'安装 Google Admob 广告
  2. In AppDelegate > didFinishLaunchingWithOptions , setup/configure Google Admob GADMobileAds.configure(withApplicationID: Constant.googleAdmobAppID)AppDelegate > didFinishLaunchingWithOptions ,设置/配置 Google Admob GADMobileAds.configure(withApplicationID: Constant.googleAdmobAppID)

  3. Next, create a class for Google Admob Banner.接下来,为 Google Admob Banner 创建一个类。

import Foundation
import GoogleMobileAds

class GoogleAdMobBanner: NSObject, GADBannerViewDelegate {

    unowned var sourceTableViewController: UITableViewController
    var adBannerView: GADBannerView

    init(sourceTableViewController: UITableViewController) {
        self.sourceTableViewController = sourceTableViewController
        adBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

        super.init()

        adBannerView.adUnitID = Constant.googleAdmobBannerID
        adBannerView.delegate = self
        adBannerView.rootViewController = sourceTableViewController
    }

    // MARK:- Google Admob

    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("Banner loaded successfully")

        // Reposition the banner ad to create a slide up effect
        let translateTransform = CGAffineTransform(translationX: 0, y: -bannerView.bounds.size.height)
        bannerView.transform = translateTransform

        UIView.animate(withDuration: 0.5) {
            bannerView.transform = CGAffineTransform.identity
            self.sourceTableViewController.tableView.tableHeaderView = bannerView
        }
    }

    func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
        print("Fail to receive ads")
        print(error)
    }

    func loadAdMob() {
        let request = GADRequest()
        request.testDevices = [kGADSimulatorID]

        adBannerView.load(request)
    }

}
  1. Declare a lazy loaded admob banner in the desired class.在所需的类中声明一个延迟加载的 admob 横幅。
class MyController: UITableViewController {

    lazy var googleAdMobBanner: GoogleAdMobBanner = {
        return GoogleAdMobBanner(sourceTableViewController: self)
    }()

}
  1. Lastly, load the Google Admob in viewDidLoad最后,在viewDidLoad加载 Google Admob
override func viewDidLoad() {
    super.viewDidLoad()

    googleAdMobBanner.loadAdMob()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios:如何在swift导航项的后退按钮上添加谷歌广告? - ios:how to add google ads on back button of navigation item in swift? iOS - 横幅测试的 Google 广告问题未仅在我的应用中显示 - iOS - Google ads issue with banner test not showing in my app only 无法在我的 iOS 应用上显示 Google 插页式广告 - Unable to display Google Interstitial Ads on my iOS App 在本机视图中的谷歌广告中添加按钮是否可以 - Is it okay to add a button in google ads in native view Expo 应用程序因 react-native-google-mobile-ads 崩溃 [在 app.json 中的 react-native-google-mobile-ads 键中找不到 ios_appd_id 键] - Expo app crashes with react-native-google-mobile-ads [ios_appd_id key not found in react-native-google-mobile-ads key in app.json] Xcode 9 Swift 4 AR应用添加广告 - Xcode 9 Swift 4 AR app add ads 如何在我的 iOS 应用中使用 Google Ads 并遵守 App Tracking Transparency - How to Use Google Ads with my iOS App and Comply with App Tracking Transparency 适用于我的分类广告网站的iOS应用 - an iOS app for my classifieds ads website 适用于Swift iOS原生应用的SignalR - SignalR for Swift iOS native app 如何在Swift的tableview中添加AdMob原生广告(使用storyboard) - How to add AdMob Native ads in tableview in Swift (using storyboard)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM