? 1, edition
Cocos2d-x 2.2
GoogleAdMobAdsSdkiOS-6.5.1
2. Import
2.1 Importing header files and. A Files
and add the path to the. A file in Librabry Search paths
2.2 Importing the required iOS constructs
Adsupport.framework
Storekit.framework
Audiotoolbox.framework
Mediaplayer.framework
Messageui.framework
Systemconfiguration.framework
Add other Linker Flags in 2.3Linking
Otherwise you will encounter-[gadobjectprivate Changestate:]: Unrecognized selector sent to instance questions
3. End of code
It is recommended to encapsulate a object-c class from scratch, but the end method is the same, since you are participating directly in the AppController code.
First import GADInterstitila.h header file, end Gadinterstitialdelegate entrusted, and define a Gadinterstitila feature. The Showinterstitial method is used for external calls to flash advertisements.
- #import
- #import "GADInterstitial.h"
- @class Rootviewcontroller;
- @interface Appcontroller:nsobject {UIWindow *window; Rootviewcontroller *viewcontroller; Gadinterstitial *interstitial_;
- }
- @property (nonatomic, retain) gadinterstitial *interstitial;
- -(void) showinterstitial;
- @end
Copy Code
The. m file is primarily the end of the delegate function, interstitial initialization, ad advance request and advertising flash methods
- #pragma mark-
- #pragma interstitial Delegate
- -(void) Interstitial: (Gadinterstitial *) interstitial
- Didfailtoreceiveadwitherror: (Gadrequesterror *) Error {
- }
- -(void) Interstitialdidreceivead: (gadinterstitial *) Interstitial {
- }
- -(void) Interstitialdiddismissscreen: (gadinterstitial *) ad
- {[Self preloadrequest];
- }
- -(void) showinterstitial{if (self.interstitial.isReady) {Cclog ("ready"); [Self.interstitial Presentfromrootviewcontroller:viewcontroller]; } else {Cclog ("not Ready"); [Self preloadrequest]; }
- }
- #pragma mark Gadrequest generation
- -(void) initinterstitial
- {//Create a new gadinterstitial each time. A gadinterstitial//would only show one request with its lifetime. The property would release the//old one and set the new one. self.interstitial = [[[Gadinterstitial alloc] init] autorelease]; Self.interstitial.delegate = self; Note:edit INTERSTITIALEXAMPLEAPPDELEGATE.M to update//interstitial_ad_unit_id with your interstitial AD UNIT Id. Self.interstitial.adUnitID = interstitial_ad_unit_id;
- }
- -(void) preloadrequest
- {Cclog ("Pre load"); [Self initinterstitial]; [Self.interstitial loadrequest: [self createrequest]];
- }
- Here we ' re creating a simple gadrequest and whitelisting the application
- For test ads. You should request test ads during development to avoid
- Generating invalid impressions and clicks.
- -(Gadrequest *) createrequest {gadrequest *request = [gadrequest request]; Make the request for a test ad. Put in a identifier for the simulator as//well as any devices you want to receive test ads. Request.testdevices = [Nsarray arraywithobjects://Todo:add your Device/simulator test identifiers here. They was//printed to the console when the app is launched. NIL]; return request;
- }
Copy Code
Unlike Android, the interstitial target must be initialized from scratch each time it is request.
Intact. m Files:
- #import "AppController.h"
- #import "EAGLView.h"
- #import "Cocos2d.h"
- #import "AppDelegate.h"
- #import "RootViewController.h"
- #define INTERSTITIAL_AD_UNIT_ID @ "your_own_id"
- @implementation AppController
- @synthesize interstitial = Interstitial_;
- #pragma mark-
- #pragma mark Application Lifecycle
- COCOS2D Application Instance
- Static Appdelegate s_sharedapplication;
- -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { //Override point for customization after application launch. //Ad d The View controller ' s view to the window and display. window = [[UIWindow alloc] Initwithfram e: [[UIScreen Mainscreen] bounds]]; //Init the eaglview Eaglview *_ _glview = [Eaglview viewwithframe: [window bounds] pixelformat:keaglcolorformatrgb565 depthformat:gl_depth24_stencil8 _oes preservebackbuffer:no sharegroup:nil multisampling:no numberofsamples:0]; //Use RootV Iewcontroller Manage eaglview Viewcontroller = [[Rootviewcontroller alloc] Initwithnibname: Nil bundle:nil]; viewcontroller.wantsfullscreenlayout = yes; VIEWCO Ntroller.view = __glview; &NBSP; //Set Rootviewcontroller to window if ([[Uidevice currentdevice].systemversion Floatva Lue] < 6.0) { //Warning:addsubview doesn ' t work on ios6 & nbsp [window addsubview:viewcontroller.view]; } else { //Use the method on ios6 [window SETROOTVI ewcontroller:viewcontroller]; } [window makekeyandvisible]; [[UIApplication sharedapplication] setstatusbarhidden:true];
- [Self initinterstitial];//initializes [self preloadrequest] every request; Cocos2d::ccapplication::sharedapplication ()->run (); return YES;
- }
- -(void) Applicationwillresignactive: (uiapplication *) application {/* Sent when the application are about to move fr Om Active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. */Cocos2d::ccdirector::shareddirector ()->pause ();
- }
- -(void) Applicationdidbecomeactive: (uiapplication *) application {/* Restart any tasks this were paused (or not yet started) while the application was inactive. If the application is previously in the background, optionally refresh the user interface. */Cocos2d::ccdirector::shareddirector ()->resume ();
- }
- -(void) Applicationdidenterbackground: (uiapplication *) application {/* Use the This method to release shared resources , save user data, invalidate timers, and store enough application state information to restore your application to its cur Rent state in case it is terminated later. If your application supports background execution, called instead of Applicationwillterminate:when the user quits. */Cocos2d::ccapplication::sharedapplication ()->applicationdidenterbackground ();
- }
- -(void) Applicationwillenterforeground: (uiapplication *) Application {/* called as part of transition from the back Ground to the inactive state:here your can undo many of the changes made on entering the background. */Cocos2d::ccapplication::sharedapplication ()->applicationwillenterforeground ();
- }
- -(void) Applicationwillterminate: (uiapplication *) Application {/* Called when the application was about to Terminat E. See also Applicationdidenterbackground:. */
- }
- #pragma mark-
- #pragma Mark Memory Management
- -(void) applicationdidreceivememorywarning: (uiapplication *) application {/* Free up as much memory as possible by Purging cached data objects that can is recreated (or reloaded from disk) later. */
- }
- -(void) Dealloc {interstitial_.delegate = nil; [Interstitial_ release]; [Window release]; [Super Dealloc];
- }
- #pragma mark-
- #pragma interstitial Delegate
- -(void) Interstitial: (Gadinterstitial *) interstitial
- Didfailtoreceiveadwitherror: (Gadrequesterror *) Error {
- }
- -(void) Interstitialdidreceivead: (gadinterstitial *) Interstitial {
- }
- -(void) Interstitialdiddismissscreen: (gadinterstitial *) ad
- {[Self preloadrequest];
- }
- -(void) showinterstitial{if (self.interstitial.isReady) {Cclog ("ready"); [Self.interstitial Presentfromrootviewcontroller:viewcontroller]; } else {Cclog ("not Ready"); [Self preloadrequest]; }
- }
- #pragma mark Gadrequest generation
- -(void) initinterstitial
- {//Create a new gadinterstitial each time. A gadinterstitial//would only show one request with its lifetime. The property would release the//old one and set the new one. self.interstitial = [[[Gadinterstitial alloc] init] autorelease]; Self.interstitial.delegate = self; Note:edit INTERSTITIALEXAMPLEAPPDELEGATE.M to update//interstitial_ad_unit_id with your interstitial AD UNIT Id. Self.interstitial.adUnitID = interstitial_ad_unit_id;
- }
- -(void) preloadrequest
- {Cclog ("Pre load"); [Self initinterstitial]; [Self.interstitial loadrequest: [self createrequest]];
- }
- Here we ' re creating a simple gadrequest and whitelisting the application
- For test ads. You should request test ads during development to avoid
- Generating invalid impressions and clicks.
- -(Gadrequest *) createrequest {gadrequest *request = [gadrequest request]; Make the request for a test ad. Put in a identifier for the simulator as//well as any devices you want to receive test ads. Request.testdevices = [Nsarray arraywithobjects://Todo:add your Device/simulator test identifiers here. They was//printed to the console when the app is launched. NIL]; return request;
- }
- @end
Copy Code
4. Call
- #elif (Cc_target_platform = = Cc_platform_ios) [(appcontroller*) [UIApplication sharedapplication].delegate Showinterstitial];
- #endif引荐网站 http://www.keymob.com/
[iOS development] cocos2d-x2.2 using AdMob interstitial advertising tutorial (i)