Amazon Ad View returning nil

58 views Asked by At

I've finished building my app and have started trying to incorporate Amazon Ads into my app. I have a bridging header in my project successfully compiling with my swift code, but I have created a function to load a banner ad. I call the function in viewDidLoad of my view controller. I keep getting an error that the ad view is nil and my app crashes. I'm not sure why this is happening.

I created an empty @IBOutlet of my ad view to follow along with the Quick Start Guide

@IBOutlet weak var amazonAdView: AmazonAdView!

My function to load the ad is here:

func loadAmazonAd(){
    if ((amazonAdView) != nil) {
        amazonAdView.removeFromSuperview()
        amazonAdView = nil
    }

    let adFrame: CGRect = CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 90);
    amazonAdView = AmazonAdView(frame: adFrame)
    amazonAdView.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
    amazonAdView.setHorizontalAlignment(.center)
    amazonAdView.setVerticalAlignment(.fitToContent)

    amazonAdView.delegate = self

    let options = AmazonAdOptions()
    options.isTestRequest = true
    amazonAdView.loadAd(options)
}

Why am I getting an error and why is the ad view returning nil?

1

There are 1 answers

0
user2455722 On BEST ANSWER

It turns out that I shouldn't have made an empty @IBOutlet. Thats why I was getting the error. Just change this:

@IBOutlet weak var amazonAdView: AmazonAdView!

to this:

var amazonAdView: AmazonAdView!