adMob iAd整合,随机根据网络状况自动显示。

时间:2021-11-28 20:06:09

最近找整合的代码,找到的都不对,有个大概对的,但要奔溃退出,只要两个单独弄。

adMob 下载好sdk,导入进去,iAd的加入iad framework.

使用方法,在viewController viewDidAppear内添加

IADView* adView = [IADView sharedInstance];
[adView createAdBannerView:self.view TOP:0];

IADView

#import "iAd/ADBannerView.h"
#import "GADBannerViewDelegate.h"
#import "GADRequest.h" @interface IADView : UIViewController<ADBannerViewDelegate,GADBannerViewDelegate>{
ADBannerView *adView;
GADBannerView *adBanner;
bool bShowAD;
}
//@property (retain) UIViewController *presentingViewController; + (IADView *)sharedInstance;
- (void) createAdBannerView:(UIView*) parentView TOP:(NSInteger) top;
- (void) hideAdView;
- (void) showAdView; @end
#import "IADView.h"
#import "GADBannerView.h"
#import "GADRequest.h"
#define ADMOB_ID "ca-app-pub-8377493162678380/4602812906"
@implementation IADView @synthesize presentingViewController; static IADView *iADHelper = nil;
+ (IADView *) sharedInstance {
if (!iADHelper) {
iADHelper = [[IADView alloc] init];
}
return iADHelper;
} - (void)createAdBannerView:(UIView*) parentView TOP:(NSInteger) top{
bShowAD = true;
//IAd
adView = [[ADBannerView alloc]initWithFrame:CGRectMake(0, top, 320, 100)];
adView.delegate = self;
adView.hidden = YES; [parentView addSubview:adView]; //AdMob
adBanner= [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, top, GAD_SIZE_320x50.width,GAD_SIZE_320x50.height)];
adBanner.adUnitID = [NSString stringWithUTF8String:ADMOB_ID];//调用id
adBanner.rootViewController = self;
adBanner.backgroundColor = [UIColor yellowColor];
adBanner.hidden = YES;
[self.view addSubview:adBanner];
[parentView addSubview:adBanner];
[adBanner setDelegate:self];
[adBanner loadRequest:[GADRequest request]];
} #pragma mark - iAd广告委托
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner
willLeaveApplication:(BOOL)willLeave
{
return YES;
} - (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"iAd loaded, AdMob will close"); [adView.superview bringSubviewToFront:adView]; if (adBanner != nil)
{
adBanner.delegate = nil;
[adBanner removeFromSuperview];
adBanner = nil;
} if(bShowAD)
adView.hidden = NO;
} - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"\n==>iAd failed to load");
if(adView != nil)
adView.hidden = YES;
} #pragma mark - adMob委托
- (void)adViewDidReceiveAd:(GADBannerView *)adViewMob {
NSLog(@"Admob loaded, iAd will close");
[adBanner.superview bringSubviewToFront:adBanner];
if(adView!=nil){
[adView setDelegate:nil];
[adView removeFromSuperview];
adView = nil;
}
if(bShowAD)
adBanner.hidden = NO;
} - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"\n==>adMob ERROR Failed to receive ad with error: %@", [error localizedFailureReason]);
adBanner.hidden = YES;
} #pragma mark - 公共方法
- (void) showAdView
{
NSLog(@"hide Ads");
if(adView!=nil)
adView.hidden = YES; if (adBanner != nil)
adBanner.hidden = YES;
bShowAD = false;
} - (void) hideAdView
{
NSLog(@"show Ads");
if(adView!=nil)
adView.hidden = NO; if (adBanner != nil)
adBanner.hidden = NO;
bShowAD = true;
} -(void)dealloc{
NSLog(@"163");
if(adView!=nil)
[adView removeFromSuperview];
} @end