【发布时间】:2020-06-24 15:00:02
【问题描述】:
我正在尝试完成(看似)简单的任务,将原生 AdMob 广告集成到我在 Swift 上运行的 iOS 应用中。让我首先向您展示我的故事板和集成代码,然后我们将继续讨论我尝试解决的问题。
设置:故事板和代码
在我的 App Delegate 中,我将 Firebase 配置为 Google Mobile Ads...
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-8482280186158418~7106629637")
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
}
我还将我的GADApplicationIdentifier 添加到我的 Info.plist。在我的故事板中,我有一个ViewController,其中包含一个UICollectionViewCell,其中包含一个GADUnifiedNativeAdView,并且本机元素通过插座链接。
在我的 ViewController 中,在 viewDidLoad 中,我加载了 5 个原生广告。
override func viewDidLoad() {
super.viewDidLoad()
// Load Ads
let adUnitID = "ca-app-pub-{adUnitId}"
let numAdsToLoad = 5
let options = GADMultipleAdsAdLoaderOptions()
options.numberOfAds = numAdsToLoad
let imageOptions = GADNativeAdImageAdLoaderOptions()
imageOptions.disableImageLoading = false
let mediaOptions = GADNativeAdMediaAdLoaderOptions()
mediaOptions.mediaAspectRatio = .square
let adOptions = GADNativeAdViewAdOptions()
adOptions.preferredAdChoicesPosition = .topLeftCorner
adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.unifiedNative], options: [options, imageOptions, mediaOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
在GADUnifiedNativeAdLoaderDelegate 中,我收到了广告并将它们添加到一个数组中。
var nativeAds = [GADUnifiedNativeAd]()
extension ViewController: GADUnifiedNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader,
didFailToReceiveAdWithError error: GADRequestError) {
print("NATIVE AD - didFailToReceiveAdWithError: \(error)")
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
print("NATIVE AD - didReceive: \(nativeAd)")
nativeAds.append(nativeAd)
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
print("NATIVE AD - adLoaderDidFinishLoading")
libraryCollection.reloadData()
}
}
在我的UICollectionView's 代表中,在cellForItemAt 中,我设置了单元格的类
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isAdCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! AdCollectionViewCell
if nativeAds.count == 5 {
cell.nativeAd = nativeAds[0]
cell.nativeAd.rootViewController = self
}
cell.setAdData()
return cell
}
最后,在我的UICollectionViewCell's 类中,我设置了广告数据
import Firebase
class AdCollectionViewCell: UICollectionViewCell {
@IBOutlet var adView: GADUnifiedNativeAdView
var nativeAd = GADUnifiedNativeAd()
func setAdData() {
adView.nativeAd = nativeAd
(adView.headlineView as! UILabel).text = nativeAd.headline
adView.callToActionView?.isUserInteractionEnabled = false
(adView.callToActionView as! UILabel).text = nativeAd.callToAction
adView.mediaView?.mediaContent = nativeAd.mediaContent
adView.mediaView?.contentMode = .scaleAspectFill
}
}
错误
现在,每当我使用 Google AdMob 提供的测试原生广告单元 ID ca-app-pub-3940256099942544/3986624511 时,一切正常!我没有收到任何错误,系统会打印
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbdc00>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbd7a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bc73a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9880>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9810>
NATIVE AD - adLoaderDidFinishLoading
但是当我尝试使用自己的原生广告单元 ID 时,问题就来了。加载时,系统打印错误
NATIVE AD - didFailToReceiveAdWithError: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info= ** Response Info **
Response ID: ymbzXou2CcrohQb16bzgBA
Network: (null)
** Mediation line items **
Entry (1)
Network: GADMAdapterGoogleAdMobAds
Credentials:
{
}
Error: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
Latency: 0.095
}
对于我加载的每个广告。我已经尝试了很多方法来解决我在互联网上找到的这个问题......
疑难解答
正如我所提到的,我已经在互联网上搜索了帮助和故障排除选项。这是我所做的。
- 创建了新的原生广告单元 ID
- 等了一个多星期才确认单位 ID 的错误
- 检查了我的 AdMob 和 AdSense 帐户信息
- 检查了我的 AdMob 和 AdSense 付款信息
- 确保 Info.plist 中的 GADApplicationIdentifier 正确
- 将所有要求添加到 Info.plist 中的
App Transport Security Settings为 laid out by AdMob。 (允许任意加载、允许任意加载媒体、允许任意加载 Web 内容) - 双重、三重、四重、五重检查代码并遵循 AdMob 提供的documentation
- 已关注 AdMob 提供的CodeLab
【问题讨论】:
-
你解决了吗?
-
你好,我也遇到同样的问题,你解决了吗?
-
@Daniel 你解决了吗?
-
@RohitKanade 不,仍然在 Admob 收到请求,但对我的应用没有印象:/
标签: swift firebase uicollectionview admob