Saturday 30 November 2013

Adding ADMOB to Appery iOS Projects

Admob in iOS Appery Apps

Step-by-Step



Adding Admob adverts to your Appery applications can be daunting.  Below you will find a walkthrough for adding the SDK, and modifying the source code to enable you to quickly and easily master this art.

Currently Appery is using Phonegap V2.4.0, so this guide relates to that version and will be updated if necessary when Phonegap is updated.



Exporting from Appery and Importing into Xcode - the first steps.


  • Firstly, please note, this code overlays the top 30px of the display, you should pad the header in your Appery application to make room.  The reason this is done is because when I generated the space in Xcode, the advert would not click  You will need to export your Appery project by clicking Export -> Xcode Project.
  • Once you have the project, unzip it a a folder, then open double click on the Xcode project file.
  • Right click on the Project and select Add Files to “app title” ,browse to your google admob ads sdk iosfolder until you reach the files. Select all the files (but not the add-ons folder) and add the files.
  • Click on the Target, then select Build Phases tab, then select Link Binary With Libraries, Click the +icon and add the following libraries: StoreKit, Audio, Toolbox, MessageUI, SystemConfiguration, CoreGraphics & AdSupport
  • Click on the Build Settings tab, then scroll down to Other Linker Flags Double click to the right to edit the setting of this flag, keep –objC and remove all load 



Source Code Changes






  • Open ApplicationDelegate.m and Scroll down to find the function:
    - (void) webViewDidFinishLoad:(UIWebView*) theWebView
    Under the following line: theWebView.backgroundColor = [UIColor blackColor];
    add
    //
    // - Ad Code Here
    // -------------- //
    // Add the view controller's view to the window and display.
    [self.window setRootViewController:self.viewController];
    [self.window makeKeyAndVisible];
    //
    // - End Ad Code
    // -------------
    //
  • Open MainViewController.h
    After #import <Cordova/CDVViewController.h> Add
    #import "GADBannerViewDelegate.h"
    @class GADBannerView, GADRequest;
    #define kSampleAdUnitID @"
    YourAdmobIDHere"
  • After @interface MainViewController : CDVViewController but before @end add
    <GADBannerViewDelegate>
    {
        GADBannerView *adBanner_;
    }
    @property (nonatomic, retain) GADBannerView *adBanner; - (GADRequest *)createRequest;







  • Open MainViewController.m
    After #import <AVFoundation/AVFoundation.h> add
    #import "GADBannerView.h" #import "GADRequest.h"
  • After @implementation MainViewController add
    @synthesize adBanner = adBanner_; 
  • Scroll down to function - (void)dealloc after { add
    adBanner_.delegate = nil; [adBanner_ release]; 
  • Scroll down to function - (void)viewDidLoad After [super viewDidLoad]; add
    // Ad Code here
    //-------------
    // Initialize the banner at the top of the screen.
    CGPoint origin = CGPointMake(0.0,0.0);
    // Use predefined GADAdSize constants to define the GADBannerView.
    self.adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease];
    // Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID
    // before compiling.
    self.adBanner.adUnitID = kSampleAdUnitID;
    self.adBanner.delegate = self;
    [self.adBanner setRootViewController:self];
    [self.view addSubview:self.adBanner];
    self.adBanner.center = CGPointMake(self.view.center.x, self.adBanner.center.y);
    [self.adBanner loadRequest:[self createRequest]];
    //
    // End Ad Code
    //----------------









 

  • Scroll to the end of the routine, just before @end add
    //
    // - Ad Handling Code here
    // -----------------------
    //
    #pragma mark GADRequest generation
    // 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 an 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 are  
    // printed to the console when the app is launched.  
    nil];  
    return request;
    }
    #pragma mark GADBannerViewDelegate impl
    // We've received an ad successfully.
    - (void)adViewDidReceiveAd:(GADBannerView *)adView
    {
       NSLog(@"Received ad successfully");
    }
    - (void)adView:(GADBannerView *)view
    didFailToReceiveAdWithError:(GADRequestError *)error
    {  
      NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
    }
    //
    // - End Of Ad Handling Code
    // -------------------------
    //

No comments:

Post a Comment