adViewDidReceiveAd in Swift

271 views
Skip to first unread message

fty...@gmail.com

unread,
Jan 17, 2016, 5:21:57 PM1/17/16
to Google Mobile Ads SDK Developers
Hello,

I want to use GADBannerView in another class that named Tools. But adViewDidReceiveAd doesn't fire. What can I do?

Tools class:

class Tools: NSObject, GADBannerViewDelegate {

    

    var view: UIView!

    

    func showAds(viewController: UIViewController) -> Void {


        self.view = viewController.view

        

        let request: GADRequest = GADRequest()

        request.testDevices = [kGADSimulatorID]

        

        let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

        bannerView.rootViewController = viewController

        bannerView.delegate = self

        bannerView.adUnitID = "ca-app-pub-xxx/xxx"

        bannerView.loadRequest(request)

    }

    

    func adViewDidReceiveAd(bannerView: GADBannerView!) {

            self.view.addSubview(bannerView)

    }

    

}


And ViewController class:


class ViewController: UIViewController, GKGameCenterControllerDelegate {


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.


        Tools().showAds(self)

    }


}

Veer Arjun Busani

unread,
Jan 19, 2016, 10:09:03 AM1/19/16
to Google Mobile Ads SDK Developers
Hi there,

Your code would have a positive retain count as you are having an UIView from Tools() strongly referenced via the UIViewController and then adding BannerView to UIView, which would again be referenced by UIViewController. What this would create is a retain cycle and while you might be adding the BannerView in UIView of Tools, it is actually added into UIViewController.view. The delegate would then work try to send messages to the presenting UIView, which in your case would be to the UIViewController. Also, you are trying to add BannerView in delegate, which is not how it's supposed to be done. 

I can suggest you this instead - 
class Tools: UIView, GADBannerViewDelegate {

    func bannerAdView(viewController: UIViewController) -> UIView {

        let request: GADRequest = GADRequest()
        request.testDevices = [kGADSimulatorID]

        let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

        addSubview(bannerView)

        bannerView.rootViewController = viewController
        bannerView.delegate = self

        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.loadRequest(request)
        return self
    }

    func adViewDidReceiveAd(bannerView: GADBannerView!) {
        print("Reveived Ad : \(bannerView.adNetworkClassName)")
    }
}
And then add it like this in your UIViewController - 

view.addSubview(Tools().bannerAdView(self))
You can read more about Memory Management to understand how this is done.

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

Iain Coleman

unread,
Feb 3, 2016, 7:11:03 PM2/3/16
to Google Mobile Ads SDK Developers
I have implemented both the Tools class as you suggested and can successfully call the function in view controller to get an ad to display. The problem is that it is displaying in the top left of the display. Is there a way of getting the ad banner to display inside an IBOutlet that I have positioned in storyboard. The IBOutlet is of type gadbannerview but I can't for the life of me work out how to link it to the view that is returned from the tools class. Sorry if this is a noob question!

Veer Arjun Busani

unread,
Feb 4, 2016, 12:09:08 PM2/4/16
to Google Mobile Ads SDK Developers
Hi Iain,

If you want to show Banner Ads, using Storyboards, through a custom UIView like the Tools example above, you follow these steps - 
  • First would be to add a View on the ViewController Scene in the Storyboard and give the proper constraints. For this example, if you want to have a Banner Ad of 320x50, make sure the View is of minimum 320x50 and place it at the bottom with Center X Aligned. Now you need to make this View of type Tools as this is where the Banner View is again added as a subview.
  • Then replace the bannerAdView function with this - 
  func bannerAdView(viewController: UIViewController) {

        let request
: GADRequest = GADRequest() 
        request
.testDevices = [kGADSimulatorID] 
        let bannerView 
= GADBannerView(adSize: kGADAdSizeSmartBannerPortrait) 
        addSubview
(bannerView) 
        bannerView
.rootViewController = viewController 
        bannerView
.delegate = self 
        bannerView
.adUnitID = "ca-app-pub-3940256099942544/2934735716"
 
        bannerView
.translatesAutoresizingMaskIntoConstraints = false 
        
if let bView = bannerView { 
            let widthConstraint 
= NSLayoutConstraint(item: bView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 320) 
            addConstraint
(widthConstraint) 

            let heightConstraint 
= NSLayoutConstraint(item: bView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50) 
            addConstraint
(heightConstraint) 

            let horizontalConstraint 
= NSLayoutConstraint(item: bView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 
            addConstraint
(horizontalConstraint) 

            let bottomConstraint 
= NSLayoutConstraint(item: bView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0) 
            addConstraint
(bottomConstraint) 
            bView
.loadRequest(GADRequest()) 
        
} 
    
}
  • Then create an IBOutlet for Tools from your Storyboard and simply call tools.bannerAdView(self)  from your main View Controller.
  • You can also create a XIB file for Tools and then add Banner View in that if you do not want to use NSLayoutConstraints.
Do let us know if you need anything else. You can also use the sample project that I have attached.

Project: Link

Thanks,
Reply all
Reply to author
Forward
0 new messages