IOS ADS-Integrate admob and IAd, and set up for sub-region use

Source: Internet
Author: User

1. Add iAd banner (horizontal bar) to the project.

Introducing Frame Iad.framework;

Define an IAD bannerview variable iadview and join the IAD framework (import iad) and adbannerviewdelegate Protocol

var iadview:adbannerview?

Implement a Iadview instance and specify its location

    Iadview = Adbannerview (adType:ADAdType.Banner)    Iadview?. frame = CGRectMake (00 -iadview!. Frame.height, iadview!. Frame.width, iadview! . Frame.height)     = self.view.frame.size.height-iadview! . Frame.height    iadview?. delegate = self    self.view.addSubview (Iadview!)

and implement the Adbannerviewdelegate protocol

    //iadbannerviewdelegateFunc 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. Add AdMob to your code.

Introduce the framework adsupport.framework and import the appropriate SDK code for ADMOB

Define a gadbannerview variable bannerview and join the gadbannerviewdelegate Protocol

var bannerview:gadbannerview?

Implement a Bannerview instance and specify its location

Bannerview =Gadbannerview (adsize:kgadadsizebanner) Bannerview?. Adunitid ="ca-app-pub-3724477525755491/7721017568"    //set up AdMob's ad IDStatusbarheight = self.view.frame.size.height-bannerview!. Frame.height//Set the y-axis position of the bannerBannerview?. Frame.size.width = Self.view.frame.size.width//set the width of the bannerview to handle different sizes of phone screensBannerview?.Delegate=Self bannerview?. Rootviewcontroller =Self self.view.addSubview (Bannerview!) Bannerview?. Loadrequest (Gadrequest ())

Implementing the Gadbannerviewdelegate Protocol

//gadbannerviewdelegateFunc 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, the implementation of different regions display different ads, because iAd only support a few areas, the code is as follows:

//iad func determines that the area branch does not support IAdFunc iadtimezonesupported ()Bool {Let iadtimezones="america/; us/; pacific/; Asia/tokyo; europe/". componentsseparatedbystring (";") var mytimezone=Nstimezone.localtimezone (). Name forZoneinchIadtimezones {if(Mytimezone.hasprefix (Zone)) {return true; }        }                return false; }

4. Refresh the interface after the app loads Bannerview ( refresh in Adbannerviewdelegate and gadbannerviewdelegate methods ), compare the view/control to adjust the position

//Redraw Banner Viewfunc relayoutviews () {var bannerframe= iadsupported? iadview!. frame:bannerview!. Frame bannerframe.origin.x=0BANNERFRAME.ORIGIN.Y=Statusbarheightifiadsupported {Iadview!. frame =Bannerframe}Else{Bannerview!. frame =Bannerframe}//reduce the original view height by one banner, or move up one banner heightSelf.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}

How to update adbannerviewdelegate and Gadbannerviewdelegate

    //iadbannerviewdelegateFunc Bannerviewwillloadad (banner:adbannerview!) {println ("Bannerviewwillloadad")} func Bannerviewdidloadad (Banner:adbannerview!) {println ("Bannerviewdidloadad") Relayoutviews ()} func Bannerview (Banner:adbannerview!, Didfailtoreceiveadwitherror error:nserror!) {println ("didfailtoreceivead error:\ (Error)") relayoutviews ()//Redraw Frame} func bannerviewactiondidfinish (Banner:adbannerview!) {println ("Bannerviewactiondidfinish") relayoutviews ()//Redraw Frame} func bannerviewactionshouldbegin (Banner:adbannerview!, Willleaveapplication Willleave:bool)Bool {println ("Bannerviewactionshouldbegin")        return true; }    //MARK:-Gadbannerview//gadbannerviewdelegateFunc 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 ()}

See also code: http://www.swiftmi.com/code4swift/29.html, Https://github.com/goldenfiredo/Swift_GoogleAd

IOS ADS-Integrate admob and IAd, and set up for sub-region use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.