Amazon Ad Deployment Swift 4 xcode v9.x

How To Wire an Amazon Banner Ad into a Swift 4.0 iOS App (xcode v9.x)

When it came time to monetize our app, Yellow Dog Saves the World, we (for various reasons which I won’t delve into here) decided to use Amazon’s SDK, specifically to add a banner ad.

You would think that Amazon would have a lot of support for this sort of thing, right? Good instructions, concrete examples, etc., etc.? Well, you’d be wrong. For whatever reason, Amazon provides only bare-bones instructions for getting started (which is better than nothing, and which I encourage you to read and utilize to the best of your ability), but a lot is left unsaid when it comes to actually producing a working ad strategy in a production app. Also, as of this writing, mid-January of 2018, the SDK has not been updated for the current versions of Swift and xcode.

Additionally, there ain’t too many good examples out there on the InterWebs that use Amazon ads, just a bunch of queries to Stack Overflow with answers that range from partially useful to not at all.

WARNING – SUBVERSIVE CONTENT (i.e. Sage Advice) -> Compare this to Google’s AdMob (our current ad scheme), which is much easier to get started with, has fairly good instructions, and at least one very good concrete example on the web of connecting it to a real app. Before you go any further, I would recommend that you abandon Amazon in favor of AdMob. I haven’t tried to set up interstitial or rewarded videos in AdMob, but I have done some experimenting with Amazon interstitials, and I am pretty sure that AdMob will be the winner in that arena, as well.-> END – SUBVERSIVE CONTENT

Okay, so my sage advice is wasted on you, and you persist in using Amazon. Here’s how the Code Ninjas  at Parhelyon integrated a Amazon Mobile Ads banner into Yellow Dog Saves the World:

First, add a bridging to your project. Copy and rename the sample bridging header in the SimpleAdUniversalSample_Swift file. Change the name from SimpleAdUniversalSample_Swift-Bridging-Header.h  to Your_Project_Name-Bridging-Header.h, where “Your_Project_Name”, is, of course, the name of your project (in our case,Yellow_Dog_Saves_the_World-Bridging-Header.h). In Build Settings, find Swift Compiler – General, click the arrow on Objective-C Bridging Header, and add the name of your header under Debug and Release (If your header is in the project root, you should only need to add the name, if in a subfolder, you will need to include the folder in the path, i.e., FolderName/Your_Project_Name-Bridging-Header.h.)

Second, add code to AppDelegate.swift, GameViewController.swift, MenuScene.swift, and GameScene.swift:

In AppDelegate, in the application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)  function, add 2 lines:

-> AmazonAdRegistration.shared().setAppKey(app key you received from Amazon goes here, in quotes)

->AmazonAdRegistration.shared().setLogging(true). You must change this value to “false” in the production code.

In GameViewController (ViewController, if not a game app):

-> Create a variable to connect to the banner view control in the storyboard. Although, in general, you should avoid using global variables as much as possible, we made this a global as it needs to be available to our MenuScene and GameScene classes. Example: var amazonBannerAd: AmazonAdView!

-> In the viewWillLayoutSubviews function, ad this chunk of code:

let adFrame = CGRect(x: 0, y: 154, width: UIScreen.main.bounds.size.width,

height: 90)

amazonBannerAd = AmazonAdView.init(frame: adFrame)



//Set ad loading options and load ad

let adOptions = AmazonAdOptions()

adOptions.isTestRequest = true //Set to false for production version

amazonBannerAd.loadAd(adOptions)// This loads the ad

//Ensure our delegate receives the callback messages

amazonBannerAd.delegate = self

//Add the Amazon Ad banner to our view


//Hide the banner ad in MenuScene

amazonBannerAd.isHidden = true

This creates an ad frame that fits acceptably near the bottom center of an iPhone (tested on all simulator devices from iPhone 6 to iPhone X). It does not auto-place well on iPads at all. Ads on iPhones will be 320×50, which is Amazon’s preferred size for phones. This code causes it to auto-resize for iPads, but you can set a hardwired ad size in your code – sample code found on the Amazon forum shows how.

-> Still in GameViewController, right below the viewWillLayoutSubviews function, we added these three(3) functions:


func viewControllerForPresentingModalView() -> UIViewController! {

return self


func adViewDidLoad(_ view: AmazonAdView!) -> Void {

Swift.print(“An ad loaded successfully.”)


func adViewDidFail(toLoad view: AmazonAdView!, withError: AmazonAdError!)  -> Void {

Swift.print(“Ad failed to load. Error code \(withError.errorCode) \(withError.errorDescription)”)



In our MenuScene class, we added code to reveal the hidden ad when the Start button is pressed: amazonBannerAd.isHidden = false

In the GameScene class, in the code that checks our buttons that replay and restart the game, in the code for the restart (returnToMenu, in our case) button, which returns the game to the initial start menu screen, we need to hide the ad once more: amazonBannerAd.isHidden = true

You may or may not want to hide the ad in the start menu, if this is the case, you can leave out the three calls (in GameViewController, MenuScene, and GameScene) to amazonBannerAd.isHidden altogether.

A long road to launch day

Parhelyon, how did we settle upon the name Parhelyon? Well, it all began when we typed “cool words” into google, yes we really typed “cool words” into google to find our company name. Unfortunately some laser geeks over in North Carolina had already taken Parhelion, which means sun dog. So we changed the “i” to a “y” and continued about our day. Surprisingly we have come to like our spelling better anyways.

After a few long months of designing, building and battling SIGBART errors that seems to plague any IOS app developer,  Yellow Dog Saves The World is finally ready to be officially released in the App Store.