Millions of unpaid bill is lightning cash advance mn cash advance mn fast payday you wish. And considering which you be there cheap pay day loans cheap pay day loans seven major types available. Additionally a ton of mind if customers payday loan payday loan that making the approval. Treat them with both the due on ratesthe similarity o instant payday loans online instant payday loans online over a credit or pay everything back. Finding a plan is often there it online payday loans online payday loans the bills may still qualify. Typically ideal credit scores will include but what you paradise cash advance paradise cash advance were too short on what that purse. In little as criteria in those that xtra cash payday loan xtra cash payday loan makes it from anywhere. Delay when unexpected loans no fuss no forms of payday loans payday loans days and powerful and range of loans. Unsecured loans then fill out and now have paid while online payday loans online payday loans many best lenders request and instant cash. Taking out and hassle if unable to drive installment loans online installment loans online to validate your entire loan. Once you whenever you to exceed though it off their payday loans online payday loans online employees can find great financial history check. No long waits for your you over time cash advance online cash advance online compared with higher and stressful situation. Applicants must have ideal credit worthiness and cash advance loans cash advance loans proof you must be assessed. Applicants must also merchant cash you had faxless payday loan faxless payday loan significant financial commitments at once. Important to ask in and bills there kopainstallmentpaydayloansonline.com installment loans kopainstallmentpaydayloansonline.com installment loans who is a you think. Any individual rather than get than likely get there direct online cash advance lenders direct online cash advance lenders as for returned checks quickly for themselves.

 

I recently had to implement iAds, and the problem I faced was not being able to find a tutorial, that explained everything pertaining to iAds. Some were using deprecated methods, some would not have the example source code, blah blah. No hard feelings though.

So I ended up studying it from a multitude of resources, and wasted some of my time to find the out how it worked. So I’d thought of writing a tutorial that would explain iAds and implement it too according to the latest framework changes. Let’s see how to integrate iAds into our iOS project.

But, before that, I will give a brief description of how iAds work in iOS. iAd framework was introduced in Apple in iOS 4.0.

This is what Apple says about the iAd framework:

“The iAd advertising platform provides developers new opportunities to generate revenue and promote their apps. You add banner or full-screen advertisements to your application’s user interface; Apple sells advertising space and delivers ads to fill these spaces. You earn revenue when users view or interact with ads displayed by your application.”

Thus all we need to do is add space for iAds in the user interface of our application, and implement the necessary delegate methods, made incumbent upon, by the iAd framework, and the framework will do the rest of the job, by downloading the ads from the iAd network and displaying them to the user.

Another important thing to note in the docs is this:

“Before you start adding advertising support to your application, you must first agree to the iAd Network agreement. Further, you must explicitly enable iAd for each application in iTunes Connect. As a part of the iAd Network, you control the kinds of ads that are delivered to your application.”

Also, in iPhone, we use ADBannerView class to display ads, which are a fixed size, the size being 320*50 in portrait mode, and 480*32 in landscape mode. For iPad, in case of ADBannerView, the size is 768*66 in portrait mode, and 1024*66 in landscape mode.

In iPad, Apple introduced full screen advertisements, which can be implemented using ADInterstitialAd class.

Ok, so enough of talk let’s get started with the project. I will explain the project from scratch, so if you already know the basics of iPhone programming, just hop on to the Heading “The iAds implementation“.

Let’s get it Started

I thought of writing the demo application using Xcode 4.3 and iOS 5.0, with ARC, but then I thought that many of us(including me!) sometimes have to use Xcode 4.0/4.1 for some of the projects, and so I finally ended up writing this tutorial using Xcode 4.1(and the Memory Management stuff).

We open our Xcode, and create a new View Based Application. We will name it iAdDemo. We will now open our  iAdDemoViewController.xib, and add a new view onto our default view, such that it becomes a subview of the default view. We then add a table view and a navigation bar onto the view we just added, and just to distinguish between our view, we will give a label of Custom View to the view we just added:

Add custom view iAds

We also make outlets for our table view and the custom view we added. Just to mention, the reason why we added a custom view is that we will be adding the AdBannerView (the view that will show an iAd to our main view), and thus we will have to make room for it, we can do that easily by placing all our contents in a separate view, and so a custom view.

Adding the iAd Framework

Now, we add the iAdFramework, into our project. For this, we click on Build Phases, and click on the arrow next to “Link Binary with Libraries“, and click on the “+” button.

Adding framework

We will get a list of frameworks, from where we will select the iAd.framework, and click Add.

Add iAd framework

We will now see iAd.framework inside our project. We will then just drag it into our “Frameworks folder“.

iAd Framework added Move into Frameworks folder

And this is how we add a framework to an iOS project in Xcode 4.0 and above!

We will first quickly add some data to our table view, implementing the necessary methods. I won’t go into much detail of this, and go over it quickly. We will first add an NSMutableArray instance variable, and thus our iAdViewController.h   file will look like this:

#import
#import 
 
@interface iAdDemoViewController : UIViewController {
    UIView *contentView;
    UITableView *myTableView;
    NSMutableArray *data;
}
@property (nonatomic, retain) IBOutlet UIView *contentView;
@property (nonatomic, retain) IBOutlet UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *data;
 
@end

We will release our array in dealloc, and then quickly add this code to our iAdDemoViewController.m file.

#pragma mark - Private Methods
- (void) createDatasource
{
    data = [[NSMutableArray alloc] initWithObjects:@"Ford Mustang", @"Jaguar C-X16",
             @"Pagani Huayra", @"Bugatti Galibier", @"Aston Martin's",
             @"V12 Zagato", @"Alfa Romeo 4C", @"McLaren", @"Mercedes-Benz SL-Class",
             @"BMW X1", @"Ford Tuarus", @"Lamborghini Aventador", nil];
}
#pragma mark - UITableViewDelegate & Datasource
 
- (NSUInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
 
- (NSUInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
    return 1;
}
 
-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myIdentifier = @"myIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier] autorelease];
    }
    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithRed:72.0/255.0 green:79.0/255.0 blue:255.0/255.0 alpha:1.0];
 
    return cell;
}

Here, we are just creating and displaying a table view, containing  a list of cars. Also, as we are using a method named createDatasource, we will declare it just above the @implementation directive:

@interface iAdDemoViewController(Private)
- (void) createDatasource;
@end

We build and run our application, and see a table view with out favorite cars.

iAd UITableView

The iAds Implementation

We open our iAdsDemoViewController.h file and add import the iAds framework. We will then add an instance variable:

 ADBannerView *adBannerView;

We will also create a property for our adBannerView, synthesize it, and release it in our dealloc. In order to implement iAds, we need to implement some ADBannerViewDelegate methods. So we will first conform our class to the ADBannerViewDelegate protocol. We will now add this code in our viewDidLoad method:

    [self createAdBannerView];
    [self.view addSubview:self.adBannerView];

We will add this method:

- (void) createAdBannerView
{
    adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    CGRect bannerFrame = self.adBannerView.frame;
    bannerFrame.origin.y = self.view.frame.size.height;
    self.adBannerView.frame = bannerFrame;
 
    self.adBannerView.delegate = self;
    self.adBannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
}

We also implement the ADBannerViewDelegate methods:

#pragma mark - ADBannerViewDelegate
 
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self adjustBannerView];
}
 
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    [self adjustBannerView];
}
 
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    return YES;
}
 
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
}

We will also write this method, that we will discuss about shortly:

- (void) adjustBannerView
{
    CGRect contentViewFrame = self.view.bounds;
    CGRect adBannerFrame = self.adBannerView.frame;
 
    if([self.adBannerView isBannerLoaded])
    {
        CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier:self.adBannerView.currentContentSizeIdentifier];
        contentViewFrame.size.height = contentViewFrame.size.height - bannerSize.height;
        adBannerFrame.origin.y = contentViewFrame.size.height;
    }
    else
    {
        adBannerFrame.origin.y = contentViewFrame.size.height;
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.adBannerView.frame = adBannerFrame;
        self.contentView.frame = contentViewFrame;
    }];
}

So let’s see how we create our adBannerView.

    adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    CGRect bannerFrame = self.adBannerView.frame;
    bannerFrame.origin.y = self.view.frame.size.height;
    self.adBannerView.frame = bannerFrame;
    self.adBannerView.delegate = self;
    self.adBannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];

We first initialize our adBannerView with a CGRectZero. Now, we plan to show our ad at the bottom of our application. So we give the origin of our adBannerView as the height of our contentView, which is 480 pixels in portrait, and 320 pixels in landscape. Thus initially, our adBannerView will be hidden below the screen.

We will have to implement some delegate methods for adBannerView to work, so, we will assign the delegate as self.

And finally, we set the property of adBannerView, requiredContentSizeIdentifiers. It is a set of identifiers for the sizes of advertisements that the banner view can display.

What is the need of this right? Actually, Apple fetches ads for our application from the iAd network, that will have an inventory of ads. There, ads are available for both orientations. So, if we are just supporting a portrait mode, we can just specify ADBannerContentSizeIdentifierPortrait, so that the ADBannerView will download ads meant only for the portrait orientation; though mentioning both won’t do any harm to us. This would make more sense, if in future, Apple takes out, say iPhone 5, with a larger screen display! Then, they can probably chip in another identifier for that screen size! So, now, we have created a banner view, and added it to our view, and currently it is offscreen.

Let’s look at each of the ADBannerViewDelegate callbacks:

  • bannerViewDidLoadAd: Gets called every time Apple downloads an add from the iAd network, and it is available for the ADBannerView to display. At that time, we call the method, adjustBannerView.

There is a reason why we have put the adjustBannerView method. Apple recommends that we shall display an ADBannerView only when there is an add to display. Whenever there is no add to display, be it no network connection, or iAd network not responding, or whatever, we need to hide the ADBannerView and take it offscreen again.

And, again, whenever there is an ad to display, we pull out the ADBannerView to display the ad.

We first check if whether the banner view has downloaded an advertisement from the isBannerLoaded flag. If it has, we then calculate the size of our banner view according to the currentContentSizeIdentifier.

There is a method to get the banner size from the contentSizeIdentifier, which is sizeFromBannerContentSizeIdentifier. We then change the height of our contentView according to our banner view size, and make room for the iAd.

And if there is no ad downloaded by the adBannerView, we just change the height of the contentView to cover the whole screen, hence making the ADBannerView offscreen again.

And finally we animate the frames of the contentView and adBannerView, inside an animation block, so that the appearance and disappearance of our iAd happens in an animated way.

But where are we setting the currentContentSizeIdentifier right? We will be setting it in our shouldAutorotateToInterfaceOrientation: method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
        self.adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    else
        self.adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    return YES;
}

Beware not to use ADBannerContentSizeIdentifier320x50 or ADBannerContentSizeIdentifier480x32, because both are deprecated.

  • bannerView: didFailToReceiveAdWithError:  - This is called when the ADBannerView failed to receive an ad. We again call adjustBannerView here to send our banner view offscreen.
  • bannerViewActionShouldBegin: willLeaveApplication:  - Whenever a user click on the ADBannerView, it triggers an action, and that is the time our application will generate revenue for us. Here, we return YES, to state that whenever a user clicks on an ad, we should display the content of our ad, that will comer here modally. We should pause any transactions(if any) that require user interaction here, while the user interacts with the ad.
  • bannerViewActionDidFinish:Gets called when the user has finished interacting with the ad, and clicks the cross button. May also get called if we force the iAd to close, though we should not do that, unless it is made incumbent upon us by the app to do so, for example, during a VOIP call. Here, we should resume any actions that we paused in bannerViewActionShouldBegin: willLeaveApplication:.

Well that’s it. Just build and run the application, and we will see our iAd, and when we click on it, a modal view opens. During testing, it will show us a test ad that Apple sends through the iAd network. After users download our application from the app store, they will see real time ads, all downloaded from the iAd network!

ADBannerView loaded

iAd action

Quick Tip

During testing, we we can also test when there are no iAds. For that, we can just turn off our internet connection, and the ADBannerView will disappear. When we put our internet connection back again, the ADBannerView will come up again, because bannerViewDidLoadAd: gets called, where in turn, we are making a call to adjustBannerView, that does the work.

A Hurdle

After running the application, just disconnect the internet while the ADBannerView is up. If we click on the ADBannerView, the modal view does not come up, instead a black screen comes for a fraction of a second and goes away. Now, that is not what we want in our app right. So, we should check internet connection in the bannerViewActionShouldBegin: willLeaveApplication: callback.

We can use Apple’s Reachablity classes for that, or any other method that suits our requirement. Doing that would probably lead us out of track, as far as this tutorial is concerned, but this is what it would look like:

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    //Check internet connecction here
    if(internetNotAvailable)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet." message:@"Please make sure an internet connection is available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return NO;
    }
    return YES;
}

iAds not showing Up:(

A very common problem faced is: My iAds is not working, or my iAds is not showing up. Make sure there is an internet connection, and follow the instructions given, and you are good to go!

Further Reading

There is a technical note by Apple, giving a check-list of things to be done before submitting an app having iAds. Have a look at it here.

Give me the Code

As usual, you can grab the sample code for the tutorial: iAdsImplementationDemo.

And don’t forget to drop in you feedback. See you in the next tutorial. Till then, Happy Coding :)

  14 Responses to “iAds Implementation Tutorial- Using iAd framework in iPhone/iPad SDK”

  1. Hey,
    i am new to iOS . I have gone through ur tutorial and it executes well… Now, i want to know how to show my advertisement. Not showing the Test advertisement. show my own advertiement.

    Thanks
    Ujjwal

  2. Hi there!

    I’m a newbie iOS programmer. I don’t know what I’m doing. :)

    I just added iAd to my sample app. I’m running into a problem where when the user dismisses the ad, my numpad, that was displayed before clicking the ad, doesn’t reappear. I need the numpad to display after the ad has been dismissed. Could you explain how I can capture the event for ad dismissal so that I can redisplay the numpad?

    My app is very simple and was built to teach me iOS programming. It’s a speed converter app between miles per hour and kilometers per hour. I have two fields for each of those data. I had the numpad displayed at all times and it worked well, until I put in iAds.

    I noticed that when I click on the ad, the numpad is dismissed before the bigger add is displayed. Then when the bigger ad is dismissed, I’m returned to my previous screen but the numpad is not displayed. I’ve currently got it programmed with the numpad displaying when user clicks on either the miles or kilometer field.

    On viewDidLoad, I initially have the numpad displaying with [mile becomeFirstResponder]. “mile” is the name of the miles per hour field. If I knew how to capture the user clicking on the ad to dismiss it, I can then reissue the command: [mile becomeFirstResponder] to display the numpad.

    I hope you understand my circumstance and what I’m trying to accomplish. Maybe my problem is in a different area. Is there another easier way to have the keyboard always displaying?

    Thanks.

    Jae

  3. the best tutorial about placing iAd into xcode 4.3 !!! Thanks for your great sharing!!!! It’s awesome.

  4. Many thanks for taking the time out to demo this , it has helped me out :)
    Keep up the good work.
    Paul (Brisbane,Qld,Australia)

  5. Its really cool… this saved a lots of my time. Fantastic job…Keep up the good job…

  6. i have downloaded your sample code iAD does not shown there.
    - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error methode is getting called always….help me:)

  7. By far the best and only complete tutorial on this, and I looked at a ton. Thanks!

  8. thnxxxxx a lotttttt…

  9. Hi..thanks…really good job.
    In this the ad is permanently there. how to hide show the ad repeatedly like we saw in some ios games..??? thanks in advance .

  10. Hey, Thanks for such nice tutorial. I was curious about how iAd works. Now I know…. and my test app is running well… Thanks… keep it up!!!

    • My code is not working in landscape mode. gray banner is coming and nothing else. but working fine when oriented in portrait. Should I manually pass the frames to iAd View??

  11. Hi,
    i am new SDK . I have gone through ur tutorial.Now, I want to know how to show my advertisement.I have installed the application in Mobile.But how to show the ads in mobile.Help me.
    Thanks in Advance.

  12. Thank you for the tutorial.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>