IOS 广告 - 集成 admob 和 iAd,并且设置分地区使用

1、向项目里添加 iAd 的 banner(横条)。

  引入框架 iAd.framework;

  定义一个 iAd bannerView 的变量 iAdView, 并加入 iAd 框架(import iAd)和 ADBannerViewDelegate 协议

var iAdView:ADBannerView?

  实现 iAdView 实例,并规定其位置

    iAdView = ADBannerView(adType: ADAdType.Banner)
    iAdView?.frame = CGRectMake(0, 0 - iAdView!.frame.height, iAdView!.frame.width, iAdView!.frame.height)
    statusbarHeight = self.view.frame.size.height - iAdView!.frame.height
    iAdView?.delegate = self
    self.view.addSubview(iAdView!)

  并且实现 ADBannerViewDelegate 协议

    // iAdBannerViewDelegate
    func bannerViewWillLoadAd(banner: ADBannerView!) {
        println("bannerViewWillLoadAd")
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        println("bannerViewDidLoadAd")
        relayoutViews()
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        println("didFailToReceiveAd error:\(error)")
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!) {
        println("bannerViewActionDidFinish")
    }
    
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        println("bannerViewActionShouldBegin")
        return true;
    }

 

2、向代码中添加 AdMob。

  引入框架 AdSupport.framework, 导入 AdMob 的相应SDK代码

  定义一个 GADBannerView 的变量 bannerView, 并加入 GADBannerViewDelegate 协议

var bannerView:GADBannerView?

  实现 bannerView 实例,并规定其位置

    bannerView = GADBannerView(adSize: kGADAdSizeBanner)
    bannerView?.adUnitID = "ca-app-pub-3724477525755491/7721017568"    // 设置 AdMob 的广告 ID
    statusbarHeight = self.view.frame.size.height - bannerView!.frame.height   // 设置 banner 的 y 轴位置
    bannerView?.frame.size.width = self.view.frame.size.width          // 设置 bannerView 的宽度,以应对不同尺寸的手机屏幕
    bannerView?.delegate = self
    bannerView?.rootViewController = self
    self.view.addSubview(bannerView!)
    bannerView?.loadRequest(GADRequest())

  实现 GADBannerViewDelegate 协议

// GADBannerViewDelegate
    func adViewDidReceiveAd(view: GADBannerView!) {
        println("adViewDidReceiveAd:\(view)");
    }
    
    func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
        println("\(view) error:\(error)")
    }
    
    func adViewWillPresentScreen(adView: GADBannerView!) {
        println("adViewWillPresentScreen:\(adView)")
    }
    
    func adViewWillLeaveApplication(adView: GADBannerView!) {
        println("adViewWillLeaveApplication:\(adView)")
    }
    
    func adViewWillDismissScreen(adView: GADBannerView!) {
        println("adViewWillDismissScreen:\(adView)")
    }

 

3、实现不同地区显示不同的广告,原因是 iAd 只支持为数不多的地区,代码如下:

// iAd func 判断该地区支不支持 iAd
    func iAdTimeZoneSupported()->Bool {
        let iAdTimeZones = "America/;US/;Pacific/;Asia/Tokyo;Europe/".componentsSeparatedByString(";")
        var myTimeZone = NSTimeZone.localTimeZone().name
        for zone in iAdTimeZones {
            if (myTimeZone.hasPrefix(zone)) {
                return true;
            }
        }
        
        return false;
    }

 

4、在app加载 bannerView 后重新刷新界面(在 ADBannerViewDelegate 和 GADBannerViewDelegate 方法中刷新),对比要的视图/控件调整位置

// 重画 banner 视图
    func relayoutViews() {
        var bannerFrame = iAdSupported ? iAdView!.frame : bannerView!.frame
        bannerFrame.origin.x = 0
        bannerFrame.origin.y = statusbarHeight
        if iAdSupported {
            iAdView!.frame = bannerFrame
        } else {
            bannerView!.frame = bannerFrame
        }
        
        // 使原来的视图高度减少一个 banner 的高度,或者上移一个 banner 的高度
        self.mainMapView.frame.size.height = self.view.frame.size.height - bannerFrame.size.height
        self.settingBtn.frame.size.height = self.view.frame.size.height - bannerFrame.size.height
    }

   更新 ADBannerViewDelegate 和 GADBannerViewDelegate 的实现方法

    // iAdBannerViewDelegate
    func bannerViewWillLoadAd(banner: ADBannerView!) {
        println("bannerViewWillLoadAd")
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        println("bannerViewDidLoadAd")
        relayoutViews()
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        println("didFailToReceiveAd error:\(error)")
        relayoutViews()   // 重画框架
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!) {
        println("bannerViewActionDidFinish")
        relayoutViews()   // 重画框架
    }
    
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        println("bannerViewActionShouldBegin")
        return true;
    }

    // MARK: - GADBannerView
    
    // GADBannerViewDelegate
    func adViewDidReceiveAd(view: GADBannerView!) {
        println("adViewDidReceiveAd:\(view)");
        relayoutViews()
    }
    
    func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
        println("\(view) error:\(error)")
        relayoutViews()
    }
    
    func adViewWillPresentScreen(adView: GADBannerView!) {
        println("adViewWillPresentScreen:\(adView)")
        relayoutViews()
    }
    
    func adViewWillLeaveApplication(adView: GADBannerView!) {
        println("adViewWillLeaveApplication:\(adView)")
        relayoutViews()
    }
    
    func adViewWillDismissScreen(adView: GADBannerView!) {
        println("adViewWillDismissScreen:\(adView)")
        relayoutViews()
    }

 可参见代码:http://www.swiftmi.com/code4swift/29.html , https://github.com/goldenfiredo/Swift_GoogleAd

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。