Google AdMob Ads iOS Fundamentals

来源:互联网 发布:基金筛选软件 编辑:程序博客网 时间:2024/06/01 18:01

转自:https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals#result


Google AdMob Ads iOS Fundamentals

  1. Overview
  2. Requirements
  3. Incorporating the SDK
  4. Adding a GADBannerView
  5. The Result
  6. What's Next?

Overview

Google AdMob Ads banners use a small portion of the screen to entice users to "click through" to a richer, full-screen experience such as a website or app store page.

To display Google AdMob Ads banners in your iOS app, simply incorporate the SDK into your Xcode project and add a GADBannerView to your UI.

For the purposes of this tutorial, the project shown will be named BannerExample. When going through the exercises, simply replace BannerExample with your project's name in the instructions.

Requirements

The Google AdMob Ads SDK for iOS requires iOS version 2.x or later (3.0 minimum for displaying ads) as well as XCode 4.2 or later.

Incorporating the SDK

The decompressed SDK consists of six Objective-C headers, a runtime library and a README.

1. Right-click on your project in Xcode, choose Add Files to "BannerExample"...

  

2. ...and select everything from the SDK except the README.

  
3. The SDK library references four iOS development frameworks which may not already be part of your project:
  • AudioToolbox
  • MessageUI
  • SystemConfiguration
  • CoreGraphics
To add these frameworks, double-click the BannerExample project name. Open the Link Binary With Libraries dropdown under theBuild Phases tab. Add the frameworks from the iOS SDK using the + button that becomes visible.
  

You should now be able to rebuild your project without any errors.

Adding a GADBannerView

iOS apps are composed of UIView objects, Objective-C instances the user sees as text areas, buttons and other controls. GADBannerView is simply aUIView subclass displaying small HTML5 ads that respond to user touch.

Like any UIView, a GADBannerView is easy to create in code.

The seven lines of code it takes to add a banner:

  • Import GADBannerView.h
  • Declare a GADBannerView instance in your app’s UIViewController
  • Create it
  • Set the ad’s unit ID – your AdMob Publisher ID
  • Set the "root view controller"
  • Add the view to the UI
  • Load it with an ad

The best place to do all this is in your app’s UIViewController.

// BannerExampleViewController.h// Import GADBannerView’s definition from the SDK#import "GADBannerView.h"@interface BannerExampleViewController : UIViewController {  // Declare one as an instance variable  GADBannerView *bannerView_;}@end

The following performs banner setup in the view controller’s viewDidLoad initialization hook.

// BannerExampleViewController.m#import "BannerExampleViewController.h"@implementation BannerExampleViewController- (void)viewDidLoad {  [super viewDidLoad];  // Create a view of the standard size at the bottom of the screen.  bannerView_ = [[GADBannerView alloc]                   initWithFrame:CGRectMake(0.0,                                            self.view.frame.size.height -                                            GAD_SIZE_320x50.height,                                            GAD_SIZE_320x50.width,                                            GAD_SIZE_320x50.height)];  // Specify the ad's "unit identifier." This is your AdMob Publisher ID.  bannerView_.adUnitID = MY_BANNER_UNIT_ID;  // Let the runtime know which UIViewController to restore after taking  // the user wherever the ad goes and add it to the view hierarchy.  bannerView_.rootViewController = self;  [self.view addSubview:bannerView_];  // Initiate a generic request to load it with an ad.  [bannerView_ loadRequest:[GADRequest request]];}- (void)viewDidUnload {  [bannerView_ release];}- (void)dealloc {  [super dealloc];}@end

Warning: Make sure you're in test mode during development to avoid being disabled for clicking your own ads. See the Best Practices guide for more details on enabling test ads.

You can download an example project containing this code here.

The Result

The outcome of either technique should be a banner at the bottom of your app:

Note: The very first time AdMob sees your publisher ID it may take up to two minutes to receive an ad. This initial two minute lag will recur every time the app goes unused for 24 hours. Refer to the Best Practices section if you would like to request test ads to make sure that your request code is working correctly.

Warning: All new iPad and iPhone apps created after October 14, 2011 will require an AdMob SDK that was released on or after March 15, 2011. This corresponds to version 4.0.2+ for iOS. If you downloaded the library from our official download site, then you're already set. Otherwise you may have an old version of the AdMob SDK that was released prior to March 15, 2011, and your new app will not receive any ad impressions until you update your SDK.

What's Next?

The Intermediate guide explains banner configuration in more detail.