iad admob整合代码


#pragma mark iAd

- (void)addIAdBanner {

    // if place the banner on the bottom

    // adView = [[ADBannerView alloc] initWithFrame:CGRectZero];

    

    adView = [[ADBannerView alloc] initWithFrame:CGRectMake(320 - (32 / 2) - 480 / 2, 480 / 2 - (50 - (32 / 2)), 0, 0)];

    

    adView.frame = CGRectOffset(adView.frame, 32, 0);   // hide

    

    adView.delegate = self;

    adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];

    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;

    [[[CCDirector sharedDirector] openGLView] addSubview:adView];

    

    // Transform iAd

    adView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

    

    iAdBannerIsVisible = NO;

}


- (void)removeIAdBanner {

    // clear iAd

    adView.delegate = nil;

    [adView removeFromSuperview];

    [adView release];

    adView = nil;

    

    iAdBannerIsVisible = NO;

}


- (void)bannerViewDidLoadAd:(ADBannerView *)banner {

    NSLog(@"[iAd]: Ad did load.");

    

    if (!iAdBannerIsVisible) {

        adView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

        

        adView.frame = CGRectOffset(adView.frame, -32, 0);

        

        adView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

        

        iAdBannerIsVisible = YES;

    }

    

    if (admobBannerIsVisible) {

        // hide it

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

        adMobAd.frame = CGRectMake(140 + 48, 215, 320, 48);

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

    }

}


- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {

    NSLog(@"[iAd]: Faild to load the banner: %@", error);

    

    if (iAdBannerIsVisible) {

        adView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

        adView.frame = CGRectOffset(adView.frame, 32, 0);

        adView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

        

        iAdBannerIsVisible = NO;

    }

    

    if (admobBannerIsVisible) {

        // show it

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

        adMobAd.frame = CGRectMake(140, 215, 320, 48);

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

    }

}


- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {

    NSLog(@"[iAd]: An action was started from the banner. Application will quit: %d", willLeave);

    

    return YES;

}


- (void)bannerViewActionDidFinish:(ADBannerView *)banner {

    NSLog(@"[iAd]: Action finished.");

}

#pragma mark AdMobDelegate methods


- (NSString *)publisherIdForAd:(AdMobView *)adView {

    return @"a14c561cd7cbb82"; // this should be prefilled; if not, get it from  www.admob.com

}


- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView {    

    return controller;

}


- (UIColor *)adBackgroundColorForAd:(AdMobView *)adView {

    return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor

}


- (UIColor *)primaryTextColorForAd:(AdMobView *)adView {

    return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor

}


- (UIColor *)secondaryTextColorForAd:(AdMobView *)adView {

    return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor

}


// Sent when an ad request loaded an ad; this is a good opportunity to attach

// the ad view to the hierachy.

- (void)didReceiveAd:(AdMobView *)adView {

    NSLog(@"AdMob: Did receive ad");

    

    adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

    adMobAd.frame = CGRectMake(140, 215, 320, 48);

    adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

    

    if (iAdBannerIsVisible) {

        // if iAdBannerIsVisible, hide admob banner

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(0));

        adMobAd.frame = CGRectMake(140 + 48, 215, 320, 48);

        adMobAd.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));

    }

    

    [[[CCDirector sharedDirector] openGLView] addSubview:adMobAd];

    

    [refreshTimer invalidate];

    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];

    

    admobBannerIsVisible = YES;

}


// Sent when an ad request failed to load an ad

- (void)didFailToReceiveAd:(AdMobView *)adView {

    NSLog(@"field receive admob.");

    

    [adMobAd removeFromSuperview];  

    

    [self performSelector:@selector(requestAd) withObject:nil afterDelay:2.0];

    

    admobBannerIsVisible = NO;

}


- (void)refreshAd:(ccTime)dt {

    NSLog(@"refresh admob.");

    

    [adMobAd requestFreshAd];

}


- (void)requestAd {    

    adMobAd = [AdMobView requestAdWithDelegate:self];

}

你可能感兴趣的:(admob)