Media Native Placement
Media Native Placement
Media Native placements allow you to create custom layouts for video ads that match your app's design.
- Swift
- Objective-C
import UIKit
import TeadsSDK
class NativeAdViewController: UIViewController {
private var nativePlacement: TeadsAdPlacementMediaNative?
private var nativeAdView: TeadsNativeAdView?
override func viewDidLoad() {
super.viewDidLoad()
setupNativeAd()
}
private func setupNativeAd() {
// Create custom native ad view
nativeAdView = createCustomNativeAdView()
// Create configuration
let config = TeadsAdPlacementMediaConfig(
pid: 84242,
articleUrl: URL(string: "https://example.com/article/123")
)
// Create placement
nativePlacement = TeadsAdPlacementMediaNative(config, delegate: self)
// Load and bind the ad
do {
let binder = try nativePlacement?.loadAd()
if let nativeAdView = nativeAdView {
binder?(nativeAdView)
addNativeAdToView(nativeAdView)
}
} catch {
print("Failed to load native ad: \(error)")
}
}
private func createCustomNativeAdView() -> TeadsNativeAdView {
// Create your custom native ad view
// This can be designed in Interface Builder or programmatically
let adView = TeadsNativeAdView(frame: .zero)
// Customize the appearance to match your app
adView.backgroundColor = .systemBackground
adView.layer.cornerRadius = 12
adView.layer.shadowColor = UIColor.black.cgColor
adView.layer.shadowOpacity = 0.1
adView.layer.shadowRadius = 8
return adView
}
private func addNativeAdToView(_ adView: UIView) {
view.addSubview(adView)
adView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
adView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
adView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
adView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
adView.heightAnchor.constraint(equalToConstant: 250)
])
}
}
#import <UIKit/UIKit.h>
#import <TeadsSDK/TeadsSDK-Swift.h>
@interface NativeAdViewController : UIViewController
@property (nonatomic, strong) TeadsAdPlacementMediaNative *nativePlacement;
@property (nonatomic, strong) TeadsNativeAdView *nativeAdView;
@end
@implementation NativeAdViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupNativeAd];
}
- (void)setupNativeAd {
// Create custom native ad view
self.nativeAdView = [self createCustomNativeAdView];
// Create the placement directly with the Objective-C-friendly convenience
// initializer (the Config-based Swift initializer does not bridge)
self.nativePlacement = [[TeadsAdPlacementMediaNative alloc] initWithPid:84242
articleUrl:[NSURL URLWithString:@"https://example.com/article/123"]
delegate:self];
// Load and bind the ad
NSError *error = nil;
void (^binder)(TeadsNativeAdView *) = [self.nativePlacement loadAdAndReturnError:&error];
if (error) {
NSLog(@"Failed to load native ad: %@", error);
return;
}
if (self.nativeAdView) {
if (binder) {
binder(self.nativeAdView);
}
[self addNativeAdToView:self.nativeAdView];
}
}
- (TeadsNativeAdView *)createCustomNativeAdView {
// Create your custom native ad view
// This can be designed in Interface Builder or programmatically
TeadsNativeAdView *adView = [[TeadsNativeAdView alloc] initWithFrame:CGRectZero];
// Customize the appearance to match your app
adView.backgroundColor = [UIColor systemBackgroundColor];
adView.layer.cornerRadius = 12;
adView.layer.shadowColor = [UIColor blackColor].CGColor;
adView.layer.shadowOpacity = 0.1;
adView.layer.shadowRadius = 8;
return adView;
}
- (void)addNativeAdToView:(UIView *)adView {
[self.view addSubview:adView];
adView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[adView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
[adView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
[adView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
[adView.heightAnchor constraintEqualToConstant:250]
]];
}
@end