Skip to content

Commit e16408c

Browse files
authored
fix(ios): prepare method return adUnitId (#223)
1 parent 4a363a2 commit e16408c

File tree

4 files changed

+149
-53
lines changed

4 files changed

+149
-53
lines changed

demo/angular/src/app/validation/tab2/tab2.page.ts

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Component } from '@angular/core';
2-
import {AdMob, InterstitialAdPluginEvents} from '@capacitor-community/admob';
3-
import {ITestItems} from '../../shared/interfaces';
4-
import {PluginListenerHandle} from '@capacitor/core';
5-
import {ViewDidEnter, ViewWillEnter, ViewWillLeave} from '@ionic/angular';
6-
import {interstitialOptions} from '../../shared/ad.options';
2+
import { AdMob, InterstitialAdPluginEvents } from '@capacitor-community/admob';
3+
import { ITestItems } from '../../shared/interfaces';
4+
import { PluginListenerHandle } from '@capacitor/core';
5+
import { ViewDidEnter, ViewWillEnter, ViewWillLeave } from '@ionic/angular';
6+
import { interstitialOptions } from '../../shared/ad.options';
77
import { HelperService } from '../../shared/helper.service';
88

9-
const tryItems: ITestItems [] = [
9+
const tryItems: ITestItems[] = [
1010
{
1111
type: 'method',
1212
name: 'prepareInterstitial',
@@ -17,15 +17,15 @@ const tryItems: ITestItems [] = [
1717
},
1818
{
1919
type: 'event',
20-
name: InterstitialAdPluginEvents.Loaded
20+
name: InterstitialAdPluginEvents.Loaded,
2121
},
2222
{
2323
type: 'event',
24-
name: InterstitialAdPluginEvents.Showed
24+
name: InterstitialAdPluginEvents.Showed,
2525
},
2626
{
2727
type: 'event',
28-
name: InterstitialAdPluginEvents.Dismissed
28+
name: InterstitialAdPluginEvents.Dismissed,
2929
},
3030
{
3131
type: 'method',
@@ -41,26 +41,46 @@ const tryItems: ITestItems [] = [
4141
@Component({
4242
selector: 'app-tab2',
4343
templateUrl: 'tab2.page.html',
44-
styleUrls: ['tab2.page.scss']
44+
styleUrls: ['tab2.page.scss'],
4545
})
4646
export class Tab2Page implements ViewDidEnter, ViewWillEnter, ViewWillLeave {
4747
private readonly listenerHandlers: PluginListenerHandle[] = [];
4848
public eventItems: ITestItems[] = [];
49-
constructor(
50-
private helper: HelperService,
51-
) {}
49+
constructor(private helper: HelperService) {}
5250

5351
ionViewWillEnter() {
5452
const eventKeys = Object.keys(InterstitialAdPluginEvents);
5553
eventKeys.forEach(key => {
56-
const handler = AdMob.addListener(InterstitialAdPluginEvents[key], (value) => {
57-
this.helper.updateItem(this.eventItems,InterstitialAdPluginEvents[key], true, value);
58-
if (key === 'Dismissed') {
59-
AdMob.prepareInterstitial({ adId: 'failed' })
60-
.then(async () => await this.helper.updateItem(this.eventItems,'prepareInterstitialFailed', false))
61-
.catch(async () => await this.helper.updateItem(this.eventItems,'prepareInterstitialFailed', true));
62-
}
63-
});
54+
const handler = AdMob.addListener(
55+
InterstitialAdPluginEvents[key],
56+
value => {
57+
this.helper.updateItem(
58+
this.eventItems,
59+
InterstitialAdPluginEvents[key],
60+
true,
61+
value,
62+
);
63+
if (key === 'Dismissed') {
64+
AdMob.prepareInterstitial({ adId: 'failed' })
65+
.then(
66+
async () =>
67+
await this.helper.updateItem(
68+
this.eventItems,
69+
'prepareInterstitialFailed',
70+
false,
71+
),
72+
)
73+
.catch(
74+
async () =>
75+
await this.helper.updateItem(
76+
this.eventItems,
77+
'prepareInterstitialFailed',
78+
true,
79+
),
80+
);
81+
}
82+
},
83+
);
6484
this.listenerHandlers.push(handler);
6585
});
6686

@@ -69,16 +89,43 @@ export class Tab2Page implements ViewDidEnter, ViewWillEnter, ViewWillLeave {
6989

7090
async ionViewDidEnter() {
7191
await AdMob.prepareInterstitial(interstitialOptions)
72-
.then(async () => await this.helper.updateItem(this.eventItems,'prepareInterstitial', true))
73-
.catch(async () => await this.helper.updateItem(this.eventItems,'prepareInterstitial', false));
92+
.then(
93+
async data =>
94+
await this.helper.updateItem(
95+
this.eventItems,
96+
'prepareInterstitial',
97+
!!data.adUnitId,
98+
),
99+
)
100+
.catch(
101+
async () =>
102+
await this.helper.updateItem(
103+
this.eventItems,
104+
'prepareInterstitial',
105+
false,
106+
),
107+
);
74108

75109
await AdMob.showInterstitial()
76-
.then(async () => await this.helper.updateItem(this.eventItems,'showInterstitial', true))
77-
.catch(async () => await this.helper.updateItem(this.eventItems,'showInterstitial', false));
110+
.then(
111+
async () =>
112+
await this.helper.updateItem(
113+
this.eventItems,
114+
'showInterstitial',
115+
true,
116+
),
117+
)
118+
.catch(
119+
async () =>
120+
await this.helper.updateItem(
121+
this.eventItems,
122+
'showInterstitial',
123+
false,
124+
),
125+
);
78126
}
79127

80128
ionViewWillLeave() {
81129
this.listenerHandlers.forEach(handler => handler.remove());
82130
}
83131
}
84-

demo/angular/src/app/validation/tab3/tab3.page.ts

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {Component, NgZone} from '@angular/core';
2-
import {AdMob, RewardAdPluginEvents} from '@capacitor-community/admob';
3-
import {ITestItems} from '../../shared/interfaces';
4-
import {ViewDidEnter, ViewWillEnter, ViewWillLeave} from '@ionic/angular';
5-
import {PluginListenerHandle} from '@capacitor/core';
6-
import {rewardOptions} from '../../shared/ad.options';
7-
import {HelperService} from '../../shared/helper.service';
1+
import { Component, NgZone } from '@angular/core';
2+
import { AdMob, RewardAdPluginEvents } from '@capacitor-community/admob';
3+
import { ITestItems } from '../../shared/interfaces';
4+
import { ViewDidEnter, ViewWillEnter, ViewWillLeave } from '@ionic/angular';
5+
import { PluginListenerHandle } from '@capacitor/core';
6+
import { rewardOptions } from '../../shared/ad.options';
7+
import { HelperService } from '../../shared/helper.service';
88

9-
const tryItems: ITestItems [] = [
9+
const tryItems: ITestItems[] = [
1010
{
1111
type: 'method',
1212
name: 'prepareRewardVideoAd',
@@ -17,19 +17,19 @@ const tryItems: ITestItems [] = [
1717
},
1818
{
1919
type: 'event',
20-
name: RewardAdPluginEvents.Loaded
20+
name: RewardAdPluginEvents.Loaded,
2121
},
2222
{
2323
type: 'event',
24-
name: RewardAdPluginEvents.Showed
24+
name: RewardAdPluginEvents.Showed,
2525
},
2626
{
2727
type: 'event',
28-
name: RewardAdPluginEvents.Rewarded
28+
name: RewardAdPluginEvents.Rewarded,
2929
},
3030
{
3131
type: 'event',
32-
name: RewardAdPluginEvents.Dismissed
32+
name: RewardAdPluginEvents.Dismissed,
3333
},
3434
{
3535
type: 'method',
@@ -45,25 +45,42 @@ const tryItems: ITestItems [] = [
4545
@Component({
4646
selector: 'app-tab3',
4747
templateUrl: 'tab3.page.html',
48-
styleUrls: ['tab3.page.scss']
48+
styleUrls: ['tab3.page.scss'],
4949
})
50-
export class Tab3Page implements ViewDidEnter, ViewWillEnter, ViewWillLeave {
50+
export class Tab3Page implements ViewDidEnter, ViewWillEnter, ViewWillLeave {
5151
private readonly listenerHandlers: PluginListenerHandle[] = [];
5252
public eventItems: ITestItems[] = [];
53-
constructor(
54-
private helper: HelperService,
55-
) {}
53+
constructor(private helper: HelperService) {}
5654

5755
ionViewWillEnter() {
5856
const eventKeys = Object.keys(RewardAdPluginEvents);
5957
eventKeys.forEach(key => {
60-
const handler = AdMob.addListener(RewardAdPluginEvents[key], (value) => {
58+
const handler = AdMob.addListener(RewardAdPluginEvents[key], value => {
6159
if (key === 'Dismissed') {
6260
AdMob.prepareRewardVideoAd({ adId: 'failed' })
63-
.then(async () => await this.helper.updateItem(this.eventItems,'prepareRewardVideoAdFailed', false))
64-
.catch(async () => await this.helper.updateItem(this.eventItems,'prepareRewardVideoAdFailed', true));
61+
.then(
62+
async () =>
63+
await this.helper.updateItem(
64+
this.eventItems,
65+
'prepareRewardVideoAdFailed',
66+
false,
67+
),
68+
)
69+
.catch(
70+
async () =>
71+
await this.helper.updateItem(
72+
this.eventItems,
73+
'prepareRewardVideoAdFailed',
74+
true,
75+
),
76+
);
6577
}
66-
this.helper.updateItem(this.eventItems,RewardAdPluginEvents[key], true, value);
78+
this.helper.updateItem(
79+
this.eventItems,
80+
RewardAdPluginEvents[key],
81+
true,
82+
value,
83+
);
6784
});
6885
this.listenerHandlers.push(handler);
6986
});
@@ -73,11 +90,39 @@ export class Tab3Page implements ViewDidEnter, ViewWillEnter, ViewWillLeave {
7390

7491
async ionViewDidEnter() {
7592
await AdMob.prepareRewardVideoAd(rewardOptions)
76-
.then(async () => await this.helper.updateItem(this.eventItems,'prepareRewardVideoAd', true))
77-
.catch(async () => await this.helper.updateItem(this.eventItems,'prepareRewardVideoAd', false));
93+
.then(
94+
async data =>
95+
await this.helper.updateItem(
96+
this.eventItems,
97+
'prepareRewardVideoAd',
98+
!!data.adUnitId,
99+
),
100+
)
101+
.catch(
102+
async () =>
103+
await this.helper.updateItem(
104+
this.eventItems,
105+
'prepareRewardVideoAd',
106+
false,
107+
),
108+
);
78109
await AdMob.showRewardVideoAd()
79-
.then(async () => await this.helper.updateItem(this.eventItems,'showRewardVideoAd', true))
80-
.catch(async () => await this.helper.updateItem(this.eventItems,'showRewardVideoAd', false));
110+
.then(
111+
async () =>
112+
await this.helper.updateItem(
113+
this.eventItems,
114+
'showRewardVideoAd',
115+
true,
116+
),
117+
)
118+
.catch(
119+
async () =>
120+
await this.helper.updateItem(
121+
this.eventItems,
122+
'showRewardVideoAd',
123+
false,
124+
),
125+
);
81126
}
82127

83128
ionViewWillLeave() {

ios/Plugin/Interstitial/AdInterstitialExecutor.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class AdInterstitialExecutor: NSObject, GADFullScreenContentDelegate {
2626
self.plugin?.notifyListeners(InterstitialAdPluginEvents.Loaded.rawValue, data: [
2727
"adUnitId": adUnitID
2828
])
29-
call.resolve([:])
29+
call.resolve([
30+
"adUnitId": adUnitID
31+
])
3032
}
3133
)
3234
}

ios/Plugin/Rewarded/AdRewardExecutor.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ class AdRewardExecutor: NSObject, GADFullScreenContentDelegate {
4343
self.plugin?.notifyListeners(RewardAdPluginEvents.Loaded.rawValue, data: [
4444
"adUnitId": adUnitID
4545
])
46-
call.resolve([:])
46+
call.resolve([
47+
"adUnitId": adUnitID
48+
])
4749
}
4850
)
4951
}

0 commit comments

Comments
 (0)