IPhone

From Admob For Developers

Jump to: navigation, search

Contents

Getting Started with AdMob iPhone Ads

This page will help you get started with becoming a publisher for AdMob iPhone Ads.

Learn about AdMob iPhone ads

  • Review http://admob.com/iphone
  • AdMob iPhone ads can be integrated with Web Apps or iPhone Apps
  • AdMob iPhone ads are customized specifically for iPhone and iTouch devices with OS 2.0 and above
  • Ad format is: 320 pixels wide x 48 pixels tall


Sign up with AdMob

1. Register or login at admob.com, and click 'Sites & Apps".

2. Click "Add iPhone Site/App", and provide the info for your site or app.

3. Once you've added your site, click "Install Code". You should see a button to download the SDK, or a code snippet for your iPhone site.

4. Once you've tested your integration, set test = false and you're ready to start serving ads!

Web Integration

Basic integration for integrating with a web app is to place the following snippet of code (also available under Install Code here http://www.admob.com/s/my_sites/ ) in the exact spot in the page where you want the ad to appear.

<script type="text/javascript">
 var admob_vars = {
  pubid: '[PUBLISHER_ID]', // publisher id
  bgcolor: '000000', // background color (hex)
  text: 'FFFFFF', // font-color (hex)
  test: true // test mode, set to false if non-test mode
 };
 </script>
 <script type="text/javascript" src="http://mm.admob.com/static/iphone/iadmob.js"></script>

The above snippet will always return a test ad for you and during development or testing this should be kept in test mode. Once you set test to false you will receive live ads and affect your impression count. It is possible for you to receive ads in test mode and not see ads once the test mode is false.

Additional API/Technical Notes

This section is for web apps or publishers that are experienced in javascript

Our javascript is written in such a way to be the lowest possible integration bar for general users. This means that the above code simply fires off an initialize function on the window 'load' event (akin to <body onload='_admob.init()'>). The init event looks for our script tags in the dom and works off these hooks. This means inserting the script dynamically into your page will fail.

If you are in need to insert ads dynamically into your page use the following method:

<script type="text/javascript">
 var admob_vars = {
  pubid: '[PUBLISHER_ID]', // publisher id
  test: true, // test mode, set to false if non-test mode
  manual_mode: true
 };
 </script>
 <script type="text/javascript" src="http://mm.admob.com/static/iphone/iadmob.js"></script>

This will suppress the init function. To request an ad use the following function

/**
 * @param el Node a dom element
 * @return object adEl is a handle to the ad.
 */
 _admob.fetchAd = function(el) {
  ....
   return {
     adEl: adFrame   
   };
 };

for example if you want an ad rendered in a <div id='admob_ad'></div> use:

 _admob.fetchAd(document.getElementById('admob_ad'));

Please keep in mind that id's must be unique to the page.

You can also use this handle to hide ads.

 var ad = _admob.fetchAd(document.getElementById('admob_ad'));
 ad.adEl.style.display = 'none'; // OR hide it w/o affecting dom layout
 ad.adEl.style.visibility = 'hidden';

Also if you want to request a new ad re-using the same div:

 <script>
 _admob.fetchAd(document.getElementById('admob_ad'));
 </script>
 <script>
 // and then a new ad is requested simply  do the following
 var c = document.getElementById('admob_ad').childNodes;
 for(var i = 0, l = c.length; i < l; i++) {
 	document.getElementById('admob_ad').removeChild(c[i]);
 }
 _admob.fetchAd(document.getElementById('admob_ad'));
 </script>

_admob.fetchAd returns the following object

  {
   adEl: <handle to the ad frame, this is an iframe>
   shim: <handle to the shim under the ad frame, this is a div>
 }

If you want to detect that an ad has returned you can check the height of the iframe.

 <script>
 var ad = _admob.fetchAd(document.getElementById('admob_ad'));
 var polling_timeout = 0;
 var polling_func = function() {
  if(ad.adEl.height == 48) {
    // we have an ad
    console.log('received ad');
  }
  if(polling_timeout < 5000) {
    console.log('repoll');
    polling_timeout += 1000;
    window.setTimeout(1000, polling_func);
  }
  else {
    console.log('no ad');
    // 5 seconds reached, slow network, or no ad was returned
  }
 };
 window.setTimeout(polling_func, 1000);</script>

Also, clicks can be opened in a new tab in Web Apps. *note: this will only work for ads that link to web pages. Simply use:

<script type="text/javascript">
 var admob_vars = {
  ...
  new_window: true
 };
 </script>


iUI integration

http://code.google.com/p/iui/

The easiest way to integrate with iUI is to create a <li> in the main <ul> portion of your site and include the AdMob iPhone Publisher code inside this li.

For subsequent fragments that are loaded via AJAX, iUI does not evaluate javascript returned (ie any script blocks in side the fragment are not evaluated). We have submitted a patch to iUI here: http://code.google.com/p/iui/issues/detail?id=102 that will resolve this issue.

Meanwhile you can grab the patch and apply it to your iUI.js file. Then you can return fragments like this:

 <div>Fragment content</div>
 <div id="admob_ad"></div>
 <script>
    _admob.fetchAd(document.getElementById('admob_ad'));
 </script>

Keep in mind that iUI returns these divs into one document object so ID collision is highly likely!

WebApp.net integration

Coming soon

http://webapp.net.free.fr/features.php

Tips

(1) Use a meta viewport tag so your page shows up in full scale to users:

 <meta name="viewport" id="viewport" content="width=320, user-scalable=no" />

(2) set a static width for your body

 <body style='width: 320px'>
 or in your css
 body {
   width: 320px;
 }

AdMob iPhone SDK

AdMob ads are displayed in 320x48 UIWebViews. They can be placed anywhere: in the main window of your app, in a tableview, on a splash page, etc. The ad delegate controls the features of the ad and receives notifications. You may display the ad view when you begin requesting an ad, or you may delay displaying the view until the ad has loaded successfully; see the sample IB controller (AdViewController.m) for an example of how to do this. However, do not prefetch ads -- that is, only request an ad that you intend to display. Prefetching ads can significantly degrade our ability to optimize ads for your app and thus negatively impact your earnings.


SDK Integration instructions

(1) Add the AdMob library code and headers to your project. They are contained in the AdMob subdirectory. There are four files in all: AdMobDelegateProtocol.h, AdMobView.h, libAdMobDevice.a, libAdMobSimulator.a. (If you are using Objective-C++, you may need a non-thumb build of libAdMobDevice.a instead, which can be found in the extras directory, called libAdMobDeviceNoThumb.a.)

NOTE: if you're compiling for SDK 3.0 then you'll need the libAdMobDevice3_0.a, libAdMobSimulator3_0.a in the extras directory. Otherwise the simulator will crash.


(2) Add the CoreLocation, CoreGraphics, QuartzCore, and AddressBook frameworks to your project. They are required to compile. They are dynamic libraries so they won't increase the size of your app. You can decline to request location information in your delegate. The AddressBook framework will never be used to pull information from a user's address book, only to allow the user to add a contact (with user confirmation) from an advertiser.


(3) Get a publisher id from www.admob.com. Be sure to set your publisher id correctly in your delegate! If you are using the IBSupport files or running one of the sample projects, your publisher id should be pre-filled for you.


(4) Integrate AdMob ads with your app. We've included sample projects showing super-simple integration in three styles. Here is sample code extracted for those three styles:

(a) Add an ad to a view using Interface Builder

  • Add AdViewController.h and AdViewController.m to your project (located in the IBSupport subdirectory).
  • Open Interface Builder.
  • Place a 320x48 UIView where you want the ad to appear.
  • Add an Object, and change its type to AdViewController.
  • Set the view outlet of the AdViewController to your UIView.
  • Edit AdViewController.m to make sure that your publisher id and other options are set correctly.

(b) Add an ad to a view programmatically

  • Create an AdMob delegate class; it can be as simple as a class that only implements -publisherId:.
  • Add the following code to your view creation (modify as appropriate):
   AdMobView *ad = [AdMobView requestAdWithDelegate:<your delegate>]; // start a new ad request
   ad.frame = CGRectMake(0, 432, 320, 48); // set the frame, in this case at the bottom of the screen
   [self.window addSubview:ad]; // attach the ad to the view hierarchy; self.window is responsible for retaining the ad
 

(c) Add an ad to a tableview

  • Create an AdMob delegate class; it can be as simple as a class that only implements -publisherId:.
  • Add to your -tableView:cellForRowAtIndexPath: method (modify as appropriate):
   [cell.contentView addSubview:[AdMobView requestAdWithDelegate:<your delegate>]];


(5) The AdMob library uses TouchJSON version 1.0; it is compiled into the AdMob SDK. We have included the TouchJSON headers in the TouchJSON directory, in case you also want to use TouchJSON. You can read more about TouchJSON at http://code.google.com/p/touchcode/


(6) Contact support@admob.com with bugs, questions, or feedback, or check the wiki at http://developer.admob.com/wiki/iPhone

UIWebView integration instructions

It is strongly discouraged to show javascript-integrated AdMob ads as part of a UIWebView. Instead, you should integrate at the SDK level (see above). This will provide a better user experience and enable ad features not possible with javascript integration. Furthermore, this is not an officially supported style of integration, and something that works now may break in the future, with no warning.

If for some reason you must show AdMob ads in a UIWebView, here are some tips to get them working:

1. Implement webView:shouldStartLoadWithRequest:navigationType: to catch any request other than about: requests and requests to your domain and direct them to the OS to open. An example implementation is:

   - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     NSURL *url = [request URL];
     
     // Check whether the url is an about: request or a request to our domain. If so, allow it.
     if([@"about" isEqualToString:[url scheme]]) {
       return YES;
     }
     if([@"expected.domain.com" isEqualToString:[url host]]) {
       return YES;
     }
     
     // Must be a link to an external site; have the OS open it. Don't bother trying to open it here.
     [[UIApplication sharedApplication] openURL:url];
     return NO;
   }

2. Be prepared for your UIWebViewDelegate methods to be called multiple times for a single page load. This happens any time that a page contains an iframe, and AdMob ads are rendered using iframes.

iPhone App Download Tracking

http://developer.admob.com/wiki/AppStoreDownloads

FAQ

Q: I think I've done everything correctly, but I'm not seeing any ads. What's up with that?

A: There are a few things to try:

1. Request an ad in test mode. In the SDK, add this to your AdMobDelegate:

- (BOOL)useTestAd {
 return YES;
}

2. Double-check that your publisher id is set correctly and try again.

3. For the SDK, check whether the included sample projects displays ads. If it does, you might want to check that you are attaching the AdMobView to your view hierarchy, that the frame is set correctly, and that the delegate is set correctly.

4. Also, the very first request to the AdMob adservers can sometimes take a while; if all else fails, wait 5-10 minutes and try again.

5. If you're using interface builder, adding ads in a nib other than MainWindow.xib, and you're not getting any delegate messages at all, you might need to add [self retain] to the -awakeFromNib method in your AdViewController (see http://developer.apple.com/releasenotes/DeveloperTools/RN-InterfaceBuilder/index.html#//apple_ref/doc/uid/TP40001016-SW5 section "FAQ: What is different about NIB loading in a Cocoa Touch application?").

6. If this still doesn't work, email support@admob.com.


Q: Sometimes, I won't see an ad, and a blank space will appear in my app. How do I fix/prevent that?

A: It is likely that the following is happening: you're placing the AdMob AdView where you'd like to show an ad, the AdMob AdViewController requests an ad and is informed an ad is not available, and then the AdMob AdView is released, leaving a blank space where the view used to be. You can fix this in two ways:

1. Place a background behind the AdMob View so, in case no ad is shown, something else is shown to the user, such as a house ad, or a background that blends in to the rest of your app.

2. Integrate via a tableview so there will be no "empty space" from an unfilled ad request, and the space reserved for that ad position will be removed.


Q: I'm getting a crash when I release my delegate. How do I fix/prevent that?

A: To prevent retain cycles, the AdMobView does not retain its delegate. However, the delegate must not be dealloc'd until after the AdMobView is dealloc'd, as it is required for proper functioning of the AdMobView. When reasonable to do so, we recommend using your application delegate as your AdMob delegate. You should also be sure that you have the latest version of the AdMob SDK, as some early versions did not handle this scenario correctly.


Q: I'm trying to build using the AdMob SDK for iPhone 3.0. Building for the simulator works fine, but when building for the actual device, I'm getting this warning message:

   libAdMobSimulator.a, file is not of required architecture

A: This is an expected warning, and you should not see any ill effects from the warning. The reason for this warning message is specific to the static library format used by the AdMob SDK. The simulator runs on an x86 architecture, while the device uses an ARM architecture. Because of this difference, the libraries for the simulator and the actual device are built separately.

Personal tools
Documentation