Translate

Tuesday, April 10, 2012

Implement iAds into your application

iAds are pretty cool to work with, and in todays session that's exactly what we are going to see.


What are iAd: iAd is a mobile advertising platform developed by Apple Inc. for its iPhone, iPod Touch, and iPad line of mobile devices allowing third-party developers to directly embed advertisements into their applications and you can make some revenu out of it.


Here's apples developer documentation on iAD Framework


How to add iAd in the release version: All the technical part is handled by apple in this link.


How to use iAd: Read the below blog silly


Here's a view at the final output that you will get








Step 1: Open Xcode and create an empty application with an XIB file and don't drag and drop anything into the XIB for now let it be empty.


Step 2: Add the iAd Framework into your application, here's what you have to do Select the XcodeProject File(one with the blue color) ->Target -> Build Phases -> Expand Link Binary -> Hit the plus button and add the iAdFramework






Step 3: Now its time to add some code, before coding we need to keep in mind that the ads are coming from the internet so their can be a senario where you don't have the internet so in that case hide the iAd view don't display it.
Don't worry to do this kind of implementation apple has already provided you with some delegate methods in which all you iOS developers can write code to hide the iAdview.


a) Lets create the iAdView: Import the AdBannerView in the .h file of your view controller



#import
#import

@interface MyiADViewController : UIViewController
{
ADBannerView *iadView;
BOOL ismyAdVisible;
}

//using it for animating the iADBannerView
@property (nonatomic,assign) BOOL ismyAdVisible;

- (void)createTheAdBannerView;

@end



Code Explanation: I am creating an object of the AdBanner class where you will be viewing the ads also the boolean variable is for the time when the network voids and you have to hide the Banner view.


The function createTheAdBannerView will create the Banner view and make it ready for the display.


And since we will be using the delegate methods of the ADBannerView class i have implemented the protocol for the same.


Let's have a look at the createTheAdBannerView function



- (void)createTheAdBannerView{

if (NSClassFromString(@"ADBannerView")) {
//making it live goes here
iadView = [[ADBannerView alloc]initWithFrame:CGRectZero];
iadView.frame = CGRectOffset(iadView.frame, 0, -50);
iadView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
iadView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
[iadView setDelegate:self];
self.ismyAdVisible = NO;
[self.view addSubview:iadView];

}
}

Code Explanation: First we check if the ADBannerView is present in the current SDK or not and if not then probably nothing will be displayed.


The second line of code will allocate the memory for the ADBannerView


Since we don't want to see the banner view on launch i am setting the frame accordingly.


The requiredContentSizeIdentifiers property will set the size for the ads that will appear in the ADBannerView


The currentContentSizeIdentifier property will set the size of the ADBannerView


And finally am setting the delegate to self and setting the boolean property to NO and after all the hard work is done am making the ADBannerView the subview of the current view.


Step 4: Now when the above code is set and in place lets code for the delegate methods, there are two delegate methods which we will be using the one to show the ad and the other to hide it when their is no network.



- (void)bannerViewDidLoadAd:(ADBannerView *)banner{

if (!self.ismyAdVisible) {
[UIView beginAnimations:@"animateAdBanner" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 70);
[UIView commitAnimations];
self.ismyAdVisible = YES;
}
}



Code Explanation: Am animating it to display the AdbannerView



- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{

if (self.ismyAdVisible) {
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.ismyAdVisible = NO;
}
}

Code Explanation: I am hiding it when their is no internet with some simple animation.

Step 5: Select the AppDelegate.m file to add the viewcontrollers view inside the iPhone window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
MyiADViewController *obj = [[MyiADViewController alloc]initWithNibName:@"MyiADViewController" bundle:nil];
[self.window addSubview:obj.view];
[self.window makeKeyAndVisible];
return YES;
}



Hit the run button and when you do you will get the output as below






And when you click the Test Advertisement probably you should see the the other view given below confirming that iAD is ready to be used in your project






Hope that you have understood the steps involved in using the iAd Framework into your project, in case of any questions or queries kindly mail me or enter you queries as a comment and i will get back to you.


The demo won't work if you have issues with you internet so if the Ads don't show up in the first place then check your net connection and try again


You can download the source file link : https://github.com/downloads/KRadix/CommonUtility/iADPoc.zip

No comments:

Post a Comment