本文主要是介绍iOS开发用swift添加iAd广告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一步,在storyboard中拖入一个iAd BannerView控件,做好布局就ok了。
第二步,将控件引用到viewController中
第四步,代码部分。
import UIKit
import iAd
class ViewController: UIViewController,ADBannerViewDelegate {@IBOutlet weak var MyAd: ADBannerView!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.self.canDisplayBannerAds = trueself.MyAd.delegate = selfself.MyAd.hidden = true }override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func bannerViewWillLoadAd(banner: ADBannerView!) {NSLog("bannerViewWillLoadAd")}func bannerViewDidLoadAd(banner: ADBannerView!) {NSLog("bannerViewDidLoadAd")self.MyAd.hidden = false //now show banner as ad is loaded}func bannerViewActionDidFinish(banner: ADBannerView!) {NSLog("bannerViewDidLoadAd")//optional resume paused game code }func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {NSLog("bannerViewActionShouldBegin") //optional pause game codereturn true}func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {NSLog("bannerView")}//... your class implementation code
}
注意:
需要import iAd
以及接口ADBannerViewDelegate
运行之后不会马上显示,与你当前的网络状况有关,有可能会等待一段时间,祝成功!
这是成功后应该出现的情况
这篇关于iOS开发用swift添加iAd广告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!