A UIViewController used to display Google Banner and Interstitial Ads.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
McAdViewControllerExample.xcodeproj 17 remove view will appear (#18) Oct 26, 2018
McAdViewControllerExample.xcworkspace 17 remove view will appear (#18) Oct 26, 2018
McAdViewControllerExample
LICENSE 14 add i phone x safe area (#15) Mar 23, 2018
Podfile 14 add i phone x safe area (#15) Mar 23, 2018
Podfile.lock
README.md 14 add i phone x safe area (#15) Mar 23, 2018

README.md

McAdViewController

About

McAdViewController is a UIViewController used to display Google Banner and Interstitial Ads. Animation and rotation ready, this view controller will automatically resize child view controllers so you can focus on developing without the burden of doing this yourself.

Usage

  1. Run pod install like normal, then add McAdViewController to your project
  2. Remove Main interface from your targets General Settings
  3. Uncheck initial View Controller in your storyboard.
  4. Tag your view controller with an Id
  5. In your AppDelegate do something similar to the below. Don't forget to put in your own applicationId, ect...
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var mcAdViewController:McAdViewController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        // Be sure to tag your view controller in the storyboard.
        //
        let viewController:ViewController = storyboard.instantiateViewController(withIdentifier: "ViewControllerId") as! ViewController

        // Initialize McAdViewcontroller and pass your 'contentController' viewController
        //
        mcAdViewController = McAdViewController(contentController: viewController, // Required
                                                applicationId: "ca-app-pub-<YOUR APP ID>", // Required
                                                bannerAdUnitId: "ca-app-pub-<YOUR BANNER ID>", // Conditional Optional - Required if interstantialAdUnitId not provided
                                                interstantialAdUnitId: "ca-app-pub-<YOUR INT ID>", // Conditional Optional - Required if bannerAdUnitId not provided
                                                isBannerBottom: false, // Optional - Default: true
                                                debug: true) // Optional - Default: false
        // safeAreaColor defaults to black
        //
        mcAdViewController?.safeAreaColor = .red

        // Set McAdViewController as the root
        //
        window!.rootViewController = mcAdViewController!
        window!.makeKeyAndVisible()

        return true
    }
}

Displaying Interstitial Ads and Removing the Banner Ad

class ViewController: UIViewController {

    let app:AppDelegate = UIApplication.shared.delegate as! AppDelegate

    @IBAction func displayInterstitial(_ sender: UIButton) {

      // Display Interstitial Ad from Root ViewController
      //
      app.mcAdViewController?.displayInterstitial()

      // Display Interstitial Ad from a Non-root ViewController.
      // ie. You subsequently presented a ViewController modally.
      //
      //app.mcAdViewController?.displayInterstitial(fromViewController: <Not McAdViewController>)
    }

    @IBAction func removeBannerAd(_ sender: UIButton) {
      // Remove the banner ad because, reasons.
      //
      app.mcAdViewController?.removeBannerAd()
    }
}

Example

To run the example project, clone the repo, run pod install then run it in from Xcode.

Note: Be sure to put in your own applicationId, ect... in the AppDelegate

Requirements

  • Swift 4
  • Xcode 9

Installation

Unfortunately, McAdViewController is NOT available through CocoaPods due to Google distributing static libraries. Discussed at length here.

  • Add the following line to your Podfile:
pod 'Google-Mobile-Ads-SDK', '~> 7.29'

puts "Downloading McAdViewController..."
require 'open-uri'
open('./McAdViewController.swift', 'wb') do |file|
  file << open('https://raw.githubusercontent.com/kmcgill88/McAdViewController/master/McAdViewControllerExample/McAdViewController.swift').read
end
puts "Successfully downloaded McAdViewController! :)"

Optional Installation

  • To stay on tagged release, replace master with the tag number. ie. https://raw.githubusercontent.com/kmcgill88/McAdViewController/1.0.0/McAdViewControllerExample/McAdViewController.swift
  • To change the download directory, replace McAdViewControllerExample with your project folder name ie:
open('./<YOUR PROJECT NAME>/McAdViewController.swift', 'wb') do |file|

Author

Kevin McGill, kevin@mcgilldevtech.com

License

McAdViewController is available under the MIT license. See the LICENSE file for more info.