Swift

This document describes the basic procedure for integrating StartApp In-App Ads into your iOS applications written in Swift programming language. 
After this simple integration process, StartApp In-App Ads enables you to reap the benefits of StartApp's In-App monetization products, which maximize the revenue generated by your application. All this profit-making is achieved with minimal effort and minimal interference with your users’ experience.

NOTES:

  • The code samples in this document can be copy/pasted into your source code
  • When submitting your application to the App Store,do not forget to update your "IDFA Settings"
  • Please notice that steps 1-3 are mandatory
  • If you have any questions, contact us via support@startapp.com

Step 1, Downloading the latest StartApp SDK

CocoaPods

Open your project's Podfile and add this line to your app's target:

pod 'StartAppSDK'

Run pod install from command line:

$ pod install

If you're new to CocoaPods, see their official documentation(https://guides.cocoapods.org/using/using-cocoapods) for info on how to create and use Podfiles.

Manual download

Add the StartApp SDK files to your application project directory

  1. Right-click on you project and choose "Add Files to…" 
  2. Add the StartApp SDK files: 

JavaScriptCore.framework 

StartApp.framework 

 StartApp.bundle

Make sure to check the "Copy items if needed" checkbox.

Step 2, Adding frameworks

Add necessary frameworks to your target project

  1. Select your application project to bring up the project editor
  2. Select your application target to bring up the target editor
  3. Select the Build Phases tab and disclose the "Link Binary with Libraries" phase and click the plus button in that phase
  4. Add the following frameworks: 
     CoreTelephony.framework 
     SystemConfiguration.framework 
     CoreGraphics.framework 
     StoreKit.framework 
     AdSupport.framework 
     QuartzCore.framework 
     CoreMedia.framework 
     AVFoundation.framework 
     WebKit.framework 
     libz.dylib in XCode 6 and below / libz.tbd in XCode 7 

 

Step 3, Adding the Bridging-Header file

Create or edit your application's Bridging-Header file

  1. Right-click your project and choose “New File…”

  1. Choose iOS->Source->Header File->Next

  1. Name the file "<YourProduct>-Bridging-Header.h”

where <YourProduct> should be identical to the one being used in your build's settings

  1. Make sure the Bridging Header is declared in "Build Settings" with the right path

  2. Import the following header file:

    #import <StartApp/StartApp.h>

Step 4, Initialization

In your application delegate class (AppDelegate.swift), add the following code to your applicationmethod:

// AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // initialize the SDK with your appID and devID
        var sdk: STAStartAppSDK = STAStartAppSDK.sharedInstance()
        sdk.appID = "Your App Id"
        
        return true
    }

Replace "Your Account Id" and "Your App ID" with your own values provided in the developers’ portal.
After logging in, your account ID will be displayed at the top right-hand corner of the page: 

To find your application ID, click on the "Apps and Sites" tab on the left pane and choose the relevant ID from your app list:

Showing the Splash Ad

A Splash Ad is a full-page ad that is displayed immediately after the application is launched. A Splash Ad first displays a full page splash screen that you define (as described below) followed by a full page ad.

StartApp SDK provides two modes for displaying Splash screens:

Splash Screen Mode Description
User-Defined Mode (default) Using your application's default splash image, with a loading animation
Template Mode StartApp SDK provides a pre-defined template in which you can place your own creatives, such as application name, logo and loading animation. for more details, please refer to the "Advanced Manual"

Adding the Splash Screen

In your application delegate class (AppDelegate.swift), after initializing the SDK, call the sdk.showSplashAd() method:

// AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // initialize the SDK with your appID and devID
        var sdk: STAStartAppSDK = STAStartAppSDK.sharedInstance()
        sdk.appID = "Your App Id"
        sdk.devID = "Your Developer Id"
        
	sdk.showSplashAd()
		
        return true
    }

If you wish to customize or use a different splash screen, please refer to the Advanced Usage.

Show Interstitial Ad

You can choose to show the interstitial ad in several locations within your application. This could be between stages, while waiting for an action, when pressing a button and more.

In your view controller declare STAStartAppAd at the begin of the class. Init STAStartAppAd within the viewDidLoad() method and load it within the viewDidAppear() method.

var startAppAd: STAStartAppAd?

override func viewDidLoad() {
        super.viewDidLoad()

        startAppAd = STAStartAppAd()
    }

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAppAd!.loadAd()
    }

Finally, add the following lines where you want to show the ad

startAppAd!.showAd()

IMPORTANT

Loading an ad might take a few seconds so it's important to show the ad as late as you can. In case you call showAd() while the ad hasn't been successfully loaded yet, nothing will be displayed. For example, if you'd like to show an ad after completing a game's level, the best practice would be to show the ad upon completing the level (for example in your viewDidDisappear() method). On the other hand, loading and showing the ad together at the beginning of the next level might result with a failure – as the ad might not have enough time to load.

Return Ads

The Return Ad is a new ad unit which is displayed once the user returns to your application after a certain period of time. To minimize the intrusiveness, short time periods are ignored. For example, the Return Ad won't be displayed if the user leaves your application to take a short phone call before returning.

Return ads are enabled and activated by default. If you want to disable this feature, simply call sdk.disableReturnAd() as part of the initialization process, in your AppDelegate.swift file:

// AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // initialize the SDK with your appID and devID
        var sdk: STAStartAppSDK = STAStartAppSDK.sharedInstance()
        sdk.appID = "Your App Id"
        sdk.devID = "Your Developer Id"
        
	sdk.disableReturnAd()
		
        return true
    }

Show Rewarded Video Ad

Rewarded Ads are interstitial video ads that provide a reward to the user in exchange for watching an entire video ad. The reward might be in-app goods, virtual currency or any premium content provided by the application. Because users actually opt-in to watch a rewarded video and are granted with something valuable in return, Rewarded Ads are an effective and clean monetization solution for stronger user retention and keeping users engaged in your application for a longer amount of time.

In order to integrate rewarded videos in your app do the following:

1 Declare STAStartAppAd instance:

class ViewController: UIViewController, STADelegateProtocol {

    // Declaration of StartApp Rewarded ad
    var startAppRewarded: STAStartAppAd?
} 

2 Use the "loadRewardedVideoAd" method:

override func viewDidLoad() {
    super.viewDidLoad()
    startAppRewarded = STAStartAppAd()

    // Loading the ad
    startAppRewarded!.loadRewardedVideoAd(withDelegate: self);
}

 3 Implement the following method in order to get a callback when the user completes watching the video and is eligible for getting the reward:

func didCompleteVideo(_ad: STAAbstractAd) {
   print("StartApp rewarded video had been completed", terminator: "")
}

4 Finally, add the following lines where you want to show the ad

startAppRewarded!.show()

Show Banner Ad

To display banners in your app, follow the following steps:

1 In your view controller declare STABannerView at the begin of the class.

// ViewController.swift 

var startAppBanner: STABannerView?

2 Create and initialize your STABannerView variable and add it as a subView to the view where you want it to be displayed.

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if (startAppBanner == nil) {
            startAppBanner = STABannerView(size: STA_AutoAdSize, autoOrigin: STAAdOrigin_Bottom, withView: self.view, withDelegate: nil);
            self.view.addSubview(startAppBanner!)
        }
    }

The STA_AutoAdSize detects the width of the device's screen in its current orientation, and provides the optimal banner for this size.

NOTE:

this example shows the banner at the bottom of the root view controller (self.view), but you can pass any other view where you want to show the banner

Steps 3 and 4 are required only in case your app supports both orientations.

3 Implement didRotateFromInterfaceOrientation in your view controller

// YourViewController.swift 
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)  {
        // notify StartApp auto Banner orientation change           
        startAppBanner!.didRotateFromInterfaceOrientation(fromInterfaceOrientation)       
        super.didRotateFromInterfaceOrientation(fromInterfaceOrientation)
    }

4 If your app supports iOS 8, implement viewWillTransitionToSize in your view controller

// YourViewController.m
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        // notify StartApp auto Banner orientation change
        startAppBanner!.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
     
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    }

Positioning the banner

The origin of the banner is determined by the "autoOrigin" parameter which can receive one of the following values

Value Position Behavior
STAAdOrigin_Top Auto Top The banner will be centered and pinned to the top of the view. In case the view is a root view controller, the banner will be located under the status bar, if exists.
STAAdOrigin_Bottom Auto Bottom The banner will be centered and pinned to the bottom of the view.

NOTE

If you wish to use a fixed origin for the banner, please refer to the "Advanced Manual"

Enjoy Higher eCPM with Demographic-Targeted Ads

If you know your user's gender, age or location, StartApp can use it to serve better-targeted ads which can increase your eCPM and revenue significantly.

Set Age and Gender

Upon initialization, after providing your DevId and AppId, use the following line:

sdk.preferences = STASDKPreferences.prefrencesWithAge(, andGender: )
  • Replace <USER_AGE> with the user's real age
  • Replace <USER_GENDER> with the user's real gender, using STAGender_Male or STAGender_Female.

Example

// AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // initialize the SDK with your appID and devID
        var sdk: STAStartAppSDK = STAStartAppSDK.sharedInstance()
        sdk.appID = "Your App Id"
        sdk.devID = "Your Developer Id"
        
        sdk.preferences = STASDKPreferences.prefrencesWithAge(22, andGender: STAGender_Male)
    
        return true
    }

Set Location

The location of the user is a dynamic property which is changed constantly. Hence, you should provide it every time you load a new Ad:

startAppAd!.loadAdWithAdPreferences(STAAdPreferences.prefrencesWithLatitude(, andLongitude: ))

Example

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAppAd!.loadAdWithAdPreferences(STAAdPreferences.prefrencesWithLatitude(37.3190383911, andLongitude: -121.96269989))
}

Updating your IDFA Settings

When submitting your application to the App Store you need to update its "Advertising Identifier (IDFA)" settings in order to comply with Apple advertising policy.

On the "Advertising Identifier" section: 
1 Choose "Yes" on the right pane 
2 Opt-in the "Serve advertisements within the app" checkbox 
3 Opt-in the confirmation checkbox under "Limit Ad tracking setting in iOS" 

Sample Project

StartApp provides a sample integration project available on GitHub

Advanced Usage

For advanced usage, please read our "Advanced Manual"

Was this article helpful?
0 out of 0 found this helpful