转自:https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals#result
Google AdMob Ads iOS Fundamentals
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.
- 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 =