Friday 1 March 2013

iAd(2) - Integration


In your viewDidLoad or init function:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//---------------------------------------------------------------------------------
 // Add iAd banner
 //---------------------------------------------------------------------------------
 Class classAdBannerView = NSClassFromString(@"ADBannerView");
 // Check if this is old firmware that does not support iAd(ex. 3.x)
 if (classAdBannerView != nil)
 {
  // Create the iAd view and place it outside the screen(later we will move it to the right place)
  ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 480, 320, 50)];
  // Set the delegate so we can get notification
  [adView setDelegate:self];
  // Set it up in portrait mode
  adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
  // Attach it to the current view
  [[[CCDirector sharedDirector] openGLView] addSubview: adView];
 }

Delegate methods for iAd:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//---------------------------------------------------------------------------------
// Whether or not the iAd view action should begin
//---------------------------------------------------------------------------------
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    NSLog(@"iAd view begins an action");
  
    // If you allow it to be executed, then return true. Otherwise, return false
    return YES;
}
 
//---------------------------------------------------------------------------------
// Did load an ad in the ad view
//---------------------------------------------------------------------------------
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"iAd view did load an Ad");
  
    // If the banner has not shown yet
    if (!m_FlagAdBannerVisible)
    {
        // Then we do an animation to fly the banner from outside to the top of the screen
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        banner.frame = CGRectMake(0, 0, 320, 50);
        [UIView commitAnimations];
        m_FlagAdBannerVisible = YES;
    }
}
 
//---------------------------------------------------------------------------------
// If there is an error receiving the ad content
//---------------------------------------------------------------------------------
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"iAd view can not receive an Ad");
  
    // If the banner is visible
    if (m_FlagAdBannerVisible)
    {
        // Then we do an animation to fly the banner out of the screen
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        banner.frame = CGRectMake(0, -50, 320, 50);
        [UIView commitAnimations];
        m_FlagAdBannerVisible = NO;
    }
}

Add this member variable in your header file:
?
1
BOOL       m_FlagAdBannerVisible;

Pretty simple, isn't it?

No comments:

Post a Comment