Display rewarded ad on Apple
Display a rewarded ad
let rewardedAd = AdropRewardedAd(unitId: "PUBLIC_TEST_UNIT_ID_REWARDED")
rewardedAd.delegate = self
rewardedAd.load()
...
// call show function when the ad needs to be displayed.
rewardedAd.show(fromRootViewController: self, userDidEarnRewardHandler: { type, amount in
print("earned reward type: \(type) amount: \(amount)")
})
self.rewardedAd = [[AdropRewardedAd alloc] initWithUnitId:@"PUBLIC_TEST_UNIT_ID_REWARDED"];
self.rewardedAd.delegate = self;
[self.rewardedAd load];
...
// call show function when the ad needs to be displayed.
[self.rewardedAd showFromRootViewController:self userDidEarnRewardHandler:^(NSInteger type, NSInteger amount) {
NSLog(@"earned reward type: %ld amount: %ld", type, amount);
}];
class AdropRewardedAdWrapper: AdropRewardedAdDelegate {
var rewardedAd: AdropRewardedAd?
var errorHandler: (String) -> Void
init(_ unitId: String, handler: @escaping (String) -> Void ) {
self.rewardedAd = AdropRewardedAd(unitId: unitId)
self.errorHandler = handler
rewardedAd?.delegate = self
}
func onAdReceived(_ ad: AdropAds.AdropRewardedAd) {
print("onAdReceived")
}
func onAdFailedToReceive(_ ad: AdropAds.AdropRewardedAd, _ error: AdropAds.AdropErrorCode) {
print("onAdFailedToReceive \(AdropErrorCodeToString(code: error))")
self.errorHandler(AdropErrorCodeToString(code: error))
}
func load() {
rewardedAd?.delegate = self
rewardedAd?.load()
}
func show(fromRootViewController: UIViewController, userDidEarnRewardHandler: @escaping AdropUserDidEarnRewardHandler) {
rewardedAd?.show(fromRootViewController: fromRootViewController, userDidEarnRewardHandler:userDidEarnRewardHandler)
}
}
struct RewardedAdView: View {
@State var adropRewardedAdWrapper :AdropRewardedAdWrapper? = nil
var body: some View {
NavigationView {
VStack {
Button {
adropRewardedAdWrapper = AdropRewardedAdWrapper("PUBLIC_TEST_UNIT_ID_REWARDED") { _ in }
adropRewardedAdWrapper?.load()
} label: {
Text("load")
}
.padding(.all)
Button {
adropRewardedAdWrapper?.show(fromRootViewController: (UIApplication.shared.windows.first?.rootViewController)!){
type, amount in
print("earned reward!11(type: \(type) amount: \(amount))")
}
} label: {
Text("show")
}
.padding(.all)
Spacer()
}
.navigationTitle("RewardedAd Example")
}
}
}
Last updated