Display banner on Apple
Display AdropBanner
Initialize AdropBanner
with Ad unit ID applying ad size, then load ad.
class ViewController: UIViewController {
@IBOutlet weak var adContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let bannerView = AdropBanner(unitId: "ADROP_PUBLIC_TEST_UNIT_ID")
bannerView.delegate = self
bannerView.load()
adContainer.addSubview(bannerView)
bannerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
bannerView.translatesAutoresizingMaskIntoConstraints = false
bannerView.frame = adContainer.bounds
}
}
extension ViewController: AdropBannerDelegate {
func onAdReceived(_ banner: AdropAds.AdropBanner) {
print("onAdReceived")
}
func onAdClicked(_ banner: AdropAds.AdropBanner) {
print("onAdClickced")
}
func onAdFailedToReceive(_ banner: AdropAds.AdropBanner, _ error: AdropAds.AdropErrorCode) {
print("onAdFailedToReceive \(AdropErrorCodeToString(code: error))")
}
}
@interface ViewController (AdropAds) <AdropBannerDelegate>
@end
@implementation ViewController (AdropAds)
- (void)onAdReceived:(AdropBanner * _Nonnull)banner {
NSLog(@"onAdReceived");
}
- (void)onAdClicked:(AdropBanner * _Nonnull)banner {
NSLog(@"onAdClicked");
}
- (void)onAdFailedToReceive:(AdropBanner * _Nonnull)banner :(enum AdropErrorCode)error {
NSLog(@"onAdFailedToReceive");
}
@end
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *adContainerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AdropBanner* banner = [[AdropBanner alloc] initWithUnitId:@"ADROP_PUBLIC_TEST_UNIT_ID"];
banner.delegate = self;
[banner load];
[_adContainerView addSubview:banner];
banner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
banner.translatesAutoresizingMaskIntoConstraints = false;
banner.frame = _adContainerView.bounds;
}
@end
class AdropBannerWrapper: AdropBannerDelegate {
let bannerRep: AdropBannerRepresented
init(){
bannerRep = AdropBannerRepresented(unitId: "ADROP_PUBLIC_TEST_UNIT_ID")
bannerRep.banner.delegate = self
}
func onAdReceived(_ banner: AdropAds.AdropBanner) {
print("onAdReceived")
}
func onAdClicked(_ banner: AdropAds.AdropBanner) {
print("onAdClicked")
}
func onAdFailedToReceive(_ banner: AdropAds.AdropBanner, _ error: AdropAds.AdropErrorCode) {
print("onAdFailedToReceive \(AdropErrorCodeToString(code: error))")
}
}
struct ContentView: View {
var adropBannerWrapper = AdropBannerWrapper()
var body: some View {
ZStack{
VStack {
Spacer()
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Adrop Ads")
Button {
adropBannerWrapper.bannerRep.banner.load()
} label: {
Text("requestAd")
}
.padding(.all)
Spacer()
}
VStack {
Spacer()
adropBannerWrapper.bannerRep.frame(height: 80)
}
}
}
}
Last updated