0

I have made that when the users click on one button it will show interstitial ad but i do not want like that.I want that if a user use app for 1 minute then it will automatically show ad and after the user close the ad again it come after someTime.

here is the code of mine with button

var interAd = ADInterstitialAd()
var interAdView:UIView = UIView()

 var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton


override func viewDidLoad() {
    super.viewDidLoad()

    closeButton.frame = CGRectMake(10, 10, 20, 20)
    closeButton.layer.cornerRadius = 10
    closeButton.setTitle("X", forState: .Normal)
    closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.blackColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.addTarget(self, action: "close:", forControlEvents:   UIControlEvents.TouchDown)
  }
   func close(sender: UIButton) {
     closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()

}

func loadAd() {
    println("load Ad")
    interAd = ADInterstitialAd()
    interAd.delegate = self

}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
  println("ad did load")

    interAdView = UIView()
    interAdView.frame = self.view.bounds
    view.addSubview(interAdView)

    interAd.presentInView(interAdView)
    UIViewController.prepareInterstitialAds()

    interAdView.addSubview(closeButton)
}
  func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    println("ad did not load")
}

   func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println("failed to recieve")
    println(error.localizedDescription)

    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}

@IBAction func ShowAds(sender: UIButton) {
    loadAd()

}
2
  • ad will display only for one time when user load app? Sep 25, 2015 at 7:54
  • Yup i want something like that @dharmeshkheni Sep 25, 2015 at 7:56

1 Answer 1

1

Two ways to achieve your requirement.

First:

You can use GCD:

override func viewDidLoad() {
    super.viewDidLoad()
    let triggerTime = (Int64(NSEC_PER_SEC) * 60)   // set delay you time here.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
        self.loadAd()
    })
}

Second:

You can use NSTimer:

override func viewDidLoad() {
    super.viewDidLoad()
     NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(60), target: self, selector: "loadAd", userInfo: nil, repeats: false)
}

Set repeat parameter to false if you don't want to repeat it again.

4
  • Can you explain me the 2nd method ? At what time it will come ?@dharmeshkheni Sep 25, 2015 at 8:05
  • After 60 second it will call loadAd method. Sep 25, 2015 at 8:06
  • And one more thing i dont want the ad to be appear by clicking on button. I want it to display automatically Sep 25, 2015 at 8:06
  • then don't call it in button action method. Sep 25, 2015 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.