Native Ads
Ad Request
Placement
Create placement from PlacementFormat
with your parameters:
- Swift
- Objective-C
let placement = try? BidMachineSdk.shared.placement(from: .native) {
$0.withPlacementId("")
$0.withCustomParameters([String:Any]())
}
NSError *error = nil;
BidMachinePlacement *placement = [[BidMachineSdk shared] placementFrom:BidMachinePlacementFormatNative
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 |
PlacementFormat
The placement format is defined by the PlacementFormat
enum:
- Swift
- Objective-C
@objc(BidMachinePlacementFormat)
public enum PlacementFormat: Int {
case native
case nativeIcon
case nativeImage
case nativeVideo
case nativeIconAndVideo
case nativeIconAndImage
case nativeImageAndVideo
}
typedef SWIFT_ENUM_NAMED(NSInteger, BidMachinePlacementFormat, "PlacementFormat", open) {
BidMachinePlacementFormatNative = 12,
BidMachinePlacementFormatNativeIcon = 13,
BidMachinePlacementFormatNativeImage = 14,
BidMachinePlacementFormatNativeVideo = 15,
BidMachinePlacementFormatNativeIconAndVideo = 16,
BidMachinePlacementFormatNativeIconAndImage = 17,
BidMachinePlacementFormatNativeImageAndVideo = 18
};
You can also get a format from a string:
- Swift
- Objective-C
let format = "native".bidmachine_placement_format
BidMachinePlacementFormat format = [@"native" bidmachine_placement_format];
Registered placement formats
Name | Description |
---|---|
native | Full native ad with all assets |
native_icon | Icon only |
native_image | Image only |
native_video | Video only |
native_icon_and_video | Icon plus video |
native_icon_and_image | Icon plus image |
native_image_and_video | Image plus 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(from: .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] placementFrom:BidMachinePlacementFormatNative
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];
Mediation
If the request is involved in third-party mediation, you can use such methods to notify the SDK about the win or loss events:
- Swift
- Objective-C
//Where ad - is loaded bidmachine ad
//Mediation WIN ( ad )
BidMachineSdk.shared.notifyMediationWin(ad)
//Mediation LOSS ( winner, price, ad )
BidMachineSdk.shared.notifyMediationLoss("WINNER_NETWORK", 1.0, ad)
//Where ad - is loaded bidmachine ad
//Mediation WIN ( ad )
[BidMachineSdk.shared notifyMediationWin:ad];
//Mediation LOSS ( winner, price, ad )
[BidMachineSdk.shared notifyMediationLoss:@"WINNER_NETWORK" ecpm:1.0 ad:ad];