Time for action — modifying the code to display ads

First, you need to open the main.h header file and make some changes to it. We dive here into Object-C/C++ code, so be confused if the code looks different from the native Monkey code:

  1. Add an import statement for the MobFox/MobFox.h file:
    #include <OpenAL/alc.h>
    #import <MobFox/MobFox.h>
    // ***** MonkeyView *****
    @interface MonkeyView : UIView{
    
  2. Next, add the MobFoxBannerViewDelegate to the Monkey view controller:
    // ***** MonkeyViewController *****
    @interface MonkeyViewController : UIViewController
    <MobFoxBannerViewDelegate> {
    @public
    }
    @end
    
  3. Next, we need to modify the main.m file, the heart of your Monkey code. Insert the viewDidLoad method into the MonkeyViewController implementation and call its super implementation. Place the method right under the shouldAutorotateToInterfaceOrientation method, which controls how the app will be able to rotate natively:
    @implementation MonkeyViewController
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    //..........
    }
    - (void)viewDidLoad {
    [super viewDidLoad];
    
  4. Now, create a new MobFox banner view:
    MobFoxBannerView *bannerView = [[MobFoxBannerView alloc] initWithFrame:
    

    The frame for creating the banner will be at the bottom of the screen. So, at the beginning, the ad is hidden in the off-screen area. You can't use the view sizes, as the view in Monkey is very small by default:

    CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, 320, 50)];
    
  5. Set the banner's delegate to self. This will trigger loading an ad:
    bannerView.delegate = self;
    
  6. Set the background color of the banner view:
    bannerView.backgroundColor = [UIColor darkGrayColor];
    
  7. Set refreshAnimation for the banner view to the CurlDown animation type:
    bannerView.refreshAnimation = UIViewAnimationTransitionCurlDown;
    
  8. Add the banner subview to the Monkey view. Then, close the method:
    [self.view addSubview:bannerView];
    }
    

    This method creates a new banner view, but you still need to let MobFox know your MobFox publisher ID. This is done inside the publisherIdForMobFoxBannerView method:

  9. First, add a pragma compiler switch for MobFox Delegate:
    #pragma mark MobFox Delegate
    
  10. Insert the publisherIdForMobFoxBannerView method, with NSString as a return value and banner as a parameter:
    - (NSString *)publisherIdForMobFoxBannerView:(MobFoxBannerView *)banner
    {
    
  11. Return the publisher ID that you got when you created the app on the MobFox website. Close the method:
    return @"InsertYourIdHere";
    }
    

    The banner was created in the off-screen area. When it receives an ad from MobFox, it should slide into the visible area. This is done with the next method.

  12. For this ad, use the mobfoxBannerViewDidLoadMobFoxAd method. The parameter is banner:
    - (void)mobfoxBannerViewDidLoadMobFoxAd:(MobFoxBannerView *)banner
    {
    
  13. So that you can see in the debugger that an ad was loaded, output some text that lets you know about it:
    NSLog(@"MobFox: did load ad");
    
  14. You want the banner to slide into the view, so begin a new animation:
    [UIView beginAnimations:@"MobFox" context:nil];
    [UIView setAnimationDuration:1];
    
  15. Set the frame of the banner to its final visible destination. Here, the bound size property works, for some reason. The height of the banner is 50 pixels, so we place it accordingly:
    banner.frame = CGRectMake(0, self.view.bounds.size.height - 50, 320, 50);
    
  16. Next, commit the animation and close the method:
    //banner.frame = CGRectMake(0, 0, 320, 50);
    [UIView commitAnimations];
    }
    

    Once an ad is received, the banner view will slide into the visible screen area. But sometimes, ads are not available. It could be that MobFox doesn't serve an ad, or the device has no connection to the Internet. For this, we need a method to slide the banner view outside the screen area.

  17. Add the method didFailToReceiveAdWithError with error as a parameter.
    - (void)mobfoxBannerView:(MobFoxBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
    {
    
  18. Output a message with the error so you can retrieve this in the debugger:
    NSLog(@"MobFox: did fail to load ad: %@", [error localizedDescription]);
    
  19. Start a new UIView animation:
    [UIView beginAnimations:@"MobFox" context:nil];
    [UIView setAnimationDuration:1];
    
  20. Create a target frame for the banner outside the view bounds, and commit the animation:
    banner.frame = CGRectMake(0, self.view.bounds.size.height, 320, 50);
    [UIView commitAnimations];
    
  21. Close this method:
    }
    @end
    
  22. You have now added all the methods that are needed to display ads in your app. If you try to build the game now, you will see some errors showing up. This is because a Monkey project is natively a C++ project. However, we are using Object-C here, and so need to let the linker know about it. For this, you need to change the linker flags, as follows:
    • Double-click on the project name to bring up the project info.
    • Inside the Build tab, search for other linker flags.
    • Add the -ObjC entry to it.
    Time for action — modifying the code to display ads
    • Make sure you add this flag to the debug and release configuration.

What just happened?

You have added all the needed methods to your XCODE project to display MobFox ads inside your game. If you now build and run the project inside the simulator, you should see a test banner ad:

What just happened?

Activating real ads

After you have finished the implementation, two more steps have to be taken to display real ads in your game. First, change the URL link to your app inside the app store once you know it.

Second, when the application is going live, request the activation of ads from MobFox. This can be done from your MobFox account, inside the list of apps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset