您的位置:首页 > 移动开发 > Swift

iOS开发用swift添加iAd广告

2015-04-26 20:49 369 查看

第一步,在storyboard中拖入一个iAd BannerView控件,做好布局就ok了。



第二步,将控件引用到viewController中



第四步,代码部分。

[code]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 = true
        self.MyAd.delegate = self
        self.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 code
        return true
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("bannerView")
    }
    //... your class implementation code
}


注意

需要
import iAd

以及接口
ADBannerViewDelegate

运行之后不会马上显示,与你当前的网络状况有关,有可能会等待一段时间,祝成功!

这是成功后应该出现的情况

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: