Coder Social home page Coder Social logo

kmcgill88 / mcadviewcontroller Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 40 KB

A UIViewController used to display Google Banner and Interstitial Ads.

License: MIT License

Swift 97.17% Ruby 2.83%
admob ios google-adsense uiviewcontroller interstitial-ads banner swift

mcadviewcontroller's Introduction

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, [email protected]

License

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

mcadviewcontroller's People

Contributors

kmcgill88 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mohd14shoeb

mcadviewcontroller's Issues

Check Interstitial for hasBeenUsed

If an Interstitial Ad hasBeenUsed and you attempt to present it again you will get the error: Request Error: Will not send request because interstitial object has been used..

Need to add the check for hasBeenUsed, per docs

    open func displayInterstitial(){
        if let inter = interstantialAd {
            if inter.isReady {
                inter.present(fromRootViewController: self)
            } else {
                printDebug("Interstitial Ad not ready, requesting to prep now.")
                prepareInterstantialAd()
            }
        }
    }

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.