Mixed Use of IAD and AdMob

Source: Internet
Author: User
Tags admob

When IAD and AdMob are mixed, AdMob is used when the IAD cannot be received. Once the IAD is received, the AdMob is closed. This enables AdMob in countries that do not support IAD. The country does not need to be judged.

The judgment method has three main items:
1. Request Both IAD and AdMob
2. If you receive an iad, turn off AdMob.
3. After receiving the AdMob ad five times, it indicates that you have not received the IAD until now. This means you can disable the IAD if you have not received the IAD.

Header file:

#import#import "iAd/ADBannerView.h"#import "GADBannerViewDelegate.h"#import "GADRequest.h"@interface IADHelper : UIViewController{ADBannerView *adView;GADBannerView *adBanner;bool bShowIAD;bool bValidIAD;int receiveAdmob;}@property (retain) UIViewController *presentingViewController;+ (IADHelper *)sharedInstance;- (GADRequest *)createRequest;- (void) hideBannerView;- (void) unhideBannerView;- (void) receiveADmob;@end

Implementation file:

# Import "iadhelper. H "# import" admobhelper. H "# import" admobhelper. H "# import" gadbannerview. H "# import" gadrequest. H "@ implementation iadhelper @ synthesize presentingviewcontroller; static iadhelper * iadhelper = nil; + (iadhelper *) sharedinstance {If (! Iadhelper) {iadhelper = [[iadhelper alloc] init];} return iadhelper;}-(void) createadbannerview {bvalidiad = false; receiveadmob = 0; // Both IAD and AdMob are created, they all request the advertisement class classadbannerview = nsclassfromstring (@ "adbannerview"); If (classadbannerview! = Nil) {adview = [[classadbannerview alloc] init]; [adview setframe: cgrectmake (0, 0,320, 50)]; [adview setdelegate: Self]; adview. hidden = true; bshowiad = true; // uiview * testv = al_iphone_get_view (); add it to your parent view [testv addsubview: adview];} cgpoint origin = cgpointmake (0.0, 0.0); // use predefined gadadsize constants to define the gadbannerview. adbanner = [[gadbannerview alloc] initwithadsize: kgadadsizeb Annerorigin: origin] autorelease]; adbanner. adunitid = @ "a150062b54b1808"; [adbanner setdelegate: Self]; [adbanner setrootviewcontroller: Self]; [self. view addsubview: adbanner]; // uiview * testv = al_iphone_get_view (); add it to your parent view [testv addsubview: adbanner]; [adbanner loadrequest: [self createrequest];}-(bool) bannerviewactionshouldbegin :( adbannerview *) bannerwillleaveapplication :( bool) willleave {return yes; }-(Void) bannerviewdidloadad :( adbannerview *) banner {If (! Bshowiad) return; nslog (@ "% d", adview. bannerloaded); adview. hidden = no; nslog (@ "did load"); [adview. superview bringsubviewtofront: adview]; // when an IAD is received, the AdMob ad window is closed if (adbanner! = Nil) {adbanner. delegate = nil; [adbanner removefromsuperview]; adbanner = nil ;}}- (void) bannerview :( adbannerview *) bannerdidfailtoreceiveadwitherror :( nserror *) error {adview. hidden = yes;}-(gadrequest *) createrequest {gadrequest * request = [gadrequest request]; return request;} # pragma mark gadbannerviewdelegate impl // we 've got ed an ad successfully. -(void) adviewdidreceivead :( gadbannerview *) advie W {nslog (@ "received ad successfully"); [adbanner. superview bringsubviewtofront: adbanner]; [self receiveadmob];}-(void) receiveadmob {// five AdMob ads have been received, indicating that IAD has not been received yet, this means that you can close IAD without receiving IAD. receiveadmob ++; If (receiveadmob> 5) {[adview setdelegate: Nil] ;}}-(void) adview :( gadbannerview *) viewdidfailtoreceiveadwitherror :( gadrequesterror *) error {nslog (@ "failed to receive ad with error: % @", [error localize Dfailurereason]);}-(void) hidebannerview {adview. Hidden = yes; If (adbanner! = Nil) adbanner. Hidden = yes; bshowiad = false;}-(void) unhidebannerview {adview. Hidden = no; If (adbanner! = Nil) adbanner. Hidden = no; bshowiad = true;}-(void) dealloc {[adview removefromsuperview]; [Super dealloc];} @ end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.