Thursday, March 7, 2013

iAd in iPhone app

This is a short blog post on integrating iAd feature into your iPhone application. So, for people aiming at money making from their applications have got their way either by putting a price tag on their app or integrating iAd framework. IOS 4 and later started supporting this feature through which user's application permits apple to push advertisements into the application and earn out from it. Apple pays 70% of what they get from the Add companies that pushes their adds into the application's addbanner.

Before to put your hands on keyboard to code, you have got something to do with your Apple account. Thats nothing but enabling iAd Network by filling up your contacts, Tax and Banking informations.
Once you are done with the above mentioned you can start with app development.
Reference the iAd framework:
For older versions of xCode you can Right click on the framework folder and select Add Exsisting framework>iAd framework and add it to application else you can open our buildphases expand Link Binary With Libraries tab and click on the plus symbol to open the existing framework list then add iAd framework to application.

You have got less to do with the designing part. If you wish using the IBOutlet just drag that and drop to the view where u would like to have a addbanner. If not you can just create a addbannerView via code as-well.

Drag and drop the AddBannerView IBOutlet into your view controller and so in your .h file you will have to declare the IBOutlet for instance adbannerView and map it to the control. In addition declare a boolean variable to store the banner's visible status. Add another subview (contentView) to the view to hold the entire contends to manage the appearance of the contents when the ad view scrolls into the screen whenever an ad is available.

Naturally your header file should appear as below

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface ViewController : UIViewController<ADBannerViewDelegate>
@property (nonatomic,assign) BOOL bannerIsVisible;
@property(nonatomic,retain) IBOutlet ADBannerView *adbannerView;
@property(nonatomic,retain)IBOutlet UIView *contentView;
@end

You have to allocate the bannerview and added it to the subview. To do this add the following codes to the viewDidLoad in implementation file.
Note: The content size differs from the below for landscapeview.

adbannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
[adbannerView setRequiredContentSizeIdentifiers:[NSSet setWithObject: ADBannerContentSizeIdentifier320x50]];
[adbannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[adbannerView setFrame:CGRectOffset([adbannerView frame], 0, -50)];
[self.view addSubview:adbannerView];

Now you have to add the following two delegates to the implementation file to handle the display of adds in the banner.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
self.bannerIsVisible = YES;
[self setAdView:[UIDevice currentDevice].orientation];
}
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
self.bannerIsVisible = NO;
[self setAdView:[UIDevice currentDevice].orientation];
}
}

The function setAdView here is used to set the frame for iAd and display it when an add is available.

- (void)setAdView:(UIInterfaceOrientation)toInterfaceOrientation {
if (adbannerView != nil) {
[adbannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[UIView beginAnimations:@"fixupViews" context:nil];
if (self.bannerIsVisible) {
CGRect adBannerViewFrame = [adbannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 0;
[adbannerView setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = 50;
contentViewFrame.size.height = self.view.frame.size.height -50;
_contentView.frame = contentViewFrame;
} else {
CGRect adBannerViewFrame = [adbannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = -50;
[adbannerView setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = 0;
contentViewFrame.size.height = self.view.frame.size.height;
_contentView.frame = contentViewFrame;
}
[UIView commitAnimations];
}
}


If your application need to support landscape orientation you can add the  ADBannerContentSizeIdentifier480x32 for landscape with an if condition that check for the orientation. This is all what you have to do as the major part of development. Build and run the application. If add is available the banner will scroll in to the screen and rest of contents will shrink in the contentview.
You can download the sample app here