0

我尝试了多个教程并在多个地方尝试了代码,但无法让 google admob 做任何事情。我没有收到任何错误,项目在模拟器中构建和运行。没有广告加载,调试器中也没有消息。基本上什么都没有发生。

我在 appDelegate 中添加了以下内容

#import <GoogleMobileAds/GoogleMobileAds.h>
#import <GoogleMobileAds/GADBannerView.h>
#import <GoogleMobileAds/GADBannerViewDelegate.h>
#import "Firebase.h"

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions我放

[FIRApp configure];
[GADMobileAds configureWithApplicationID:@"ca-app-pub-5601547953982889~9365662253"];

在我的viewcontroller.h我还添加了

#import <GoogleMobileAds/GoogleMobileAds.h>
#import <GoogleMobileAds/GADBannerView.h>
#import <GoogleMobileAds/GADBannerViewDelegate.h>
#import "Firebase.h"

添加

@interface BT_viewController : UIViewController <GADBannerViewDelegate,

@property (nonatomic, weak) IBOutlet GADBannerView  *bannerView;

@protocol GADBannerViewDelegate <NSObject>
@optional
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
- (void)adView:(GADBannerView *)bannerView
didFailToReceiveAdWithError:(GADRequestError *)error;
- (void)adViewWillPresentScreen:(GADBannerView *)bannerView;
- (void)adViewDidDismissScreen:(GADBannerView *)bannerView;
- (void)adViewWillDismissScreen:(GADBannerView *)bannerView;
- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView;

@end

然后在我的视图中添加了controller.m

- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
bannerView.hidden = NO;
    [BT_debugger showIt:self theMessage:@"Google ad shown"];
}
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:    (GADRequestError *)error {
NSLog(@"adView:didFailToReceiveAdWithError: %@", error.localizedDescription);
}

然后在-(void)viewDidLoad我添加了

self.bannerView = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
                                            self.view.frame.size.height -
                                            GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];
self.bannerView.delegate = self;
self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";

self.bannerView.rootViewController = self;
[self.view addSubview:self.bannerView];

[self.bannerView loadRequest:[GADRequest request]];

很确定我把这一切都涵盖了,但绝对没有任何效果。没有错误,没有调试,压缩...

请帮忙!

4

1 回答 1

0

所以我决定尝试使用 Cocoa Pods 进行安装,结果仍然相同。然后我在 GADbannerview 文件中发现它有一个加载广告的示例代码。我尝试了这段代码,现在显示了广告!

 GADBannerView *adView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
adView.rootViewController = self;
adView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
///   // Place the ad view onto the screen.
[self.view addSubview:adView];
///   // Request an ad without any additional targeting information.
[adView loadRequest:[GADRequest request]];
于 2016-12-28T22:08:57.217 回答