使用 Firebase 实施 AdMob 的问题 iOS

Issues implementing AdMob with Firebase iOS

因此,自从 iAD 将关闭的公告发布后,我就开始寻找一种替代方法来使用广告通过我的应用获利。随着 Google 5 月 18 日对 Firebase 的更新(将 AdMob 与其集成非常有吸引力),我决定在我的应用程序中添加一个简单的横幅广告。我遵循了文档中的所有说明 (https://firebase.google.com/docs/admob/ios/quick-start#load_an_ad_into_gadbannerview),但不幸的是我遇到了一些严重错误。

这是文档中的代码,它与我的应用程序代码几乎相同:

import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

  @IBOutlet weak var bannerView: GADBannerView!

override func viewDidLoad() {
  super.viewDidLoad()

  bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
  bannerView.rootViewController = self
  bannerView.loadRequest(GADRequest())
}

} 

@IBOutlet weak var bannerView: GADBannerView! 我收到错误:weak may only be applied to class and class-bound protocol types, not '<<error type>> 这是我一生中从未见过的错误!

我也收到 Use of unresolved identifier 'GADRequest,我不明白,因为这是 GADBannerView 的内置方法...?

我使用 cocoapods 安装了所有必要的库,所以这应该不是问题。任何帮助,将不胜感激。谢谢!

我解决了将 cocoapods 更新到最新版本,再次安装所有 pods 并清理项目

import Firebase

在ViewController

你的cocoapods是最新版本吗?

在将 firebase 添加到我已经实施了 GoogleMobileAds 的项目后,旧版本的 cocoapods 也遇到了类似的问题。

导入 Firebase 后出现 3 个构建错误:

"Use of unresolved identifier GADRequest"

"Use of unresolved identifier GADBannerViewDelegate"

"Use of unresolved identifier GADBannerView"

我评论了 import Firebase,那些错误消失了。

我决定更新我的 cocoapods 并删除我的旧 podfile。

然后使用pod init创建一个新的podfile并添加依赖。 当然再装pods

这对我有用。

不确定它是否适用于您的情况,但希望对您有所帮助。

我可能会迟到,但在 GoogleMobileAds 旁边。我们必须导入 Firebase。不知道原因,但这解决了我的问题。我 运行 在不同的场合遇到过同样的问题。

当您使用Pod安装时, 不要忘记取消标记 'use_framework!'

我用 'use_framework' 重新安装了 pods ,成功了! 当您尝试使用 GADBannerview 时,您不需要导入 Firebase

您不需要导入 Firebase

我用这种方法解决了所有问题,如不可预测的错误、编译错误和导入错误。 我在 Swift3 中使用 Xcode8.x。

  1. 如果您在 iOS 项目中安装了 cocoapod,请卸载。 (删除所有 pod 文件夹、pod 框架和 pod 制作的工作区)。无所谓Podfile.

  2. 编辑Podfile如下

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'

target 'YourTargetName' do
    use_frameworks!
    pod 'Firebase/Core'
    pod 'Firebase/AdMob'
end
  1. 在终端中,输入pod update

  2. pod install

  3. 通过 yourproject.workspace 打开项目 cocoapod

导入 GoogleMobileAds 并做任何你想做的事。

呜呜呜!

希望对大家有所帮助。 您可以通过 google here.

下载示例

解决方案非常简单,只需同时导入 Firebase 和 Google Mobile Ads 即可:

import Firebase
import GoogleMobileAds

Firebase/Google广告,Swift,Xcode,Cocoapods不断更新。这些年来,我一直在改变如何声明 pods 和导入它们。

现在(2018年),你的Podfile应该这样写:

pod 'Firebase/AdMob'

就是这样。它在子规范 "AdMob" 下合并到 Firebase 的一个大家族下。您 运行 一个 pod install 并且您会意识到对 pod "Google-Mobile-Ads-SDK" 有依赖性(但您不应该将 pod 添加到 Podfile)。

您需要 import Firebase,并且项目应该解决 GADRequest 等等

它将起作用,直到其中之一再次改变集成方式。