The document discusses adding iAd and Mobclix advertisements to a Core Data Books sample application from Apple. It provides code snippets for integrating the iAd and Mobclix frameworks, creating banner views, modifying views to accommodate banners, and implementing delegate methods to handle loading and displaying ads. It also covers strategies for prioritizing iAd over Mobclix and handling ad display in landscape orientation.
UiPath Test Automation using UiPath Test Suite series, part 3
iphonedevcon 2010: Cooking with iAd
1. cooking With Iad
JOnathan Saggau / Noah Gift
Tuesday, September 28, 2010
2. The Talk in a Nutshell
Tuesday, September 28, 2010
3. Agenda
• iAd Overview
• Mobclix and iAd manual Integration
• Integrating iAds into an app: Apple’s
Core Data Books sample
• Demos and other neat stuff
Tuesday, September 28, 2010
4. Ad Delivery Strategy
• Use iAD when it is available
• Higher CPM
• Better Looking
• Use mobclix otherwise
• Internationally
• on < iOS 4.0 (read: orig. iPhone)
Tuesday, September 28, 2010
5. Grab The Code
• hg clone http://bitbucket.org/
jonmarimba/iadcoredatabooks
Tuesday, September 28, 2010
6. Core Data Books From
apple
hg update --rev 1 Show default
Tuesday, September 28, 2010
7. Adding iAd to Core Data Books
Adding iAd Framework
• Adding the
iAd
Framework
hg update --rev 1
Tuesday, September 28, 2010
9. Adding a Banner
• Do it in code if you want to support <
iOS 4.0 (original iPhone)
-(ADBannerView *)iadBanner
{
if (!iadBanner)
{
Class bannerClass = NSClassFromString(@"ADBannerView");
if (bannerClass) {
iadBanner = [[ADBannerView alloc] initWithFrame:CGRectZero];
[iadBanner setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
iadBanner.requiredContentSizeIdentifiers = [NSSet
setWithObjects:ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil];
[self.view addSubview:iadBanner];
CGRect viewFrame = [[self view] frame];
CGFloat width = viewFrame.size.width;
[iadBanner setFrame:CGRectMake(0, self.tableView.frame.size.height,
width, width == 480. ? 32. : 50.)];
[iadBanner setDelegate:self];
}
}
return iadBanner;
}
Tuesday, September 28, 2010
10. iAD Delegate Protocol
#pragma mark iAD Delegate
// This method is invoked each time a banner loads a new advertisement. Once a banner has loaded an
ad,
// it will display that ad until another ad is available. The delegate might implement this method
if
// it wished to defer placing the banner in a view hierarchy until the banner has content to
display.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner;
{
[self showIAdBanner:YES];
}
// This method will be invoked when an error has occurred attempting to get advertisement content.
// Possible error codes defined as constants in ADManager.h.
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error;
{
// no iAd adds, so try mobclix
[self hideIAdBanner:YES];
}
// This message will be sent when the user taps on the banner and some action is to be taken.
// Actions either display full screen content in a modal session or take the user to a different
// application. The delegate may return NO to block the action from taking place, but this
// should be avoided if possible because most advertisements pay significantly more when
// the action takes place and, over the longer term, repeatedly blocking actions will
// decrease the ad inventory available to the application. Applications may wish to pause video,
// audio, or other animated content while the advertisement's action executes.
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave;
{
return YES;
}
// This message is sent when a modal action has completed and control is returned to the
application.
// Games, media playback, and other activities that were paused in response to the beginning
// of the action should resume at this point.
- (void)bannerViewActionDidFinish:(ADBannerView *)banner;
{
NSLog(@"bannerViewActionDidFinish");
}
Tuesday, September 28, 2010
11. Modifying the table
view’s frame when an ad
is shown
-(CGRect)frameForIAdTable:(BOOL)showingBanner
{
CGRect frameForTable = self.view.frame;
frameForTable.origin.x = frameForTable.origin.y = 0.0;
if (showingBanner) {
frameForTable.size.height -= ([self iadBanner].frame.size.height);
}
return frameForTable;
}
Tuesday, September 28, 2010
12. Showing Banner
-(void)showIAdBanner:(BOOL)animated
{
if (showingIAdBanner) {
return;
}
//resets some of the geometry
[self.view addSubview:[self iadBanner]];
[self hideIAdBanner:NO];
if (animated)
{
[UIView beginAnimations:@"showIAdBanner" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
}
CGRect frameForTable = [self frameForIAdTable:YES];
[self.tableView setFrame:frameForTable];
CGRect frameForBanner = [[self iadBanner] frame];
frameForBanner.origin.y = frameForTable.size.height;
[[self iadBanner] setFrame:frameForBanner];
showingIAdBanner = YES;
if (animated)
{
[UIView commitAnimations];
}
}
Tuesday, September 28, 2010
14. #protip
• killall adlibd
• ^ for when the simulator doesn’t seem
to want to serve up ads.
Tuesday, September 28, 2010
15. What happens when
There is no iAD?
• Internationally (iAD is US only
currently)
• Fill rate is meh (ish).
• Not available on original phone.
• You’ll want a supplemental ad service.
hg update
Tuesday, September 28, 2010
16. Mobclix
• http://mobclix.com/
• Largest Mobile Ad Exchange
• Advertisers Compete for eyeballs
• Decent dough
hg update --rev 1
Tuesday, September 28, 2010
17. Adding MobClix to Core Data
Books
• Add the Static Library
• Other Linker Flags
• -all_load
• -ObjC
hg update
Tuesday, September 28, 2010
21. New stuff in iAD
delegate methods
#pragma mark iAD Delegate
// This method is invoked each time a banner loads a new advertisement. Once a banner has loaded an ad,
// it will display that ad until another ad is available. The delegate might implement this method if
// it wished to defer placing the banner in a view hierarchy until the banner has content to display.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner;
{
[self showIAdBanner:YES];
//give full priority to iAd and get rid of the MobClix ad
[self hideMobAdBanner:YES];
[self.mobAdBanner cancelAd];
[self.mobAdBanner pauseAdAutoRefresh];
}
// This method will be invoked when an error has occurred attempting to get advertisement content.
// Possible error codes defined as constants in ADManager.h.
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error;
{
// no iAd adds, so try mobclix
[self hideIAdBanner:YES];
[self.mobAdBanner resumeAdAutoRefresh];
}
Tuesday, September 28, 2010
22. Modifying the table
view’s frame when a
Mobclix ad is shown
-(CGRect)frameForMobTable:(BOOL)showingBanner
{
CGRect frameForTable = self.view.frame;
frameForTable.origin.x = frameForTable.origin.y = 0.0;
if (showingBanner) {
frameForTable.size.height -= ([self mobAdBannerHost].frame.size.height);
}
else if (showingIAdBanner)
{
frameForTable = [self frameForIAdTable:YES];
}
return frameForTable;
}
Tuesday, September 28, 2010
25. Prettification of
mobclix banners
// prettify the mobclix banner a little
[[mobAdBanner layer] setBorderColor:[[UIColor grayColor] CGColor]];
[mobAdBannerHost setBackgroundColor:[UIColor colorWithRed:199./255.
green:206/255.
blue:213/255.
alpha:1.]];
CGFloat scale = 1.0;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
}
if (scale > 1.0)
{
//iPhone 4 looks okay with border width of one, it's sort of meh on the
//lower DPI screens.
[[mobAdBanner layer] setBorderWidth:1];
[[mobAdBanner layer] setCornerRadius:4];
} else {
[[mobAdBanner layer] setBorderWidth:2];
[[mobAdBanner layer] setCornerRadius:6];
}
[mobAdBanner setClipsToBounds:YES];
// Drop shadow below table view (this is an ugly way to do it, but it's easier
// in a way given that we support 3.1.2 through 4.0 at this point.
UIImage *img = [UIImage imageNamed:@"littleShadow.png"];
CGRect shadowViewFrame = CGRectMake(0, 0, mobAdBannerHost.frame.size.width, img.size.height);
UIImageView *shadowView = [[UIImageView alloc] initWithFrame:shadowViewFrame];
[shadowView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[shadowView setImage:img];
[mobAdBannerHost addSubview:shadowView];
[mobAdBannerHost bringSubviewToFront:shadowView];
[shadowView release];
Tuesday, September 28, 2010
26. Rotatin’
• Mobclix ads look awful rotated to
landscape
• Too tall
• Too Narrow
• iAD ads look great in landscape
• Hide mobclix no matter what in
landscape
Tuesday, September 28, 2010
27. Rotatin’
(heh. Get it? Rotatin?)
//
- Ov
(B err
in O
te OL) ide
rf s
ac hou to a
// eO l
re Ret ri dAu llow
e
tu ur nta toro ori
rn n t t e
} (i Y E S i o n a t e T n t a t
nt { oI i
in erf for nt ons
er
t
in erf aceO supp fa oth
ce e
te a
rf ceO rien orte Or r t
ie h
ac r
eO ien tati d or nt an
at t
ri t
en ati on = ient io he
n: d
ta o
ti n = = UI atio (U efa
II u
on = I
== UI nte ns nt lt
er p
D
UI evi rfac fa ort
ce r
De c
vi eOr eOri Or ait
ie
ce i
Or ent enta nt ori
at e
ie a
nt tio tion io nta
n) ti
at n
io Lan Port on
.
nL ds ra
an ca i
ds pe t |
ca R
pe igh |
Le t
ft |
); |
Tuesday, September 28, 2010
28. Rotatin’
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
[self iadBanner].currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
if(showingMobAdBanner)
{
//Admob ads look awful in landscape. They're too big.
//Disable them until we can make them pretty
[self hideMobAdBanner:YES];
[self.mobAdBanner cancelAd];
[self.mobAdBanner pauseAdAutoRefresh];
}
}
else
{
[self iadBanner].currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
if(!showingIAdBanner)
{
[self.mobAdBanner resumeAdAutoRefresh];
if (mobAdHasContent)
{
[self performSelector:@selector(showMobAdBannerAnimated) withObject:nil
afterDelay:duration+1];
}
}
}
}
Tuesday, September 28, 2010