Admob ( GoogleMobileAds 8.0.0 ) iOS SDK - GADInterstitial API 未找到,请问如何使用 GADInterstitialAd - 示例代码?
Admob ( GoogleMobileAds 8.0.0 ) iOS SDK - GADInterstitial API not found, How to use GADInterstitialAd - sample code please?
下一行没有错误
#import <GoogleMobileAds/GoogleMobileAds.h>
但是 Admob API 的 none 检测到......它给所有 admob API 错误。检测到另一个 SDK(Applovin) API。
这是截图。如何修复 Admob/GoogleMobileAds ?
吊舱文件:
AdMob 刚刚将主要版本更新到 8.0.0,并进行了多项 API 更改。
两者都
- 使用
pod 'Google-Mobile-Ads-SDK', '~> 7.69'
将广告连播锁定到 7.x
- 执行 https://developers.google.com/admob/ios/migration
中记录的 8.x 迁移
GoogleMobileAds 8.0.0 iOS GADInterstitialAd 全屏广告代码:
// 在 .h 文件中
#import <GoogleMobileAds/GoogleMobileAds.h>
@interface AppController : NSObject <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADInterstitialAd *interstitial;
// 在 .m 文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[GADMobileAds sharedInstance] startWithCompletionHandler:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self loadAdmob_Ads];
}
-(void)loadAdmob_Ads
{
GADRequest *request = [GADRequest request];
[GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-Your_Interstitial_Ad_Unit_ID"
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error)
{
if (error)
{
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob Failed to load interstitial ad with error: %@", [error localizedDescription]);
#endif
return;
}
self.interstitial = ad;
self.interstitial.fullScreenContentDelegate = self;
}];
}
// 每当您想显示全屏广告时调用 showAdmobAdsFullScreen
-(void)showAdmobAdsFullScreen
{
if (self.interstitial) {
[self.interstitial presentFromRootViewController:self.viewController];
}
else
{
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob Ad wasn't ready\n");
#endif
}
}
// admob 代表
- (void)adDidPresentFullScreenContent:(id)ad {
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob ad did present full screen content.\n");
#endif
}
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
#ifdef COCOS2D_DEBUG
NSLog(@"Admob Ad failed to present full screen content with error %@.", [error localizedDescription]);
#endif
}
- (void)adDidDismissFullScreenContent:(id)ad {
[self loadAdmob_Ads];
#ifdef COCOS2D_DEBUG
NSLog(@"Admob Ad did dismiss full screen content.");
#endif
}
在 GoogleMobileAds 8.0 (Admob iOS) 中使用 GADInterstitialAd 的示例
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADFullScreenContentDelegate {
var ad: GADInterstitialAd!
override func viewDidLoad() {
super.viewDidLoad()
loadAd()
}
func loadAd() {
let id = "ca-app-pub-3940256099942544/4411468910"
GADInterstitialAd.load(withAdUnitID: id, request: GADRequest()) { ad, error in
if error != nil { return }
self.ad = ad
self.ad.fullScreenContentDelegate = self
self.ad.present(fromRootViewController: self)
}
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("present-ads")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("dismiss-ads")
}
}
如果您没有在您的导入列表中看到以下警告:
No such module GoogleMobileAds
...但是您 ARE 在代码的其他地方看到以下警告:
Cannot find type GADInterstitialAd
in scope
...检查您是否混淆了 Google-Mobile-Ads-SDK
SDK v7 和 v8 的实现。
- v7 使用
GADInterstitial
- v8 使用
GADInterstitialAd
https://developers.google.com/admob/ios/migration
默认情况下,即使您没有在 Podfile 中指定版本,您也可能会发现下载的是 v7;然而 Google 的文档为您提供了实施 v8.
的说明
下一行没有错误
#import <GoogleMobileAds/GoogleMobileAds.h>
但是 Admob API 的 none 检测到......它给所有 admob API 错误。检测到另一个 SDK(Applovin) API。
这是截图。如何修复 Admob/GoogleMobileAds ?
AdMob 刚刚将主要版本更新到 8.0.0,并进行了多项 API 更改。
两者都
- 使用
pod 'Google-Mobile-Ads-SDK', '~> 7.69'
将广告连播锁定到 7.x
- 执行 https://developers.google.com/admob/ios/migration 中记录的 8.x 迁移
GoogleMobileAds 8.0.0 iOS GADInterstitialAd 全屏广告代码:
// 在 .h 文件中
#import <GoogleMobileAds/GoogleMobileAds.h>
@interface AppController : NSObject <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADInterstitialAd *interstitial;
// 在 .m 文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[GADMobileAds sharedInstance] startWithCompletionHandler:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self loadAdmob_Ads];
}
-(void)loadAdmob_Ads
{
GADRequest *request = [GADRequest request];
[GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-Your_Interstitial_Ad_Unit_ID"
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error)
{
if (error)
{
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob Failed to load interstitial ad with error: %@", [error localizedDescription]);
#endif
return;
}
self.interstitial = ad;
self.interstitial.fullScreenContentDelegate = self;
}];
}
// 每当您想显示全屏广告时调用 showAdmobAdsFullScreen
-(void)showAdmobAdsFullScreen
{
if (self.interstitial) {
[self.interstitial presentFromRootViewController:self.viewController];
}
else
{
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob Ad wasn't ready\n");
#endif
}
}
// admob 代表
- (void)adDidPresentFullScreenContent:(id)ad {
#ifdef COCOS2D_DEBUG
NSLog(@"\nAdmob ad did present full screen content.\n");
#endif
}
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
#ifdef COCOS2D_DEBUG
NSLog(@"Admob Ad failed to present full screen content with error %@.", [error localizedDescription]);
#endif
}
- (void)adDidDismissFullScreenContent:(id)ad {
[self loadAdmob_Ads];
#ifdef COCOS2D_DEBUG
NSLog(@"Admob Ad did dismiss full screen content.");
#endif
}
在 GoogleMobileAds 8.0 (Admob iOS) 中使用 GADInterstitialAd 的示例
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADFullScreenContentDelegate {
var ad: GADInterstitialAd!
override func viewDidLoad() {
super.viewDidLoad()
loadAd()
}
func loadAd() {
let id = "ca-app-pub-3940256099942544/4411468910"
GADInterstitialAd.load(withAdUnitID: id, request: GADRequest()) { ad, error in
if error != nil { return }
self.ad = ad
self.ad.fullScreenContentDelegate = self
self.ad.present(fromRootViewController: self)
}
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("present-ads")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("dismiss-ads")
}
}
如果您没有在您的导入列表中看到以下警告:
No such module
GoogleMobileAds
...但是您 ARE 在代码的其他地方看到以下警告:
Cannot find type
GADInterstitialAd
in scope
...检查您是否混淆了 Google-Mobile-Ads-SDK
SDK v7 和 v8 的实现。
- v7 使用
GADInterstitial
- v8 使用
GADInterstitialAd
https://developers.google.com/admob/ios/migration
默认情况下,即使您没有在 Podfile 中指定版本,您也可能会发现下载的是 v7;然而 Google 的文档为您提供了实施 v8.
的说明