This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
tutorials:using_iads_with_sparrow [2013/05/30 12:52] – daniel | tutorials: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 ===== | ||
+ | |||
+ | [[/ | ||
+ | [[http:// | ||
+ | |||
+ | 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:// | ||
+ | </ | ||
+ | |||
+ | == 1. Create a new Sparrow project using the scaffold == | ||
+ | |||
+ | This tutorial is built on the most recent scaffold application, | ||
+ | |||
+ | == 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, | ||
+ | <code objc> | ||
+ | #import < | ||
+ | |||
+ | @interface ViewController : UIViewController < | ||
+ | </ | ||
+ | |||
+ | \\ Next, we need to add a variable to hold the iAd banner. In ViewControler.m, | ||
+ | <code objc> | ||
+ | @implementation ViewController { | ||
+ | ADBannerView *_adView; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ Now set up the banner in the init method: | ||
+ | <code objc> | ||
+ | - (id)initWithSparrowView: | ||
+ | { | ||
+ | if ((self = [super init])) | ||
+ | { | ||
+ | // ... | ||
+ | | ||
+ | _adView = [[ADBannerView alloc] initWithFrame: | ||
+ | _adView.requiredContentSizeIdentifiers = [NSSet setWithObjects: | ||
+ | _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: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ We need to implement the banner delegate methods in ViewController.m to tell your Sparrow game about iAd display: | ||
+ | <code objc> | ||
+ | - (void)bannerViewDidLoadAd: | ||
+ | banner.hidden = NO; | ||
+ | Game *game = (Game *)[mSparrowView.stage childAtIndex: | ||
+ | [game adVisible: | ||
+ | } | ||
+ | |||
+ | - (void)bannerView: | ||
+ | banner.hidden = YES; | ||
+ | Game *game = (Game *)[mSparrowView.stage childAtIndex: | ||
+ | [game adVisible: | ||
+ | } | ||
+ | |||
+ | - (BOOL)bannerViewActionShouldBegin: | ||
+ | [mSparrowView stop]; | ||
+ | Game *game = (Game *)[mSparrowView.stage childAtIndex: | ||
+ | [game userInteractingWithAd: | ||
+ | return YES; | ||
+ | } | ||
+ | |||
+ | - (void)bannerViewActionDidFinish: | ||
+ | [mSparrowView start]; | ||
+ | Game *game = (Game *)[mSparrowView.stage childAtIndex: | ||
+ | [game userInteractingWithAd: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ The last change in ViewController.m is to handle orientation: | ||
+ | <code objc> | ||
+ | - (void)willAnimateRotationToInterfaceOrientation: | ||
+ | | ||
+ | { | ||
+ | // ... | ||
+ | | ||
+ | _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: | ||
+ | <code objc> | ||
+ | - (void)adVisible: | ||
+ | - (void)userInteractingWithAd: | ||
+ | </ | ||
+ | |||
+ | Implement these methods in Game.m: | ||
+ | <code objc> | ||
+ | - (void)adVisible: | ||
+ | if (visible) { | ||
+ | // move game elements to fit around ad | ||
+ | } else { | ||
+ | // move game elements to fill screen or display your own ad | ||
+ | } | ||
+ | } | ||
+ | |||
+ | - (void)userInteractingWithAd: | ||
+ | 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. | ||
+ | \\ [[http:// |