如題,請(qǐng)教一下怎么才能設(shè)置Google Admob的廣告?
1. 在.h中聲明變量 GADBannerView *adBanner_;
2. 導(dǎo)入框架MessageUK.framework, AudioToolbox.framework, systemConfiguration.framework
3. 在viewDidAppear(或者其他地方)加入如下代碼:
CGPoint origin = CGPointMake(0.0, 0.0);
// Use predefined GADAdSize constants to define the GADBannerView.
self.adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin] autorelease];
// Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID
// before compiling.
self.adBanner.adUnitID = kSampleAdUnitID;
self.adBanner.delegate = self;
[self.adBanner setRootViewController:self];
[self.view addSubview:self.adBanner];
[self.adBanner loadRequest: [GADRequest request]];
4. 如果有必要實(shí)現(xiàn)起delegate方法
#pragma mark GADBannerViewDelegate impl
// We've received an ad successfully.
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
NSLog(@"Received ad successfully");
}
- (void)adView:(GADBannerView *)view
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}
5. 在build Setting中,將 Other Linker Flags, add -ObjC to both Debug and Release.
大功告成,運(yùn)行程序就可以在屏幕上方看到admob廣告了