Code Integration for iOS

Get started with Prebid Mobile by creating a Prebid Server account. Once your account is set up include the Prebid Mobile SDK in your app by either using Cocoapods or by cloning the repo and using our included script to build the SDK.

Package Managers

Cocoapods

If you are not familar with using Cocoapods for dependency management visit their getting started page. Once you have your podfile setup, include the following:

platform :ios, '10.0'

target 'MyAmazingApp' do
    pod 'PrebidMobile'
end

Run the following commands:

  1. Install CocoaPods
  2. Run pod install
  3. Add Prebid pod into Podfile, specify version if nedded
  4. Run pod update
  5. Use the .xcworkspace file which was generated by CocoaPods

Carthage

If you are not familiar with the Carthage package builder, please refere to the project github page for more details.

  1. Install Carthage
  2. Add github prebid/prebid-mobile-ios to your Cartfile.
  3. Run carthage update.
  4. Drag <module_name>.framework from Carthage/Build to General -> Linked Frameworks and Libraries

XCFramework

  1. Clone the project and run buildPrebidMobile.sh script from scripts folder
  2. Drag XC<module_name>.xcframework(e.g. XCPrebidMobile.xcframework) from generated/output directory into your project. Make sure Copy items if needed is selected.
  3. Go to your Xcode project’s General -> Frameworks, Libraries, and Embedded Content settings. Use Embed & Sign for dynamic and Do Not Embed for static linking

Swift PM

If you are not familiar with the Swift Package Manager, please refere to the project github page for more details.

  1. Add Prebid dependency File -> Swift Packages -> Add Package Dependency...
  2. Select desired version, branch or commit
  3. Select Prebid module
  4. Build the specific schema CarthageBuild.sh

    Variant 1

    • Run CarthageBuild.sh script from Cartfile folder. The path should be: .../Carthage/Checkouts/prebid-mobile-ios/scripts/CarthageBuild.sh

    • Enter Schema name (PrebidMobile or PrebidMobileCore)

      • If you run CarthageBuild.sh and see Permission denied use: chmod +x <path_to_CarthageBuild.sh>

    Variant 2

    • Open PrebidMobile.xcodeproj at .../Carthage/Checkouts/prebid-mobile-ios/PrebidMobile.xcodeproj using Xcode

    • Manage Schemes -> Check Shared checkbox for a necessary schema

    • run carthage build prebid-mobile-ios

  5. Integrate the binary into your project

You can find the schema name in the build PrebidSDK framework inside Info.plist with PrebidMobileName key

Build framework from source

Build Prebid Mobile from source code. After cloning the repo, use Terminal or another command line tool, change to the root directory and run:

./scripts/buildPrebidMobile.sh

This will output the PrebidMobile.framework.

Setup Prebid Server

In order to conduct header bidding within your app you will need a Prebid Server hosted account. There are two options available for publishers described at Getting Started with Prebid Mobile.

Once you have a Prebid Server, you will add ‘account’ info to the app. For example, if you’re using the AppNexus Prebid Server:

Prebid.shared.prebidServerAccountId = @"YOUR_ACCOUNT_ID";
Prebid.shared.prebidServerHost = PrebidHostAppnexus;

Note that in actuality, the “account ID” is just the name of the “top-level” stored request as described on the Prebid Server Stored Request page. By convention, most Prebid Server host companies define the top level stored request ID as the account ID they assign to the publisher. This is a convenient convention since publishers generally set the same timeout and price granularity across all apps. But it may not be the case for your Prebid Server host company, so please check with them. If you’re hosting your own Prebid Server, this value can be whatever value you wish, not necessarily an account ID.

If you have opted to host your own Prebid Server solution you will need to store the url to the server in your app.

Prebid.shared.setCustomPrebidServer(url:URL_STRING_TO_SERVER)

Integrate Ad Servers With Your App

Integrating MoPub with your application

  1. Go to MoPub.com and register for a MoPub account . If you already have an account with them, you can log in.

  2. After the registration you will be automatically prompted to set up a new MoPub application required for integrating mobile ads to your application.

Integrating Google with your application

Go to Google’s developer site and follow the instructions for integrating their Mobile Ads SDK into your app.

Set Targeting Parameters (Optional)

Targeting parameters enable you to define the target audience for the bid request. Prebid Mobile supports the following global targeting parameters. These targeting parameters are set only once and apply to all Prebid Mobile ad units. They do not change for a given user session.

View the full list of targeting parameters.

Create Ad Units

Banner and interstitial ad units can be created:

let bannerUnit = BannerAdUnit(configId: "6ace8c7d-88c0-4623-8117-75bc3f0a2e45", size: CGSize(width: 300, height: 250))

For details on creating the specific ad units and additional parameters and methods associated with each view the documentation pertaining to them:

Banner Ad Unit
Interstitial Ad Unit

Using Asset Ids with In-App Native Ad Units

Setting this option to true, in your instance of Prebid Mobile, enables you to add an id for each asset in the assets array. The default setting is false

Swift

Prebid.shared.shouldAssignNativeAssetID = true

Objective C

[Prebid shared].shouldAssignNativeAssetID = YES;

Resize ad slot

Prebid recommends app developers to resize ads slots to the Prebid rendering ad size using native code due to an unresolved bug in the Google Mobile Ads SDK (described here) where render failures can occur with 3rd party creatives (such as Prebid Universal Creative) using size overrides.

Swift

func adViewDidReceiveAd(_ bannerView: GADBannerView) {

    AdViewUtils.findPrebidCreativeSize(bannerView,
                                            success: { (size) in
                                                guard let bannerView = bannerView as? DFPBannerView else {
                                                    return
                                                }

                                                bannerView.resize(GADAdSizeFromCGSize(size))

        },
                                            failure: { (error) in
                                                print("error: \(error)");

        })
}

Objective-C

 -(void) adViewDidReceiveAd:(GADBannerView *)bannerView {
    NSLog(@"Ad received");
    [AdViewUtils findPrebidCreativeSize:bannerView
                                   success:^(CGSize size) {
                                       if ([bannerView isKindOfClass:[DFPBannerView class]]) {
                                           DFPBannerView *dfpBannerView = (DFPBannerView *)bannerView;

                                           [dfpBannerView resize:GADAdSizeFromCGSize(size)];
                                       }
                                   } failure:^(NSError * _Nonnull error) {
                                       NSLog(@"error: %@", error);
                                   }];
}

Further Reading