[iOS development] cocos2d-x2.2 using AdMob interstitial advertising tutorial (i)

Source: Internet
Author: User
Tags admob

? 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.

    1. #import
    2. #import "GADInterstitial.h"
    3. @class Rootviewcontroller;
    4. @interface Appcontroller:nsobject {UIWindow *window;        Rootviewcontroller *viewcontroller; Gadinterstitial *interstitial_;
    5. }
    6. @property (nonatomic, retain) gadinterstitial *interstitial;
    7. -(void) showinterstitial;
    8. @end
Copy Code



The. m file is primarily the end of the delegate function, interstitial initialization, ad advance request and advertising flash methods

  1. #pragma mark-
  2. #pragma interstitial Delegate
  3. -(void) Interstitial: (Gadinterstitial *) interstitial
  4. Didfailtoreceiveadwitherror: (Gadrequesterror *) Error {
  5. }
  6. -(void) Interstitialdidreceivead: (gadinterstitial *) Interstitial {
  7. }
  8. -(void) Interstitialdiddismissscreen: (gadinterstitial *) ad
  9. {[Self preloadrequest];
  10. }
  11. -(void) showinterstitial{if (self.interstitial.isReady) {Cclog ("ready");        [Self.interstitial Presentfromrootviewcontroller:viewcontroller]; } else {Cclog ("not Ready"); [Self preloadrequest]; }
  12. }
  13. #pragma mark Gadrequest generation
  14. -(void) initinterstitial
  15. {//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;
  16. }
  17. -(void) preloadrequest
  18. {Cclog ("Pre load"); [Self initinterstitial]; [Self.interstitial loadrequest: [self createrequest]];
  19. }
  20. Here we ' re creating a simple gadrequest and whitelisting the application
  21. For test ads. You should request test ads during development to avoid
  22. Generating invalid impressions and clicks.
  23. -(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;
  24. }
Copy Code

Unlike Android, the interstitial target must be initialized from scratch each time it is request.
Intact. m Files:

  1. #import "AppController.h"
  2. #import "EAGLView.h"
  3. #import "Cocos2d.h"
  4. #import "AppDelegate.h"
  5. #import "RootViewController.h"
  6. #define INTERSTITIAL_AD_UNIT_ID @ "your_own_id"
  7. @implementation AppController
  8. @synthesize interstitial = Interstitial_;
  9. #pragma mark-
  10. #pragma mark Application Lifecycle
  11. COCOS2D Application Instance
  12. Static Appdelegate s_sharedapplication;
  13. -(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];
  14. [Self initinterstitial];//initializes [self preloadrequest] every request;        Cocos2d::ccapplication::sharedapplication ()->run (); return YES;
  15. }
  16. -(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 ();
  17. }
  18. -(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 ();
  19. }
  20. -(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 ();
  21. }
  22. -(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 ();
  23. }
  24. -(void) Applicationwillterminate: (uiapplication *) Application {/* Called when the application was about to Terminat E. See also Applicationdidenterbackground:. */
  25. }
  26. #pragma mark-
  27. #pragma Mark Memory Management
  28. -(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. */
  29. }
  30. -(void) Dealloc {interstitial_.delegate = nil;        [Interstitial_ release];        [Window release]; [Super Dealloc];
  31. }
  32. #pragma mark-
  33. #pragma interstitial Delegate
  34. -(void) Interstitial: (Gadinterstitial *) interstitial
  35. Didfailtoreceiveadwitherror: (Gadrequesterror *) Error {
  36. }
  37. -(void) Interstitialdidreceivead: (gadinterstitial *) Interstitial {
  38. }
  39. -(void) Interstitialdiddismissscreen: (gadinterstitial *) ad
  40. {[Self preloadrequest];
  41. }
  42. -(void) showinterstitial{if (self.interstitial.isReady) {Cclog ("ready");        [Self.interstitial Presentfromrootviewcontroller:viewcontroller]; } else {Cclog ("not Ready"); [Self preloadrequest]; }
  43. }
  44. #pragma mark Gadrequest generation
  45. -(void) initinterstitial
  46. {//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;
  47. }
  48. -(void) preloadrequest
  49. {Cclog ("Pre load"); [Self initinterstitial]; [Self.interstitial loadrequest: [self createrequest]];
  50. }
  51. Here we ' re creating a simple gadrequest and whitelisting the application
  52. For test ads. You should request test ads during development to avoid
  53. Generating invalid impressions and clicks.
  54. -(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;
  55. }
  56. @end
Copy Code

4. Call


    1. #elif (Cc_target_platform = = Cc_platform_ios) [(appcontroller*) [UIApplication sharedapplication].delegate Showinterstitial];
    2. #endif引荐网站 http://www.keymob.com/

[iOS development] cocos2d-x2.2 using AdMob interstitial advertising tutorial (i)

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.