Rich Townsend
Rich Townsend

Reputation: 589

Call Admob interstitial after delay of 5 seconds - Swift iOS

Please can anyone help me rewrite my code as I'm struggling to present my interstitial ad after a delay of 5 seconds. I've built a view controller (below), using the VEA software tutorial from youTube (this tutorial uses a UIButton to trigger the interstitial). I've then added a NSTimer line to run a func called timerFunc.

Now I've only left the UIButton and timerFunc in the code to show it works. Ultimately I would like the NSTimer to call the ad after the 5 seconds without any interaction from the user.

I've included all the necessary frameworks and I'm sure someone may see this as a simple challenge but I'd be grateful if anyone could help, any questions let me know.

Thank you.

import UIKit
import SpriteKit
import GoogleMobileAds



class GameOverViewController: UIViewController {

var intersitital: GADInterstitial!

@IBOutlet weak var bannerView: GADBannerView!
// start google tracking

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    var tracker = GAI.sharedInstance().defaultTracker
    tracker.set(kGAIScreenName, value: "Game Over Screen")

    var builder = GAIDictionaryBuilder.createScreenView()
    tracker.send(builder.build() as [NSObject : AnyObject])

    //end google tracking
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.intersitital = self.createAndLoadAd()


    NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "timerFunc", userInfo: nil, repeats: false)

}

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

func createAndLoadAd() -> GADInterstitial    {
    var ad = GADInterstitial(adUnitID: "ca-app-pub-7019466409243282/2303966909")

    var request = GADRequest()

    request.testDevices = [""]
    ad.loadRequest(request)
    return ad

}


func someSelector() {
    // Something after a delay
}
//

@IBAction func adButton(sender: AnyObject) {

    if (self.intersitital.isReady)
    {
        self.intersitital.presentFromRootViewController(self)
        self.intersitital = self.createAndLoadAd()
        }


}

func timerFunc() {
    println("Timer")
}

override func prefersStatusBarHidden() -> Bool {
    return true
}
}

Upvotes: 0

Views: 1372

Answers (1)

fabian789
fabian789

Reputation: 8412

The cleanest way is to use dispatch_after:

let seconds: Double = 5
let delayTime = dispatch_time(DISPATCH_TIME_NOW, 
                              Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    // show your ad
}

That said, apps that show ads after 5 seconds are evil and will most likely get deleted immediately, at least on my devices.

Upvotes: 2

Related Questions