forked from nextgeniuspro/QtAdMob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtAdMobInterstitialIos.mm
224 lines (179 loc) · 4.73 KB
/
QtAdMobInterstitialIos.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "QtAdMobInterstitialIos.h"
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
#include <GoogleMobileAds/GADInterstitial.h>
#include "IQtAdMobBanner.h"
class QtAdMobInterstitialIosProtected
{
public:
QtAdMobInterstitialIosProtected() {};
~QtAdMobInterstitialIosProtected() {};
static void OnLoaded(QtAdMobInterstitialIos* handler, bool status)
{
if (!handler)
{
return;
}
handler->onStatusChanged(status);
}
static void OnLoading(QtAdMobInterstitialIos* handler)
{
if (!handler)
{
return;
}
handler->loading();
}
static void OnWillPresent(QtAdMobInterstitialIos* handler)
{
if (!handler)
{
return;
}
handler->willPresent();
}
static void OnClosed(QtAdMobInterstitialIos* handler)
{
if (!handler)
{
return;
}
handler->closed();
}
};
@interface QtAdMobInterstitialDelegate : NSObject<GADInterstitialDelegate>
@property (nonatomic, strong) GADInterstitial* interstitial;
@property (nonatomic, strong) NSMutableArray* testDevices;
@property (nonatomic, assign) QtAdMobInterstitialIos *handler;
- (id)initWithHandler:(QtAdMobInterstitialIos *)handler
adUnitId:(NSString *)adUnitId;
- (void)load;
@end
@implementation QtAdMobInterstitialDelegate
- (id)initWithHandler:(QtAdMobInterstitialIos *)handler adUnitId:(NSString *)adUnitId
{
self = [super init];
if (self)
{
_handler = handler;
_testDevices = [[NSMutableArray alloc] initWithArray:@[kGADSimulatorID]];
_interstitial = [[GADInterstitial alloc] initWithAdUnitID:adUnitId];
_interstitial.delegate = self;
}
return self;
}
- (void)dealloc
{
_interstitial.delegate = nil;
_handler = nullptr;
}
- (void)load
{
GADRequest *request = [GADRequest request];
request.testDevices = _testDevices;
[_interstitial loadRequest:request];
QtAdMobInterstitialIosProtected::OnLoading(self.handler);
}
- (void)show
{
UIApplication *application = [UIApplication sharedApplication];
UIWindow *window = [[application windows] firstObject];
UIViewController* rootViewController = [window rootViewController];
[_interstitial presentFromRootViewController:rootViewController];
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
Q_UNUSED(ad);
QtAdMobInterstitialIosProtected::OnLoaded(self.handler, true);
}
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
Q_UNUSED(ad);
Q_UNUSED(error);
QtAdMobInterstitialIosProtected::OnLoaded(self.handler, false);
}
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad
{
Q_UNUSED(ad);
QtAdMobInterstitialIosProtected::OnWillPresent(self.handler);
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad
{
Q_UNUSED(ad);
QtAdMobInterstitialIosProtected::OnClosed(self.handler);
}
@end
QtAdMobInterstitialIos::QtAdMobInterstitialIos()
: m_AdMob(nil)
, m_IsNeedToShow(false)
{
}
QtAdMobInterstitialIos::~QtAdMobInterstitialIos()
{
}
void QtAdMobInterstitialIos::setUnitId(const QString& unitId)
{
if (isValid() &&
!m_AdMob.interstitial.hasBeenUsed)
{
return;
}
m_IsNeedToShow = false;
QtAdMobInterstitialDelegate *delegate = [[QtAdMobInterstitialDelegate alloc] initWithHandler:this
adUnitId:[NSString stringWithUTF8String:unitId.toUtf8().data()]];
m_AdMob = (__bridge QtAdMobInterstitialDelegate*)(__bridge_retained void*)delegate;
[m_AdMob load];
m_UnitId = unitId;
}
const QString& QtAdMobInterstitialIos::unitId() const
{
return m_UnitId;
}
void QtAdMobInterstitialIos::setVisible(bool isVisible)
{
if (!isVisible)
{
return; // TODO: implement hide
}
if (isValid() && isLoaded())
{
[m_AdMob show];
}
else
{
m_IsNeedToShow = true;
}
}
bool QtAdMobInterstitialIos::visible()
{
return false; // TODO: add implemention
}
bool QtAdMobInterstitialIos::isLoaded()
{
if (!isValid())
{
return false;
}
return m_AdMob.interstitial.isReady;
}
void QtAdMobInterstitialIos::addTestDevice(const QString& hashedDeviceId)
{
NSString *deviceId = [NSString stringWithUTF8String:hashedDeviceId.toUtf8().data()];
[m_AdMob.testDevices addObject:deviceId];
}
void QtAdMobInterstitialIos::onStatusChanged(bool status)
{
if (!status)
{
m_AdMob = nil;
}
else if (m_IsNeedToShow)
{
[m_AdMob show];
}
emit loaded();
}
bool QtAdMobInterstitialIos::isValid() const
{
return (m_AdMob != nil);
}
#endif // TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR