Code Monkey home page Code Monkey logo

iads's Introduction

iAds

Demo for integrating different types of iAds in app

iAd is an ad platform from Apple.

In this post we will discuss how to monetize your apps using iAd platform. Lets get started.

Four types of ads

  • ) Banner ads
  • ) Interstitial ads
  • ) Medium Rect
  • ) Pre roll video

Banner

  • ) Full device width view
  • ) Placed at the bottom of the content
  • ) The ads keeps rolling no configuration required
  • ) Supports iphone and ipad
#### Integration
* Link iAd framework
* Import iAd framework in your view controller and configure view controller
	 import iAd
	 viewController.canDisplayBannerAds = true
#### Handling Banner ad clicks
When the banner ad is clicked a full screen pop will show more about the ad. Before this happens we need to pause any activity in the viewWillDisappear method and resume the activity in viewWillAppear.

Interstitial

  • ) Full screen add
#### Prepare for Interstitial
In `applicationDidFinishLaunchingWithOptions` prepare for interstitial ads display by calling `UIViewController.prepareInterstitialAds()`

Integration

In automatic mode you dont can't control when the ad shows up

viewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Automatic

In manual mode you control when to show up the ad

viewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual

Medium Rect

  • Positined inline with content
  • Automatic recycling
  • Fullscreen ad on tap

This is a bit more complicated than the banner and interstitial ads. We need to create the banner explicity and handle delegate to display medium rect ad.

//
//  MediumRectViewController.swift
//  iAds
//
//  Created by Shrikar Archak on 12/6/14.
//  Copyright (c) 2014 Shrikar Archak. All rights reserved.
//

import UIKit
import iAd
class MediumRectViewController: UIViewController, ADBannerViewDelegate {
    var adView: ADBannerView?
    override func viewDidLoad() {
        super.viewDidLoad()

        adView = ADBannerView(adType: ADAdType.MediumRectangle)
        adView!.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        self.view.addSubview(adView!)
        self.view.layoutIfNeeded()
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        adView!.removeFromSuperview()
        self.view.layoutIfNeeded()
        
    }

}

Pre Roll Ad

  • Short video before content
  • Full screen ads on tap
//
//  PreRollVideoViewController.swift
//  iAds
//
//  Created by Shrikar Archak on 12/6/14.
//  Copyright (c) 2014 Shrikar Archak. All rights reserved.
//

import UIKit
import MediaPlayer
import iAd

class PreRollVideoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "https://www.youtube.com/watch?v=m3ZyU98N3Fk")
        let moviePlayer = MPMoviePlayerController(contentURL: url)
        moviePlayer.prepareToPlay()
        moviePlayer.view.frame = self.view.bounds
        
        NSLog("Self : frame : \(NSStringFromCGRect(self.view.frame))")
        NSLog("Self : bounds : \(NSStringFromCGRect(self.view.bounds))")
        NSLog("Movie : frame : \(NSStringFromCGRect(moviePlayer.view.frame))")
        NSLog("Movie : bounds : \(NSStringFromCGRect(moviePlayer.view.bounds))")
        
        self.view.addSubview(moviePlayer.view)
        moviePlayer.view.layoutIfNeeded()
        self.view.layoutIfNeeded()

        moviePlayer.playPrerollAdWithCompletionHandler { (error) -> Void in
            NSLog("\(error)")
            moviePlayer.play()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

iads's People

Contributors

sarchak avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.