Smart AdServer mediation
- Latest Version: iOS release notes
- Sample App: Teads iOS Sample App
This article shows you how to deliver Teads ads on your application using the Smart Ad Server Mediation adapter.
Using CocoaPods to have the Teads Smart Ad Server mediation plugin will automatically import the Teads inApp SDK framework.
Prerequisites
- Platform: iOS 14+
- Xcode: 26.2+
- Built with Swift 6.2.3
- Smart-Display-SDK 7.6.2+
Installation
Before installing the Teads SAS adapter, you will need to integrate Smart Ad Server SDK into your application.
CocoaPods
If your project is managing dependencies through CocoaPods, you just need to add this pod in your Podfile.
It will install the Teads adapter along with the Teads inApp SDK.
- Add Pod named TeadsSASAdapter your Podfile:
pod 'TeadsSASAdapter', '~> 6.2'
- Run the following to install the adapter in your project.
$ pod install --repo-update
- Follow the Defining a Custom Event step below (in the relevant tab) to finish the integration.
Manually
- Integrate the latest version of the Teads inApp SDK to your project.
- Download the latest release of TeadsSASAdapter.
- Drop adapter files in your iOS project.
- Follow the Defining a Custom Event step below to finish the integration.
- Media
- Native Ad
Defining a Custom Event
In order to display a Teads ad using Smart Ad, you need to create a custom event.
See the SDK documentation for more test PIDs serving different creative formats.
| Name | Value |
|---|---|
| SDK Name | TeadsSDK |
| Ad Format Type | Banner |
| Adapter Class | TeadsSASAdapter.TeadsSASBannerAdapter |
| Placement Info | { "placementId": #PID#, "teadsAdSettingsKey": "[sas_kw | teadsAdSettingsKey]" } |
Important
Don't forget to replace the #PID# with your Teads placement ID.
Integration
Note
Check out the sample ViewController from our iOS GitHub repository
You have the ability to pass extra parameters in order to customize third-party ad network settings. For Teads, you need to pass TeadsAdapterSettings.
- Concat your existing keywordsTargeting using
TeadsSASAdapterHelper.concatAdSettingsToKeywords. - Create
SASAdPlacementwith your keywordsTargeting
- Swift
- Objective-C
let settings = TeadsAdapterSettings { settings in
settings.pageUrl("https://example.com/article1")
}
let webSiteId = #WebSiteId
let pageId = #PageId
let formatId = #formatId
var keywordsTargetting = "yourkeyword=something"
keywordsTargetting = TeadsSASAdapterHelper.concatAdSettingsToKeywords(keywordsStrings: keywordsTargetting, adSettings: teadsAdSettings)
// Create a placement
let adPlacement = SASAdPlacement(siteId: webSiteId, pageId: pageId, formatId: formatId, keywordTargeting: keywordsTargetting)
banner?.load(with: adPlacement)
TeadsAdapterSettings *settings = [[TeadsAdapterSettings alloc] initWithBuild:^(TeadsAdapterSettings *settings) {
[settings pageUrl:@"https://example.com/article1"];
}];
NSInteger webSiteId = #WebSiteId;
NSInteger pageId = #PageId;
NSInteger formatId = #formatId;
NSString *keywordsTargetting = @"yourkeyword=something";
keywordsTargetting = [TeadsSASAdapterHelper concatAdSettingsToKeywordsWithKeywordsStrings:keywordsTargetting adSettings:teadsAdSettings];
// Create a placement
SASAdPlacement *adPlacement = [[SASAdPlacement alloc] initWithSiteId:webSiteId pageId:pageId formatId:formatId keywordTargeting:keywordsTargetting];
[self.banner loadWithPlacement:adPlacement];
Ad Resizing
The code below must be implemented to enable the resizing of the mediated ad slot making possible the rendering of square and vertical creatives.
Register to ad resizing through TeadsAdapterSettings by providing your delegate class and the SASBannerView that need to be resized
- Swift
- Objective-C
var sasAdView: SASBannerView!
@IBOutlet weak var slotHeight: NSLayoutConstraint! // set to 0 on InterfaceBuilder
let adSettings = TeadsAdapterSettings { settings in
settings.registerAdView(sasAdView, delegate: self)
}
@property (nonatomic, strong) SASBannerView *sasAdView;
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *slotHeight; // set to 0 on InterfaceBuilder
TeadsAdapterSettings *adSettings = [[TeadsAdapterSettings alloc] initWithBuild:^(TeadsAdapterSettings *settings) {
[settings registerAdView:self.sasAdView delegate:self];
}];
Simply conform to the Teads delegate TeadsMediatedAdViewDelegate and implements the didUpdateRatio function.
You can instantiate a new listener as below, the listener needs to be a class member field or strong referenced somewhere, we keep internally a weak reference to it, so you are responsible to retain it and destroy it when you don't need it anymore:
- Swift
- Objective-C
func didUpdateRatio(_ adView: UIView, adRatio: TeadsAdRatio) {
let height = adRatio.calculateHeight(for: slotView.frame.width) // call calculateHeight
slotHeight.constant = height //update NSLayoutConstraint of the slot
}
- (void)didUpdateRatio:(UIView *)adView adRatio:(TeadsAdRatio *)adRatio {
CGFloat height = [adRatio calculateHeightForWidth:self.slotView.frame.size.width]; // call calculateHeight
self.slotHeight.constant = height; //update NSLayoutConstraint of the slot
}
This native implementation is specifically designed to fit feed views You can customize view to look a like other cells around an offer immersive experience
Defining a Custom Event
In order to display a Teads ad using Smart Ad, you need to create a custom event.
See the SDK documentation for more test PIDs serving different creative formats.
| Name | Value |
|---|---|
| SDK Name | TeadsSDK |
| Ad Format Type | Banner |
| Adapter Class | TeadsSASAdapter.TeadsSASNativeAdapter |
| Placement Info | { "placementId": #PID#, "teadsAdSettingsKey": "[sas_kw | teadsAdSettingsKey]" } |
Important
Don't forget to replace the #PID# with your Teads placement ID.
Integration
Note
Check out the sample ViewController from our iOS GitHub repository
Important
You're responsible for positioning the elements (title label, main image, call to action button...). Those elements need to be visible (without overlay, even transparent ones) otherwise our visibility algorithm will consider the ad view as hidden.
Interface
- Add a
SmartNativeAdTableViewCellto the storyboard - Set UI elements as a cell properties
- Make sure to link IBOutlet to Interface Builder
class SmartNativeAdTableViewCell: UITableViewCell {
@IBOutlet public var titleLabel: UILabel?
@IBOutlet public var contentLabel: UILabel?
@IBOutlet public var mediaView: UIView!
@IBOutlet public var iconImageView: UIImageView?
@IBOutlet public var advertiserLabel: UILabel?
@IBOutlet public var callToActionButton: UIButton?
@IBOutlet public var ratingView: UIView?
@IBOutlet public var priceLabel: UILabel?
}
Load an Ad
You have the ability to pass extra parameters in order to customize third-party ad network settings. For Teads, you need to pass TeadsAdapterSettings.
- Concat your existing keywordsTargeting using
TeadsSASAdapterHelper.concatAdSettingsToKeywords. - Create
SASAdPlacementwith your keywordsTargeting
- Swift
- Objective-C
let teadsAdSettings = TeadsAdapterSettings { settings in
settings.pageUrl("https://example.com/article1")
}
let adRowNumber = 3
let webSiteId = #WebSiteId
let pageId = #PageId
let formatId = #formatId
var keywordsTargetting = "yourkeyword=something"
keywordsTargetting = TeadsSASAdapterHelper.concatAdSettingsToKeywords(keywordsStrings: keywordsTargetting, adSettings: teadsAdSettings)
// Create a placement
let adPlacement = SASAdPlacement(siteId: webSiteId, pageId: pageId, formatId: formatId, keywordTargeting: keywordsTargetting)
nativeAdManager = SASNativeAdManager(placement: adPlacement)
nativeAdManager?.requestAd { (ad: SASNativeAd?, error: Error?) in
if let nativeAd = ad {
self.elements.insert(nativeAd, at: self.adRowNumber)
self.tableView.reloadData()
} else if let error = error {
print("Unable to load ad: \(error.localizedDescription)")
} else {
print("Unknown error")
}
}
TeadsAdapterSettings *teadsAdSettings = [[TeadsAdapterSettings alloc] initWithBuild:^(TeadsAdapterSettings *settings) {
[settings pageUrl:@"https://example.com/article1"];
}];
NSInteger adRowNumber = 3;
NSInteger webSiteId = #WebSiteId;
NSInteger pageId = #PageId;
NSInteger formatId = #formatId;
NSString *keywordsTargetting = @"yourkeyword=something";
keywordsTargetting = [TeadsSASAdapterHelper concatAdSettingsToKeywordsWithKeywordsStrings:keywordsTargetting adSettings:teadsAdSettings];
// Create a placement
SASAdPlacement *adPlacement = [[SASAdPlacement alloc] initWithSiteId:webSiteId pageId:pageId formatId:formatId keywordTargeting:keywordsTargetting];
self.nativeAdManager = [[SASNativeAdManager alloc] initWithPlacement:adPlacement];
[self.nativeAdManager requestAdWithCompletionHandler:^(SASNativeAd * _Nullable ad, NSError * _Nullable error) {
if (ad) {
[self.elements insertObject:ad atIndex:self.adRowNumber];
[self.tableView reloadData];
} else if (error) {
NSLog(@"Unable to load ad: %@", error.localizedDescription);
} else {
NSLog(@"Unknown error");
}
}];
Store Ad (Sequence)
When implementing native in a tableView or collectionView we suggest to add SASNativeAd inside your sequence structure
private var elements = [SASNativeAd?]()
private var nativeAdManager: SASNativeAdManager?
override func viewDidLoad() {
super.viewDidLoad()
(0..<8).forEach { _ in
elements.append(nil) //nil reprensents all other data of cells
}
...
}
Ad Binding
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let nativeAd = elements[indexPath.row] {
guard let cell = tableView.dequeueReusableCell(withIdentifier: teadsAdCellIndentifier, for: indexPath) as? SmartNativeAdTableViewCell else {
return UITableViewCell()
}
nativeAd.delegate = self
cell.bind(sasNativeAd: nativeAd)
return cell
}
...
}
Bind SASNativeAd into your cell using custom func bind
extension SmartNativeAdTableViewCell {
func bind(sasNativeAd: SASNativeAd) {
titleLabel?.text = sasNativeAd.title
contentLabel?.text = sasNativeAd.subtitle
if sasNativeAd.hasMedia {
let sasMediaView = SASNativeAdMediaView()
sasMediaView.frame = mediaView.bounds
sasMediaView.registerNativeAd(sasNativeAd)
mediaView.addSubview(sasMediaView)
} else {
sasNativeAd.coverImage?.load { image in
let imageView = UIImageView(image: image)
imageView.frame = self.mediaView.bounds
imageView.contentMode = .scaleAspectFill
self.mediaView?.addSubview(imageView)
}
}
sasNativeAd.icon?.load { image in
self.iconImageView?.image = image
self.iconImageView?.contentMode = .scaleAspectFill
}
advertiserLabel?.text = sasNativeAd.sponsored
callToActionButton?.setTitle(sasNativeAd.callToAction, for: .normal)
priceLabel?.text = sasNativeAd.price
}
}
Load images using a custom extension on SASNativeAdImage
extension SASNativeAdImage {
func load(success: @escaping (UIImage) -> Void) {
let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
let dataTask = defaultSession.dataTask(with: url) { data, urlResponse, error in
if let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
success(image)
}
}
}
dataTask.resume()
}
}
Event Monitoring
The Smart Ad Server SDK provides a delegate to monitor events during the ad life cycle and help you customize the ad experience in your apps.
protocol SASNativeAdDelegate {
// When an event click has been fired
func nativeAd(_ nativeAd: SASNativeAd, didClickWith URL: URL)
// Notifies the delegate that a modal view will appear to display the ad's landing page.
func nativeAdWillPresentModalView(_ nativeAd: SASNativeAd)
...
}
Mediation settings
Find the full settings list here