Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
tutorials:using_iads_with_sparrow [2013/05/30 12:52] danieltutorials:using_iads_with_sparrow [2016/01/12 20:40] (current) – [Using iAds with Sparrow] 86.24.198.50
Line 1: Line 1:
  
 +===== Using iAds with Sparrow =====
 +
 +[[/users/brian/start|My Wiki Page]] \\
 +[[http://forum.sparrow-framework.org/topic/tutorial-using-iads-with-sparrow|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.
 +
 +<note warning>
 +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
 +</note>
 +
 +== 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:
 +<code objc>
 +#import <iAd/iAd.h>
 +
 +@interface ViewController : UIViewController <ADBannerViewDelegate>
 +</code>
 +
 +\\ Next, we need to add a variable to hold the iAd banner. In ViewControler.m, change @implementation to look like this:
 +<code objc>
 +@implementation ViewController {
 +    ADBannerView *_adView;
 +}
 +</code>
 +
 +\\ Now set up the banner in the init method:
 +<code objc>
 +- (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;
 +}
 +</code>
 +
 +\\ We need to implement the banner delegate methods in ViewController.m to tell your Sparrow game about iAd display:
 +<code objc>
 +- (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];
 +}
 +</code>
 +
 +\\ The last change in ViewController.m is to handle orientation:
 +<code objc>
 +- (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;
 +    */
 +}
 +</code>
 +== 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:
 +<code objc>
 +- (void)adVisible:(BOOL)visible;
 +- (void)userInteractingWithAd:(BOOL)interacting;
 +</code>
 +
 +Implement these methods in Game.m:
 +<code objc>
 +- (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.
 +    }
 +}
 +</code>
 +
 +== 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.
 +\\ [[http://cl.ly/000F3M2T0c0I|Sample Project]]
  tutorials/using_iads_with_sparrow.txt · Last modified: 2016/01/12 20:40 by 86.24.198.50
 
Powered by DokuWiki