question

DougM@Amazon avatar image
DougM@Amazon asked

Technote: Mobile Ads for iOS in Swift

Steps:

First step is to create a new Xcode project of your choice, using Swift as the programming language. From there you will need to add the required Frameworks for our MobileAds SDK to function, this can be done in your project settings in Xcode under the General tab:

StoreKit.framework

AmazonAd.framework

Foundation.framework

UIKit.framework

AdSupport.framework

CoreLocation.framework

SystemConfiguration.framework

MediaPlayer.framework EventKit.framework

EventKitUI.framework

CoreGraphics.framework

CoreTelephony.framework

Our AmazonAd.framework you can download with our Mobile App SDK at https://developer.amazon.com/public/resources/development-tools/sdk

In addition to this work you need to do the following things: Go into the AmazonAd.framework and go to both the AmazonAdView.h and AmazonAdInterstitial.h files

Under #import add: #import

After you are through with that, you will need to go to the top of your project, and create a new header file Call it -Bridging-Header.h The contents of this file between the #define and #endif statements will be the following :

#import #import #import #import #import

Once this file is saved, please go back to your project settings and go to Build Settings: Under Swift Compiler – Code Generation Setting: Objective-C Bridging Header Debug and Release : add the bridging header file you just created.

Within func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool Add the following statements:

var adRegistration = AmazonAdRegistration.sharedRegistration() adRegistration.setAppKey("YOUR-APP-KEY-FROM-MOBILE-APP-DISTRIBUTION-PORTAL")

Following that, within any View Controllers you wish to invoke the AmazonAdView, using a basic 320x50 ad as an example:

var adView : AmazonAdView = AmazonAdView(adSize: AmazonAdSize_320x50)

var options: AmazonAdOptions = AmazonAdOptions()

options.isTestRequest = true adView.loadAd(options) self.view.addSubview(adView)

For displaying interstitial ads We use the following ViewController example. import UIKit

class AmazonInterstitialAdTestViewController: UIViewController,AmazonAdInterstitialDelegate{

@IBOutlet weak var adStatusLabel: UILabel!

var interstitial: AmazonAdInterstitial?

override func viewDidLoad() { super.viewDidLoad() }

@IBAction func loadAd(sender: UIButton) { interstitial = AmazonAdInterstitial() interstitial?.delegate = self var options: AmazonAdOptions = AmazonAdOptions()

options.isTestRequest = true self.interstitial?.load(options) }

@IBAction func displayAd(sender: UIButton) {

self.interstitial?.presentFromViewController(self) } //MARK:AmazonAdInterstitialDelegate func interstitialDidLoad(interstitial: AmazonAdInterstitial!) { self.adStatusLabel.text="Ad loaded" } func interstitialDidFailToLoad(interstitial: AmazonAdInterstitial!, withError error: AmazonAdError!) { self.adStatusLabel.text = "Interstitial failed to load with error \(error.description)" } func interstitialDidPresent(interstitial: AmazonAdInterstitial!) { println("Interstitial presented") } func interstitialWillDismiss(interstitial: AmazonAdInterstitial!) { println("Interstitial will dismiss") } }

Following these steps, you should be able to build and run your Swift app and be able to see an Amazon Ad View in your Swift application.

If you have any questions, please follow up on this thread, or feel free to Contact Us at https://developer.amazon.com/public/support/contact/contact-us .

mobile ads
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers