Last active
August 29, 2015 14:11
-
-
Save suwa-yuki/ff63719b29a12a8fae73 to your computer and use it in GitHub Desktop.
AdMobの実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GADBannerView.h" | |
// Google AdMob | |
static NSString *const ADMOB_ID = @"YOUR_ADMOB_ID"; | |
static NSString *const ADMOB_IPAD_ID = @"YOUR_ADMOB_ID"; | |
@interface GADViewController () <GADBannerViewDelegate> | |
@property (weak, nonatomic) IBOutlet GADBannerView *adBannerView; | |
@end | |
@implementation GADViewController | |
#pragma mark - Admob | |
- (void)initAdmob | |
{ | |
// AdMobの初期化 | |
if (self.adBannerView == nil){ | |
GADBannerView *adBannerView = nil; | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
// iPad | |
adBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, | |
self.view.frame.size.height - GAD_SIZE_728x90.height, | |
GAD_SIZE_728x90.width, | |
GAD_SIZE_728x90.height)]; | |
adBannerView.adUnitID = ADMOB_IPAD_ID; | |
} else { | |
// iPhoneなど | |
adBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, | |
self.view.frame.size.height - GAD_SIZE_320x50.height, | |
GAD_SIZE_320x50.width, | |
GAD_SIZE_320x50.height)]; | |
adBannerView.adUnitID = ADMOB_ID; | |
} | |
adBannerView.rootViewController = self; | |
adBannerView.delegate = self; | |
adBannerView.hidden = YES; | |
[self.view addSubview:adBannerView]; | |
self.adBannerView = adBannerView; | |
} | |
GADRequest *request = [GADRequest request]; | |
[self.adBannerView loadRequest:request]; | |
} | |
- (void)adViewDidReceiveAd:(GADBannerView *)adBannerView | |
{ | |
// AdMob表示 | |
adBannerView.hidden = NO; | |
adBannerView.frame = CGRectMake(self.view.frame.size.width / 2 - adBannerView.frame.size.width / 2, | |
self.view.frame.size.height - adBannerView.frame.size.height, | |
adBannerView.frame.size.width, | |
adBannerView.frame.size.height); | |
} | |
- (void)adView:(GADBannerView *)adBannerView didFailToReceiveAdWithError:(GADRequestError *)error | |
{ | |
// AdMobを削除 | |
[adBannerView removeFromSuperview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment