Swiftpack.co - Package - adzerk/adzerk-ios-sdk

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.


Access Adzerk's ad serving APIs via iOS



Use of the Adzerk iOS SDK requires iOS 10.0 or later.


Installation of the framework can be done manually by building and copying the framework into your project, or with automatically with Swift Package Manager (preferred), Carthage, or CocoaPods.

Note that for manual and Carthage framework imports you may have to specify "Embedded Content Contains Swift Code" to avoid getting a linker error during build. Another way to force Xcode to load the Swift libraries is to add a single Swift source file to your project.

Swift Package Manager

Using Xcode, add a Swift Package in the Project Settings tab. Enter the URL https://github.com/adzerk/adzerk-ios-sdk.git and click Next. Choose your version and click continue to integrate it.


If you're using Carthage, add this to your Cartfile:

github "adzerk/adzerk-ios-sdk" ~> 2.0.1

If you want to be on the bleeding edge, you can specify the master branch:

github "adzerk/adzerk-ios-sdk" "master"

Then run carthage update to fetch and build the framework. You can find the framework in the Carthage folder, and you can add this to your project manually.


If you're using CocoaPods, add this to your Podfile:

pod 'adzerk-ios-sdk', '~> 2.0.1

Again, if you want to be on the latest master branch:


pod 'adzerk-ios-sdk', github: 'adzerk/adzerk-ios-sdk', branch: 'master'

Then run pod install to download the code and integrate it into your project. You'll then open the pod-created workspace instead of your project to build.


API Credentials & Required IDs

  • Network ID: Log into Adzerk UI & use the "circle-i" help menu in upper right corner to find Network ID. Required for all SDK operations.
  • Site ID: Go to Manage Sites page to find site IDs. Required when fetching an ad decision.
  • Ad Type ID: Go to Ad Sizes page to find Ad Type IDs. Required when fetching an ad decision.
  • User Key: UserDB IDs are specified or generated for each user.

Fetching an Ad Decision

import AdzerkSDK

// Demo network, site, & ad type IDs; find your own via the Adzerk UI!
DecisionSDK.defaultNetworkId = 23
DecisionSDK.defaultSiteId = 667480

let client = DecisionSDK()

var p = Placements.custom(divName: "div0", adTypes: [5])

var reqOpts = PlacementRequest<StandardPlacement>.Options()
reqOpts.userKey = "abc"
reqOpts.keywords = ["keyword1", "keyword2"]

client.request(placements: [p], options: reqOpts) {response in

Recording Impressions

Use with the fetch ad example above.

client.request(placements: [p], options: reqOpts) {
    switch $0 {
    case .success(let response):
        for decision in response.decisions {
            for selection in decision.value {
                dump(selection, maxDepth: 3)
                print("\nFiring impression pixel...")
                client.recordImpression(pixelURL: selection.impressionUrl!)
    case .failure(let error):

UserDB: Reading User Record

import AdzerkSDK

// Demo network ID; find your own via the Adzerk UI!
DecisionSDK.defaultNetworkId = 23

let keyStore = UserKeyStoreKeychain()
keyStore.save(userKey: "abc")

let client = DecisionSDK(keyStore: keyStore)

client.userDB().readUser() {response in

UserDB: Setting Custom Properties

import AdzerkSDK

// Demo network ID; find your own via the Adzerk UI!
DecisionSDK.defaultNetworkId = 23

let keyStore = UserKeyStoreKeychain()
keyStore.save(userKey: "abc")

let client = DecisionSDK(keyStore: keyStore)

let props:[String: AnyCodable] = [
    "favoriteColor":  .string("blue"),
    "favoriteNumber": .int(42),
    "favoriteFoods":  .array([

client.userDB().postProperties(props) {response in


All API operations are done with an instance of DecisionSDK.

For most uses, a single Network ID and Site ID will be used for the entire application. If this is the case you can configure it once in the AppDelegate:

@import AdzerkSDK

func applicationDidFinishLaunching(...) {
  DecisionSDK.defaultNetworkId = YOUR_NETWORK_ID
  DecisionSDK.defaultSiteId = YOUR_SITE_ID

For requests that need a different Network ID or Site ID, you can specify this on the individual placement request.

You can also set a custom host if you need to, up front, like this:

  DecisionSDK.host = "your custom host"

Note that the host is just the domain part of the reuqests. Do not include a scheme like https:// in your custom hosts.

Requesting Placements

To request a placement, you can build a type that conforms to Placement and specify the attributes you want to send.

There are two types of placements builtin:

  • StandardPlacement
  • CustomPlacement

You can use CustomPlacement if you need to send additional JSON data to the server.

For brevity, you can create placements using the Placements type:

let placement = Placements.standard(...)

To send the request:

// Assumes that the default network ID and site ID are already set on DecisionSDK

let sdk = DecisionSDK()
let placement = Placements.standard(divName: "div1", adTypes: [1])

sdk.request(placement: placement) { result in
    // gives you a Swift Result of type Result<PlacementResponse, AdzerkError>

Note: completion blocks are called on the main queue. If you want to be called back on a different queue, you can pass this queue to the DecisionSDK initializer.

Handling the Response

A placement request will accept a completion block that is handed an instance of Result<PlacementResponse, AdzerkError>.

Handle each case as appropriate for your application. In the case of .success you are given an PlacementResponse that contains the decisions for each placement requested.

GDPR Consent

Consent preferences can be specified when building a request. For example, to set GDPR consent for tracking in the European Union (this defaults to false):

var options = PlacementRequest<StandardPlacement>.Options()
options.consent = Consent(gdpr: false)


By default, warnings and errors will be directed to os_log. You can configure your desired log level:

DecisionSDK.logger.level = .debug

App Transport Security

Adzerk's API Server is compliant with App Transport Security.

Building / Running Tests

You can run tests using the command line:

swift test

Generating Docs

Docs are generated with jazzy and are hosted on github pages. To install jazzy:

$ gem install jazzy

If you're using system ruby, you'll probably need to prefix the above with sudo.

All doc generation happens on a different detached branch. Make sure your working copy is clean, close Xcode, and switch to the gh-pages branch:

$ git checkout gh-pages

Once there the content of the working directory becomes the static HTML site. Run the generate_docs.sh script to copy the latest version of the project from the master branch and run jazzy on it to generate the doc HTML:

$ ./generate_docs.sh

Once done, commit changes and push to github:

$ git add .
$ git commit -m "Update docs"
$ git push

After a few seconds, your changes will be live on https://adzerk.github.io/adzerk-ios-sdk.


This SDK is released under the Apache 2.0 license. See LICENSE for more information.


  • 2.0.1: Update visibility of some members to allow addiiton flexibility.

  • 2.0: Rewritten for Swift, Swift Package Manager. This is a breaking change, as many of the types have evolved to more closely match Swift style.

  • 1.2: Add support for configurable hostname overrides

  • 1.1: Read/update GDPR consent

  • 1.0.4: Objective-C compatibility fixes for placements and decisions.

  • 1.0.3: Turns off logging by default, adds control over how/when to log to the console.

  • 1.0.2: Can specify which queue the sdk calls you back on. Defaults to DispatchQueue.main

  • 1.0: Swift 3 support

Breaking change: The Objective-C status code was changed from NSNumber * to NSInteger, as Swift 3 no longer automatically maps Int? to NSNumber *.

  • 0.4: Initial release


Stars: 9
Last commit: 3 days ago


2.0.1 - 2021-02-22T21:22:55

This release updates the visibility of some attributes to allow more flexibility when building requests.