Published on March 6, 2011 by Andreas in I talk code, I talk process, iPad and iPhone OS. 3 Comments Tags: , , , , , .GoogleAdmob_crop380w.jpg  
 

Integration of Google? AdMob? isn’t a big deal at all. AdMob provides a very good documentation how you can use their SDK within your iPhone/iPad app.

Today I wanna shortly summarize the few important steps you need to do to integrate AdMob:

  • First of all sign up at AdMob.
  • After that, add a new site/app within AdMob and provide the basic information requested.
  • Download the iPhone/iPad AdMob SDK and import it to your iPhone/iPad Xcode project.
  • Add following frameworks to your target:
    • libz.1.2.3.dylib
    • MediaPlayer.framework
    • MessageUI.framework
    • QuartzCore.framework
  • Now let your application delegate implement the AdMobDelegate protocol and add a adMobVisible boolean variable.
  • Within your application delegate add following methods for creating, showing and hiding your AdMob banner:
    // Add a new AdMob banner view to the current view  - (void) addAdmobView  {     // Create a new AdMobView for iPhone portrait mode     // At this point you can check your device and environment and     // provide the correct sized ad banner.     AdMobView *adMobView = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:self];        // Set position to the top but hide it     CGRect frame   = adMobView.frame;     frame.origin   = CGPointMake(0, -adMobView.frame.size.height);     adMobView.frame = frame;        // Add Banner to current view     [viewController.view addSubview:adMobView];     adMobVisible = NO;  }     // Hide the given AdMob banner with animation  - (void) hideAdMobView:(AdMobView*)adView  {     @synchronized(self)     {        if (adMobVisible == YES)        {           [UIView beginAnimations:@"animateBannerOff" context:NULL];           // Assumes the banner view is placed at the top of the screen.           adView.frame = CGRectOffset(adView.frame, 0, -adView.frame.size.height);           [UIView commitAnimations];              // Update view status           adMobVisible = NO;        }     }  }     // Show the given AdMob banner with animation  - (void) showAdMobView:(AdMobView*)adView  {     @synchronized(self)     {        if (adMobVisible == NO)        {           [UIView beginAnimations:@"animateBannerOn" context:NULL];           // Assumes the banner view is just off the top of the screen.           adView.frame = CGRectOffset(adView.frame, 0, adView.frame.size.height);           [UIView commitAnimations];              // Update view status           adMobVisible = YES;        }     }  }
  • Now call the method addAdmobView at the end of your application:didFinishLaunchingWithOptions method and a new ad banner will be created and added to the top of your current view.
  • Further more you need to implement some delegate methods:
    // Return your AdMob app publisher id  - (NSString *)publisherIdForAd:(AdMobView *)adView  {    // If you are going to develop a universal app    // you can return different publisher id depending    // on the current device over here. This makes reporting    // easier.     return @"YOUR_ADMOB_PUBLISHER_ID";  }     // Return the top level view controller of your ad view  - (UIViewController *)currentViewControllerForAd:(AdMobView *)adView  {     return viewController;  }     // Show the view when a new ad arrived  - (void)didReceiveAd:(AdMobView *)adView  {     [self showAdMobView:adView];  }     // Show the view when the ad was refreshed  - (void)didReceiveRefreshedAd:(AdMobView *)adView;  {     [self showAdMobView:adView];  }     // Hide the view in case of not receiving an ad  - (void)didFailToReceiveAd:(AdMobView *)adView;  {     [self hideAdMobView:adView];  }     // Hide the view in case of not receiving an refreshed ad  - (void)didFailToReceiveRefreshedAd:(AdMobView *)adView;  {     [self hideAdMobView:adView];  }
  • For testing purpose you should also implement the testDevices delegate method to return an array of UDIDs of your test devices. This will ensure, that you don’t get real ads on your test devices or within the simulator (clicking on real ads will be seen as a violation against AdMob terms & conditions).
    // Return an array of UDIDs of your test devices  - (NSArray *)testDevices  {     // Only return the UDID of the iphone simulator     return [NSArray arrayWithObjects:ADMOB_SIMULATOR_ID, nil];  }
  • Start the application within your simulator and you will see an ad banner at the top of your app and it will receive some test ads.

As you can see it isn’t that hard to use the AdMob SDK within your app. Go ahead and try it on your own. You can also check out my example at my GitHub repository. If you want to try out the example you need to download and import the AdMob SDK on your own.

 

http://blog.blackwhale.at/2011/03/admob-integration/

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()