Thursday, February 21, 2019

Add Admob for ios app

Download Admob framework, extract and drag to project. On pop up window choose Copy items if needed.


If not use drag and check Copy if needed like this, there will be access library error.
Click on top tree folder, choose Build phases, make sure Link Binary with Libraries already has framework.

Click to Build Settings, on Search box, type bitcode. In Build Options, change Enable Bitcode to No.

Add this code to project.
import GoogleMobileAds
Add to class declare.
GADBannerViewDelegate
Declare banner ads.
var adMobBannerView = GADBannerView()
let ADMOB_BANNER_UNIT_ID = "ca-app-pub-3940256099942544/2934735716"

Banner height 50dp, other view will below it, let set their height y >50.
Add this line to viewDidLoad
initAdMobBanner()
Copy function to above last close bracket.
func initAdMobBanner() {
ifUIDevice.currentDevice().userInterfaceIdiom == .Phone {
// iPhone
adMobBannerView.adSize=GADAdSizeFromCGSize(CGSize(width: 320, height: 50))
adMobBannerView.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
        }
else{
// iPad
adMobBannerView.adSize=GADAdSizeFromCGSize(CGSize(width: 468, height: 60))
adMobBannerView.frame = CGRect(x: 0, y: 0, width: 468, height: 60)
        }
//view.frame.size.height
adMobBannerView.adUnitID = ADMOB_BANNER_UNIT_ID
adMobBannerView.rootViewController = self
adMobBannerView.delegate = self
view.addSubview(adMobBannerView)
let request = GADRequest()
adMobBannerView.loadRequest(request)
    }
// Hide the banner
func hideBanner(banner: UIView) {
UIView.beginAnimations("hideBanner", context: nil)
        banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
UIView.commitAnimations()
        banner.hidden = true
    }
// Show the banner
func showBanner(banner: UIView) {
UIView.beginAnimations("showBanner", context: nil)
        banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: 0, width: banner.frame.size.width, height: banner.frame.size.height)
//view.frame.size.height - banner.frame.size.height
UIView.commitAnimations()
        banner.hidden = false
    }
// AdMob banner available
func adViewDidReceiveAd(_view: GADBannerView!) {
showBanner(adMobBannerView)
    }
// NO AdMob banner available
func adView(_view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
hideBanner(adMobBannerView)
    }
If you want banner at bottom screen, change y index of two set position lines to y:c-50.
adMobBannerView.frame = CGRect(x: 0, y: c-50, width: 320, height: 50)
adMobBannerView.frame = CGRect(x: 0, y: c-50, width: 468, height: 60)
c get from this line.
let c = UIScreen.mainScreen().bounds.size.height
If want vertical banner, add these lines in to viewDidLoad.
UIView.animateWithDuration(0.0, animations: {
self.adMobBannerView.transform = CGAffineTransformMakeRotation(CGFloat(90) * CGFloat(M_PI)/180)
        })
You must change x, y to make it show at true position.
Run to see result.

Change example ads
let ADMOB_BANNER_UNIT_ID = "ca-app-pub-3940256099942544/2934735716"

by your app ID ads.
Now iphone X has black band height 30pt, so we change y: 0 to y: 30 to move ads banner down.
Should use if like this.
if (height==812){
move=30
}

Variable move use to set for y: CGFloat(0+move)

No comments:

Post a Comment