Native Ads
Ad Request
Placement
Create placement from AdFormat with your parameters:
- Swift
- Objective-C
let placement = try? BidMachineSdk.shared.placement(.native) {
$0.withPlacementId("")
$0.withCustomParameters([String:Any]())
}
NSError *error = nil;
BidMachinePlacement *placement = [BidMachineSdk.shared placement:BidMachineAdFormat.native
error:&error
builder:^(id<BidMachinePlacementBuilderProtocol> _Nonnull builder) {
[builder withPlacementId:@""];
[builder withCustomParameters:@{}];
}];
| Parameter | Type | Description |
|---|---|---|
placementId | String | Placement ID |
customParameters | [String: Any] / NSDictionary | Passed directly to the server |
AdFormat
The placement format is defined by static properties of the AdFormat class:
- Swift
- Objective-C
@objc(BidMachineAdFormat)
final public class AdFormat : NSObject {
@objc public static var native: BidMachine.AdFormat { get }
@objc public static var nativeIcon: BidMachine.AdFormat { get }
@objc public static var nativeImage: BidMachine.AdFormat { get }
@objc public static var nativeVideo: BidMachine.AdFormat { get }
@objc public static var nativeIconAndVideo: BidMachine.AdFormat { get }
@objc public static var nativeIconAndImage: BidMachine.AdFormat { get }
@objc public static var nativeImageAndVideo: BidMachine.AdFormat { get }
}
@interface BidMachineAdFormat (SWIFT_EXTENSION(BidMachine))
+ (BidMachineAdFormat * _Nonnull)native;
+ (BidMachineAdFormat * _Nonnull)nativeIcon;
+ (BidMachineAdFormat * _Nonnull)nativeImage;
+ (BidMachineAdFormat * _Nonnull)nativeVideo;
+ (BidMachineAdFormat * _Nonnull)nativeIconAndVideo;
+ (BidMachineAdFormat * _Nonnull)nativeIconAndImage;
+ (BidMachineAdFormat * _Nonnull)nativeImageAndVideo;
@end
Available native formats
| Format | Description |
|---|---|
native | Full native ad with all assets |
nativeIcon | Native ad with icon only |
nativeImage | Native ad with image only |
nativeVideo | Native ad with video only |
nativeIconAndVideo | Native ad with icon and video |
nativeIconAndImage | Native ad with icon and image |
nativeImageAndVideo | Native ad with image and video |
General Request
Auction request is used to set bidding parameters: The request is created with a special placement and your parameters
- Swift
- Objective-C
let request = BidMachineSdk.shared.auctionRequest(placement: placement) {
$0.withUnitConfigurations([BidMachineUnitConfiguration]())
$0.appendPriceFloor(Double(10), UUID().uuidString)
}
BidMachineAuctionRequest *request = [[BidMachineSdk shared] auctionRequestWithPlacement:placement builder: ^(id<BidMachineAuctionRequestBuilderProtocol> _Nonnull builder) {
[builder withUnitConfigurations:@[]];
[builder appendPriceFloor:10.0 identifier:[[NSUUID UUID] UUIDString]];
}];
| Parameter | Type | Description |
|---|---|---|
unitConfigurations | [BidMachineUnitConfiguration] | Header bidding unit configurations |
priceFloor | (Double, String) | Price floor (value and identifier) |
Client Bidding Request
Bid Token
With S2S integration, you will need a token that you need to transfer in the request. To get a token, you can use method:
- Swift
- Objective-C
BidMachineSdk.shared.token(placement: placement) { token in }
[BidMachineSdk.shared tokenWithPlacement:placement completion:^(NSString *token) {
}];
Bid Payload
After completing the server-side auction, you will receive a Base64-encoded payload string, which must be passed as a parameter to the AuctionRequest.
- Swift
- Objective-C
let request = BidMachineSdk.shared.auctionRequest(placement: placement) {
$0.withPayload("")
}
BidMachineAuctionRequest *request = [[BidMachineSdk shared] auctionRequestWithPlacement:placement builder: ^(id<BidMachineAuctionRequestBuilderProtocol> _Nonnull builder) {
[builder withPayload:@""];
}];
| Parameter | Type | Description |
|---|---|---|
payload | String | Custom BidMachine payload string |
Ad Display
The ad object downloads and displays native ads.
Prepare the ad object
Request an ad using your auction request:
- Swift
- Objective-C
BidMachineSdk.shared.native(request: request) { [weak self] ad, error in
guard let self else { return }
self.native = ad
}
__weak typeof(self) weakSelf = self;
[[BidMachineSdk shared] nativeWithRequest:request
completion:^(BidMachineNative *ad, NSError * _Nullable error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf.native = ad;
}];
The loaded ad provides auctionInfo and requestInfo.
The auctionInfo contains information about the auction, such as the winning bidder, price, and other relevant details.
The auctionInfo is described here.
Delegate
Assign the delegate to receive ad events:
- Swift
- Objective-C
ad.delegate = self
ad.delegate = self;
Implement BidMachineAdDelegate methods:
- Swift
- Objective-C
func didLoadAd(_ ad: BidMachineAdProtocol) {}
func didFailLoadAd(_ ad: BidMachineAdProtocol, _ error: Error) {}
func didPresentAd(_ ad: BidMachineAdProtocol) {}
func didFailPresentAd(_ ad: BidMachineAdProtocol, _ error: Error) {}
func didDismissAd(_ ad: BidMachineAdProtocol) {}
func willPresentScreen(_ ad: BidMachineAdProtocol) {}
func didDismissScreen(_ ad: BidMachineAdProtocol) {}
func didUserInteraction(_ ad: BidMachineAdProtocol) {}
func didExpired(_ ad: BidMachineAdProtocol) {}
func didTrackImpression(_ ad: BidMachineAdProtocol) {}
func didTrackInteraction(_ ad: BidMachineAdProtocol) {}
func didReceiveReward(_ ad: BidMachineAdProtocol) {}
- (void)didLoadAd:(id<BidMachineAdProtocol>)ad {}
- (void)didFailLoadAd:(id<BidMachineAdProtocol>)ad :(NSError *)error {}
- (void)didPresentAd:(id<BidMachineAdProtocol>)ad {}
- (void)didFailPresentAd:(id<BidMachineAdProtocol>)ad :(NSError *)error {}
- (void)didDismissAd:(id<BidMachineAdProtocol>)ad {}
- (void)willPresentScreen:(id<BidMachineAdProtocol>)ad {}
- (void)didDismissScreen:(id<BidMachineAdProtocol>)ad {}
- (void)didUserInteraction:(id<BidMachineAdProtocol>)ad {}
- (void)didExpired:(id<BidMachineAdProtocol>)ad {}
- (void)didTrackImpression:(id<BidMachineAdProtocol>)ad {}
- (void)didTrackInteraction:(id<BidMachineAdProtocol>)ad {}
- (void)didReceiveReward:(id<BidMachineAdProtocol>)ad {}
Loading and presenting native ads
Ensure both delegate and controller are set before loading.
Example of native ad loading:
- Swift
- Objective-C
guard let placement = try? BidMachineSdk.shared.placement(.native) else { return }
let request = BidMachineSdk.shared.auctionRequest(placement: placement)
BidMachineSdk.shared.native(request: request) { [weak self] ad, error in
guard let self = self else {
return
}
self.native = ad
self.native.controller = self
self.native.delegate = self
self.native.loadAd()
}
NSError *error = nil;
BidMachinePlacement *placement = [BidMachineSdk.shared placement:BidMachineAdFormat.native
error:&error
builder:nil];
if (!placement) {
return;
}
BidMachineAuctionRequest *request = [[BidMachineSdk shared] auctionRequestWithPlacement:placement
builder:nil];
__weak typeof(self) weakSelf = self;
[[BidMachineSdk shared] nativeWithRequest:request
completion:^(BidMachineNative *ad, NSError * _Nullable error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf.native = ad;
strongSelf.native.controller = strongSelf;
strongSelf.native.delegate = strongSelf;
[strongSelf.native loadAd];
}];
When presenting, implement BidMachineNativeAdRendering to expose UI assets and register them for interaction:
- Swift
- Objective-C
@objc public protocol BidMachineNativeAdRendering {
var titleLabel: UILabel? { get }
var callToActionLabel: UILabel? { get }
var descriptionLabel: UILabel? { get }
var iconView: UIImageView? { get }
var mediaContainerView: UIView? { get }
var adChoiceView: UIView? { get }
}
@objc public enum BidMachineNativeAdRenderingAssetType: Int {
case titleLabel
case callToActionLabel
case descriptionLabel
case iconView
case mediaContainerView
case adChoiceView
}
class NativeAdView: UIView, BidMachineNativeAdRendering {
var titleLabel: UILabel? { titleLab }
var callToActionLabel: UILabel? { callToActionLab }
var descriptionLabel: UILabel? { descriptionLab }
var iconView: UIImageView? { icon }
var mediaContainerView: UIView? { mediaContainer }
var adChoiceView: UIView? { nil }
@IBOutlet weak var titleLab: UILabel!
@IBOutlet weak var descriptionLab: UILabel!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var mediaContainer: UIView!
@IBOutlet weak var callToActionLab: UILabel!
}
// Register assets and present
nativeAd.registerAssetsForInteraction([
BidMachineNativeAdRenderingAssetType.descriptionLabel.rawValue
])
let nativeAdView = NativeAdView()
try nativeAd.presentAd(in: nativeAdView, rendering: nativeAdView)
@protocol BidMachineNativeAdRendering <NSObject>
@property (nonatomic, nullable) UILabel *titleLabel;
@property (nonatomic, nullable) UILabel *callToActionLabel;
@property (nonatomic, nullable) UILabel *descriptionLabel;
@property (nonatomic, nullable) UIImageView *iconView;
@property (nonatomic, nullable) UIView *mediaContainerView;
@property (nonatomic, nullable) UIView *adChoiceView;
@end
typedef NS_ENUM(NSInteger, BidMachineNativeAdRenderingAssetType) {
BidMachineNativeAdRenderingAssetTypeTitleLabel,
BidMachineNativeAdRenderingAssetTypeCallToActionLabel,
BidMachineNativeAdRenderingAssetTypeDescriptionLabel,
BidMachineNativeAdRenderingAssetTypeIconView,
BidMachineNativeAdRenderingAssetTypeMediaContainerView,
BidMachineNativeAdRenderingAssetTypeAdChoiceView
};
@interface BDMNativeAdView : UIView <BidMachineNativeAdRendering>
@property (nonatomic, weak) IBOutlet UILabel *titleLab;
@property (nonatomic, weak) IBOutlet UILabel *descriptionLab;
@property (nonatomic, weak) IBOutlet UIImageView *icon;
@property (nonatomic, weak) IBOutlet UIView *mediaContainer;
@property (nonatomic, weak) IBOutlet UILabel *callToActionLab;
@end
@implementation BDMNativeAdView
- (UILabel *)titleLabel {
return self.titleLab;
}
- (UILabel *)callToActionLabel {
return self.callToActionLab;
}
- (UILabel *)descriptionLabel {
return self.descriptionLab;
}
- (UIImageView *)iconView {
return self.icon;
}
- (UIView *)mediaContainerView {
return self.mediaContainer;
}
-(UIView *)adChoiceView {
return nil;
}
@end
// Register assets and present
NSError *error = nil;
[nativeAd registerAssetsForInteraction:@[
@(BidMachineNativeAdRenderingAssetTypeDescriptionLabel)
]];
BDMNativeAdView *nativeAdView = [[BDMNativeAdView alloc] init];
[nativeAd presentAdInContainer:nativeAdView
rendering:nativeAdView
error:&error];