添加定时器接收iAd Interstitial(Xcode,swift)

Adding a timer to receive iAd Interstitial(Xcode,swift)

我已经做到,当用户点击一个按钮时,它会显示插页式广告,但我不希望 that.I 希望如果用户使用应用程序 1 分钟,它会自动显示广告,然后用户在一段时间后再次关闭广告。

这是我的带有按钮的代码

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()

}

两种方式来达到你的要求。

第一个:

您可以使用 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()
    })
}

第二个:

您可以使用 NSTimer:

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

如果您不想再次重复,请将重复参数设置为 false