gpt4 book ai didi

xcode - 添加计时器接收iAd Interstitial(Xcode,swift)

转载 作者:行者123 更新时间:2023-11-30 10:11:27 25 4
gpt4 key购买 nike

我已经做到了,当用户点击一个按钮时,它会显示插页式广告,但我不希望这样。我希望如果用户使用应用程序 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

关于xcode - 添加计时器接收iAd Interstitial(Xcode,swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com