How to use IAD to show banner ads in apps

Source: Internet
Author: User

While you're probably about to start developing the next Super app, all your planning and design is ready, but there's one thing you might not think about--how do you monetize it? There are two options, either to make it a paid app, for those potential users to download for a fee, or to make it a free app, add some ads, and earn revenue from your ads.   Today's trend suggests that users are more likely to download free apps instead of charging apps. They only buy apps that are really worth buying, or it's super-famous and get a good rating. However, if you decide to make your app free, you can integrate your ads within it. To do this, you have different service options to display ads, one of which is an IAD network provided by Apple. Without a doubt, you've even seen from the title of the tutorial that today's topic is about how to use iad ads, but starters see some introductory stuff before you move on.   It is important to determine whether your app is free or paid before the actual development begins, as it will directly affect your job. For paid apps, there's no need to make any special considerations, but the free app needs to consider where to display the ads in the app and set them up in the appropriate interface. When it comes to location, ads should be placed at the bottom or top of the view controller. If your app does not include tab bar, the ad should be placed at the bottom of the screen, or it should be placed at the top of the screen. Please note that if you show ads elsewhere in the view, you will be in violation of human Interface guidelines and Apple would reject your app.   In addition to all of the above provisions on advertising location, there is a set of rules you should consider. First, don't expect ads to be always available. A variety of network or server-side issues can cause problems for your ads, and you'll also want to consider the possibility of unsightly whitespace when no ads appear. It's a good choice to show another meaningful view when there's no ads. In fact, it all depends on whether the design of your application should show another view, not the ad banner display or not.   In addition, banner ads should not always be obscured by other view controllers. You have to make sure that they will appear on the most visible view controller, and when users interact with the application, they spend more time on it, which provides the greatest possibility for your ads to gain user clicks and profitability.   As for revenue, Apple and developers will benefit when users click Ads. In fact, developers earn 70% of their revenue and the remaining 30% are owned by Apple, which provides IAD network services. The same partitioning applies to paid applications.   It is very common that independent developers and companies follow another approach to creating two versions of the same application. First version, charge, full feature. The second version is free, but it does not packFeatures that include all paid version features. The free version, also known as Lite version, can have ads that you may earn from these ads later, and if users like the app and buy a paid version, you can earn extra income. Keep in mind that you should never add ads to your paid apps. Even if Apple allows such a thing to happen, I have serious doubts about it, and you may get very bad criticism and scoring, because users don't spend money on advertising.   In the next section of this tutorial, we will, as always, implement a sample application, and through this you will learn how to integrate Apple's advertising services into your project. Even if you're unfamiliar with it, you'll find that using ADS is a super-simple process. So, please read on and learn how this new great project works!   App OverviewAs always, our tutorial will be based on a sample application that was created. Before we start developing, we'll look at how to integrate IAD ads and display ad banners in a few steps. More specifically, we will do the following: 1. We will add a banner ad at the bottom of the view. This will be the container that the ad will display when it is available. If no ads are available, we'll hide them until we have the content displayed. 2. In general, when you click an ad, a full-screen modal view pops up, overwriting everything on the screen. In this case, any critical or important tasks regarding visual updates must be paused or stopped and restarted after the ad is closed. To demonstrate how to manage this process, we will use a timer count to calculate the number of seconds the user views the default view controller and use a label to show the value of the timer that the information contains. When the ad is clicked, we will stop counting and we will restart the count after it is closed again. In addition, in this tutorial, we will use an additional framework, the IAD framework. Without it, it's impossible to show and process iad ads. Illustrates the sample application used in this tutorial: Project CreationLet's start by creating a new project for the sample application. Launch Xcode and select the appropriate option in the Welcome window, such as: In the wizard that appears, select the single view application template according to the section's iOS Application category (application): Click Next, in the product Iaddemo as the name of the project as set in the Name field. Also, make sure that the Device menu option is selected for iphone. The other settings remain the same, and continue: In the last step of the boot, select a directory to save the project, and then click Create. Interface ConfigurationNow that we've built an empty project, let's start working on the application interface. Click Main.storyboard file to show interface Builder. What you see below is all the sub-views you should add as well as their properties, so the final result of our work in this section is similar to the one shown earlier in the program diagram. Note that you can skip this part of the description sub-view if you just want to focus on the banner ad. UILabel1. Frame:x=20, y=60, width=280, height=402. Text:iad Integration3. Font:avenir Black, 24.0pt4. Text Alignment:center UILabel1. Frame:x=20, y=108, width=280, height=212. Text:by Appcoda3. Font:avenir Medium Oblique, 17.0pt4. Text Alignment:center5. Text color:red=255, green=128, blue=0 UILabel1. Frame:x=20, y=360, width=280, height=402. Text:none3. Font:noteworthy Light, 15.0pt4. Text Alignment:center5. Text Color:light Gray UILabel1. Frame:x=0, y=518, width=320, height=502. Text:ad banner would appear here3. Font:avenir Medium, 14.0pt4. Text Alignment:center5. Text Color:white6. Background color:red=255, green=128, blue=0 Adbannerview1. Frame:x=0, y=518, width=320, height=50 join all of the above sub-views and set their properties, your interface should look like this: it is therefore necessary to create and concatenate two Iboutlet properties now. The first one connects to the banner ad view, and the second connects to a label that does not contain text. For this tab, we will display the value of the time counter later. Open the ViewController.h file and add the following two lines:
 
   
  
  1. @Interface Viewcontroller:uiviewcontroller <ADBannerViewDelegate>
  2. @property (Weak, nonatomic) Iboutlet Adbannerview *adbanner;
  3. @property (Weak, nonatomic) Iboutlet UILabel *lbltimermessage;
  4. @end
Xcode will issue an error and some warnings at this point. That's because we haven't joined the IAD framework yet. We did not include the corresponding library in the file. Let's fix it and let's start joining the framework. In the Item navigator (Project Navigator), click the Iaddemo project, and then click the General tab. Next, at the bottom of the screen, in the linked frameworks and Libraries section, click the Plus icon to add a new frame. In the new window that appears, start typing: "IAd Framework", and Xcode will immediately display it. Select it and then click the Add button: Now, go back to the ViewController.h header file and import the next library:
 
   
  
  1. #import <iAd/iAd.h>
At this point, no more errors and warnings will be displayed in Xcode. Finally, our class must take the banner ad protocol so that we can access all the necessary delegate methods. Modify the interface header row as follows:
 
   
  
  1. @Interface Viewcontroller:uiviewcontroller <ADBannerViewDelegate>
In the final step, these two Iboutlet properties must be connected to the corresponding child view. Open the Main.storyboard file and connect the Adbanner property to the banner view while connecting the lbltimermessage tag to the no text label view. Integration of IADBy adding banner ads to the interface and putting the IAD framework into the project, ads can be delivered to the application. However, no processing is available, so our goal is to take all the actions that are possible to manage advertising banners. The IAD framework provides several delegate methods to manage ads. These methods let us know that an ad is about to be displayed or it has been displayed, that an error occurs when a user clicks on an ad, that no more ads can be delivered, and that the modal view is lost when the user clicks on the ad to view its full-screen view. In addition to all of this, you should always remember that ads may not be communicated. As a result, banner ads must be invisible until the first ad is ready to be displayed. It should also be a reason to hide in the IAD network that it will stop running ads. It should also be hidden when the IAD network stops advertising services for some reason. Before we dive into the code, we have to do two things: we first construct the delegate class for the banner ad view, and then we hide it. Open the viewcontroller.m file and go to the Viewdidload method and add the next few lines:
 
   
  
  1. -  (void ) viewdidload 
  2. {  
  3.     [super  viewDidLoad];  
  4.     // do any additional setup  after loading the view, typically from a nib.  
  5. &NBSP;
  6.     // make self  the delegate of the ad banner.  
  7. &NBSP;&NBSP;&NBSP;&NBSP;SELF.ADBANNER.DELEGATE&NBSP;=&NBSP;SELF;&NBSP;
  8.  
  9.     // initially hide the  ad banner.  
  10.     self.adbanner.alpha = 0.0; 
As you can see, by setting its alpha value to 0, we hide the banner view. Later, when a delegate method notifies us that the ad is ready to be displayed, we then set the value to 1, and we also use animations to make it more appealing. Changing the alpha value is obviously not the only way to hide or show an ad banner. There are many other ways to do this, such as setting the value of a property that changes the hidden display bar, or placing the banner ad outside the visible screen area at the outset. In what way, your final choice is entirely up to you, choosing the best method for your application. For the sake of brevity, I just choose to use the alpha value. The Adbannerviewdelegate protocol provides a total of 5 delegate methods. Through them, we can manage the final behavior of ads and applications. We'll do a simple analysis one by one. It is important to note that in each delegate method body, I specifically add the NSLog command, so when we run the application we can know which method is called. The first method:
 
   
  
  1. -(void) Bannerviewwillloadad: (Adbannerview *) banner{
  2. NSLog (@"ad Banner would load AD.");
  3. }
From the name of the method it is easy to draw a conclusion when a new ad is about to be loaded in the call. Please note that the ad is not ready to be displayed when this method is invoked. The second method of delegation:
 
   
  
  1. -(void) Bannerviewdidloadad: (Adbannerview *) banner{
  2. NSLog (@"ad Banner did load AD.");
  3. }
This method is called when the new ad is loaded and ready to be displayed. Then, in this approach, we need to add the code that will make the banner ad visible again, so we know there's an ad to display. The third method of delegation:
 
   
  
  1. -(BOOL) Bannerviewactionshouldbegin: (Adbannerview *) banner willleaveapplication: (BOOL) willleave{
  2. NSLog (@"Ad Banner action is on to begin.");
  3. return YES;
  4. }
This method is called when the user clicks on the banner ad, and the ad is displayed in a full-size mode view. When this happens, you need to pause any visual processes or mission-critical tasks for the user until the modal view is dismissed. The interesting thing about this approach is the value it returns. When the return value is yes, the ad will be displayed in full screen view. However, if the return value is no, nothing happens when the user clicks the ad. The fourth method of delegation:
 
   
  
  1. -(void) Bannerviewactiondidfinish: (Adbannerview *) banner{
  2. NSLog (@"Ad Banner action did finish");
  3. }
This method is called when the full screen view is added to release. This is very useful because any pause or stop task here should be put into action again. Finally, a fifth delegate method:
 
   
  
  1. -(void) Bannerview: (Adbannerview *) banner didfailtoreceiveadwitherror: (Nserror *) error{
  2. NSLog (@"Unable to show ads. Error:%@ ", [Error localizeddescription]);
  3. }
This method is also very useful and important as it is called when no ads can be delivered to the application. When it is called, our job is to hide the banner view so that when no ads are displayed, the user is not given a blank space. As you can see, all of these delegate methods make it very easy to get your ad processed. Now, as we said before, we have to show banner ads when the ads are ready to be displayed, so modify the Bannerviewdidloadad: Delegate method as follows:
 
   
  
  1. -(void) Bannerviewdidloadad: (Adbannerview *) banner{
  2. NSLog (@"ad Banner did load AD.");
  3. //Show the ad banner.
  4. [UIView animatewithduration:0.5 animations:^{
  5. Self.adBanner.alpha = 1.0;
  6. }];
  7. }
You see, we set the alpha value of our banner ad view to 1 in animated form. The animation is set to a fade-in effect. Finally, when no ads will be displayed, we should hide the ad banner view. In Bannerview:didfailtoreceiveadwitherror: Add the same code as above, this time setting the alpha value to 0:
 
   
  
  1. -(void) Bannerview: (Adbannerview *) banner didfailtoreceiveadwitherror: (Nserror *) error{
  2. NSLog (@"Unable to show ads. Error:%@ ", [Error localizeddescription]);
  3. //Hide the ad banner.
  4. [UIView animatewithduration:0.5 animations:^{
  5. Self.adBanner.alpha = 0.0;
  6. }];
  7. }
The most important work related to advertising has been completed. In the next section of this tutorial, we'll see how to use other delegate methods, but our application is ready to use. So, you can try it once and wait for the first ad to be displayed. Adding countersAdvertising banners are now working well, and the most important part--correctly displayed and hidden banner ads have been implemented. However, this tutorial is not just about all the important delegate methods, so in this section we are going to do an interesting project. We will use a timer (a Nstimer object) that will calculate the number of seconds the user sees the view controller scene and take advantage of the delegate method provided by the IAD framework, and we will pause the count when the ad is displayed in full-screen mode. When the ad view is lifted, the counter will work again. Let's start by declaring three properties to achieve the purpose we need. The first property is the timer object, the second is the integer value that will be used to calculate the number of seconds, and the last third property is a Boolean flag that indicates whether the count should be paused. In the viewcontroller.m file, go to the Private class section and add the properties as shown below:
 
   
  
  1. @Interface Viewcontroller ()
  2. @property (nonatomic, strong) Nstimer *timer;
  3. @property (nonatomic) int secondselapsed;
  4. @property (nonatomic) BOOL pausetimecounting;
  5. @end
Now, in the Viewdidload method, let's do some initialization:
  
 
    1. -  (void ) viewdidload 
    2.     [super  viewdidload]; 
    3.       ... 
    4. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;...&NBSP;
    5.  
    6.     // start the timer.  
    7.     self.timer = [nstimer scheduledtimerwithtimeinterval:1.0  target:self selector: @selector (showtimermessage)  userinfo:nil repeats:yes]; 
    8. &NBSP;
    9.     // set the initial  value for the elapsed seconds.  
    10. &NBSP;&NBSP;&NBSP;&NBSP;SELF.SECONDSELAPSED&NBSP;=&NBSP;0;&NBSP;
As you can see during the creation of the timer object, the selector parameter is set to the Showtimermessage method. This is a private method that does not yet exist, and is called by the timer every second (set to 1.0 interval parameter). Of course, we want our timers to work over and over again, so we repeatedly set the parameter value to Yes. Xcode now displays a warning because it cannot find the method of the above declaration. Let's first declare this method to fix it, and then we'll define it. Go back to the Private class section and add the next line:
 
   
  
  1. @Interface Viewcontroller ()
  2. ...
  3. ...
  4. -(void) showtimermessage;
  5. @end
Now in its definition, you will see the following code snippet, which relies on the pausetimecounting flag for the code execution process. If its value is false, then we will continue to count and update the text in Lbltimermessage. Obviously, when its value is true, we don't count it. Note that it is necessary to increase the value of the Secondselapsed property before any text is updated. The implementation is very simple, so you see:
  
 
    1. -(void ) showtimermessage{ 
    2.      if   (!self.pausetimecounting)  { 
    3. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SELF.SECONDSELAPSED++;&NBSP;
    4.  
    5.         self.lbltimermessage.text = [ Nsstring stringwithformat:@ "you" ve been viewing this view  For %d seconds ",  self.secondselapsed]; 
    6.     } 
    7.     else { 
    8.          self.lbltimermessage.text = @ "Paused  To show ad ";  
    9. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;
Let's take a look at how to use the other two IAD delegate methods. The first one we saw was bannerviewactionshouldbegin:willleaveapplication:. As I've already said, this method is called after the user has clicked on the ad and displays it in full-screen view. Of course, it is necessary to set the return value of the method to Yes, otherwise there will be no ad display. When we call this method, it's very simple: tell our application to pause the time count. To do this, we can simply change the value of the pausetimecounting flag. Let's take a look at the method first:
 
   
  
  1. -(BOOL) Bannerviewactionshouldbegin: (Adbannerview *) banner willleaveapplication: (BOOL) willleave{
  2. NSLog (@"Ad Banner action is on to begin.");
  3. self.pausetimecounting = YES;
  4. return YES;
  5. }
In addition to pausing the counting process, when a full image view with ads is turned off, we must also allow it to go on. For this purpose, we will use the Bannerviewactiondidfinish: Delegate method. In this case, we will simply change the value of the flag again. As follows:
 
   
  
  1. -(void) Bannerviewactiondidfinish: (Adbannerview *) banner{
  2. NSLog (@"Ad Banner action did finish");
  3. self.pausetimecounting = NO;
  4. }
This is our last link! The count will now be paused when the ad banner is clicked, and it will be counted again when the banner is lifted. compiling and running the applicationAt this point, you are free to compile and run the application. You will notice that the banner will appear at the bottom of the screen for a period of time and the value of the counter will be displayed on the message label. Click on the ad banner and you'll see a full-screen mode ad. When you return to the scene of the view controller and show that you are watching the ad, the counter you see is not active. If suddenly no ads are served, don't worry, it's normal. Ad serving may stop at any time. As an additional exercise, you can try to display the ad banner at the top of the view. SummaryAdvertising integration into your application is a fairly common strategy, and it's a great way to get some revenue from your free apps. To be honest, maybe you won't make a lot of money by advertising, but if your app gets a big download volume, then you might have a significant benefit. As you can see in this tutorial, there are simple rules for advertising banners, and the required code is just a few lines. So if you're still thinking about whether you should add ads to your app and how to add them, that's a good opportunity. Of course, official documents are also recommended and encouraged to use them. I hope this tutorial will be of some help to you, and I hope you can always share any ideas or comments with us.

How to use IAD to show banner ads in apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.