Using iAds with Sparrow

My Wiki Page
Tutorial Forum Post

iAds are a great way to make some money off the free version of your application. I have seen a couple people in the forums wanting to do ads and I was also curious about it. After playing around with iAds, I have successfully integrated them with Sparrow at the top or bottom of the screen in all orientations and on all devices.

This tutorial is for Sparrow 1.3; in Sparrow 2.x, the whole process is much simpler. FIXME

Fixed in post: http://forum.sparrow-framework.org/topic/iads-on-sparrow-2

1. Create a new Sparrow project using the scaffold

This tutorial is built on the most recent scaffold application, iOS 5, and ARC. The setup will be different if you are not using the scaffold.

2. Add iAd.framework to your project
3. Edit ViewController.h and .m

ViewController is the core part of UIKit support in the new scaffold. The benefit of using it is that since it holds the Sparrow view, it can listen and dispatch events through the stage.


In ViewController.h, import the iAd framework and add the delegate protocol:

#import <iAd/iAd.h>
 
@interface ViewController : UIViewController <ADBannerViewDelegate>


Next, we need to add a variable to hold the iAd banner. In ViewControler.m, change @implementation to look like this:

@implementation ViewController {
    ADBannerView *_adView;
}


Now set up the banner in the init method:

- (id)initWithSparrowView:(SPView *)sparrowView
{
    if ((self = [super init]))
    {
        // ...
 
        _adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
        _adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
        _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        _adView.delegate = self;
 
        #warning uncomment these lines if you want ads at the bottom of the screen
        /*
        CGRect adViewFrame = _adView.frame;
        adViewFrame.origin.y = self.view.frame.size.height-_adView.frame.size.height;
        _adView.frame = adViewFrame;
        */
 
        [self.view addSubview:_adView];
    }
    return self;
}


We need to implement the banner delegate methods in ViewController.m to tell your Sparrow game about iAd display:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    banner.hidden = NO;
    Game *game = (Game *)[mSparrowView.stage childAtIndex:0];
    [game adVisible:YES];
}
 
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    banner.hidden = YES;
    Game *game = (Game *)[mSparrowView.stage childAtIndex:0];
    [game adVisible:NO];
}
 
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    [mSparrowView stop];
    Game *game = (Game *)[mSparrowView.stage childAtIndex:0];
    [game userInteractingWithAd:YES];
    return YES;
}
 
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
    [mSparrowView start];
    Game *game = (Game *)[mSparrowView.stage childAtIndex:0];
    [game userInteractingWithAd:NO];
}


The last change in ViewController.m is to handle orientation:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    // ...
 
    _adView.currentContentSizeIdentifier = UIInterfaceOrientationIsPortrait(interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
    #warning uncomment these lines if you want ads at the bottom of the screen
    /*
    CGRect adViewFrame = _adView.frame;
    adViewFrame.origin.y = UIInterfaceOrientationIsPortrait(interfaceOrientation) ? self.view.frame.size.height-_adView.frame.size.height : self.view.frame.size.width-_adView.frame.size.height;
    _adView.frame = adViewFrame;
    */
}
4. Edit Game.h and .m

Above, we added the delegate methods for ADBannerView to ViewController.m. We wrote methods to communicate with the Game class that don't exist yet. Now we need to write those methods.
Add these method stubs in Game.h:

- (void)adVisible:(BOOL)visible;
- (void)userInteractingWithAd:(BOOL)interacting;

Implement these methods in Game.m:

- (void)adVisible:(BOOL)visible {
    if (visible) {
        // move game elements to fit around ad
    } else {
        // move game elements to fill screen or display your own ad
    }
}
 
- (void)userInteractingWithAd:(BOOL)interacting {
    if (interacting) {
        // pause sounds etc.
    } else {
        // resume sounds etc.
    }
}
5. You're done!

Make sure that you have set up banking info for iAds in iTunes Connect.
Here is a sample project. I have even added an example of displaying your own ad when the iAd request isn't filled.
Sample Project

  tutorials/using_iads_with_sparrow.txt · Last modified: 2016/01/12 20:40 by 86.24.198.50
 
Powered by DokuWiki