-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathDW.AdMobBannerAd.iOS.pas
305 lines (260 loc) · 9.93 KB
/
DW.AdMobBannerAd.iOS.pas
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
unit DW.AdMobBannerAd.iOS;
{*******************************************************}
{ }
{ Kastri }
{ }
{ Delphi Worlds Cross-Platform Library }
{ }
{ Copyright 2020-2024 Dave Nottage under MIT license }
{ which is located in the root folder of this library }
{ }
{*******************************************************}
interface
implementation
uses
// RTL
System.TypInfo, System.Types,
// macOS
Macapi.ObjectiveC, Macapi.Helpers,
// iOS
iOSapi.UIKit, iOSapi.Foundation, iOSapi.CoreGraphics, iOSapi.Helpers,
// FMX
FMX.Presentation.Messages, FMX.Presentation.iOS, FMX.Presentation.Factory, FMX.Controls, FMX.Controls.Presentation, FMX.Controls.Model, FMX.Types,
// DW
DW.iOSapi.UIKit, DW.iOSapi.GoogleMobileAds, DW.AdMob, DW.AdMobBannerAd;
type
IGADBannerView = interface(iOSapi.UIKit.UIView)
['{D46FDA3D-B11A-4B0D-861C-169655ADBEBE}']
end;
TiOSAdMobBannerAd = class;
TGADBannerViewDelegate = class(TOCLocal, GADBannerViewDelegate)
private
FBannerAd: TiOSAdMobBannerAd;
public
{ GADBannerViewDelegate }
procedure bannerView(bannerView: GADBannerView; didFailToReceiveAdWithError: NSError); cdecl;
procedure bannerViewDidDismissScreen(bannerView: GADBannerView); cdecl;
procedure bannerViewDidReceiveAd(bannerView: GADBannerView); cdecl;
procedure bannerViewDidRecordClick(bannerView: GADBannerView); cdecl;
procedure bannerViewDidRecordImpression(bannerView: GADBannerView); cdecl;
procedure bannerViewWillDismissScreen(bannerView: GADBannerView); cdecl;
procedure bannerViewWillPresentScreen(bannerView: GADBannerView); cdecl;
public
constructor Create(const ABannerAd: TiOSAdMobBannerAd);
end;
TOpenAdMobBannerAd = class(TCustomAdMobBannerAd);
TiOSAdMobBannerAd = class(TiOSNativeControl)
private
FBannerView: GADBannerView;
FDelegate: TGADBannerViewDelegate;
function AdControl: TOpenAdMobBannerAd;
procedure AdUnitIdChanged;
procedure AdSizeChanged;
function GetAdSize: GADAdSize;
function GetModel: TCustomAdMobBannerAdModel;
protected
function GetObjectiveCClass: PTypeInfo; override;
function DefineModelClass: TDataModelClass; override;
procedure AdClicked;
procedure AdClosed;
procedure AdFailedToLoad(const AAdError: NSError);
procedure AdImpression;
procedure AdLoaded;
procedure AdOpened;
procedure MMAdSizeChanged(var AMessage: TDispatchMessage); message MM_BANNER_ADSIZE_CHANGED;
procedure MMAdUnitIdChanged(var AMessage: TDispatchMessage); message MM_BANNER_ADUNITID_CHANGED;
procedure MMLoadAd(var AMessage: TDispatchMessage); message MM_BANNER_LOADAD;
procedure MMTestModeChanged(var AMessage: TDispatchMessage); message MM_BANNER_TESTMODE_CHANGED;
procedure PMGetAdjustedSize(var AMessage: TDispatchMessageWithValue<TSizeF>); message PM_GET_ADJUST_SIZE;
procedure PMGetAdjustedType(var AMessage: TDispatchMessageWithValue<TAdjustType>); message PM_GET_ADJUST_TYPE;
procedure PMGetRecommendSize(var AMessage: TDispatchMessageWithValue<TSizeF>); message PM_GET_RECOMMEND_SIZE;
public
constructor Create; override;
destructor Destroy; override;
property Model: TCustomAdMobBannerAdModel read GetModel;
end;
function SharedApplication: UIApplication;
begin
Result := TUIApplication.OCClass.sharedApplication;
end;
{ TGADBannerViewDelegate }
constructor TGADBannerViewDelegate.Create(const ABannerAd: TiOSAdMobBannerAd);
begin
inherited Create;
FBannerAd := ABannerAd;
end;
procedure TGADBannerViewDelegate.bannerView(bannerView: GADBannerView; didFailToReceiveAdWithError: NSError);
begin
FBannerAd.AdFailedToLoad(didFailToReceiveAdWithError);
end;
procedure TGADBannerViewDelegate.bannerViewDidDismissScreen(bannerView: GADBannerView);
begin
FBannerAd.AdClosed;
end;
procedure TGADBannerViewDelegate.bannerViewDidReceiveAd(bannerView: GADBannerView);
begin
FBannerAd.AdLoaded;
end;
procedure TGADBannerViewDelegate.bannerViewDidRecordClick(bannerView: GADBannerView);
begin
FBannerAd.AdClicked;
end;
procedure TGADBannerViewDelegate.bannerViewDidRecordImpression(bannerView: GADBannerView);
begin
FBannerAd.AdImpression;
end;
procedure TGADBannerViewDelegate.bannerViewWillDismissScreen(bannerView: GADBannerView);
begin
// Not implemented
end;
procedure TGADBannerViewDelegate.bannerViewWillPresentScreen(bannerView: GADBannerView);
begin
FBannerAd.AdOpened;
end;
{ TiOSAdMobBannerAd }
constructor TiOSAdMobBannerAd.Create;
var
LConstraint: NSLayoutConstraint;
begin
inherited;
FBannerView := TGADBannerView.Wrap(TGADBannerView.Alloc.initWithAdSize(GADAdSizeFromCGSize(CGSizeMake(0, 0))));
FBannerView.setTranslatesAutoresizingMaskIntoConstraints(False);
FDelegate := TGADBannerViewDelegate.Create(Self);
FBannerView.setDelegate(FDelegate.GetObjectID);
FBannerView.setRootViewController(SharedApplication.keyWindow.rootViewController);
View.addSubview(iOSapi.UIKit.TUIView.Wrap(NSObjectToID(FBannerView)));
LConstraint := TNSLayoutConstraint.Wrap(TNSLayoutConstraint.OCClass.constraintWithItem(NSObjectToID(FBannerView),
NSLayoutAttributeTop, NSLayoutRelationEqual, NSObjectToID(View), NSLayoutAttributeTop, 1, 0));
LConstraint.setActive(True);
LConstraint := TNSLayoutConstraint.Wrap(TNSLayoutConstraint.OCClass.constraintWithItem(NSObjectToID(FBannerView),
NSLayoutAttributeBottom, NSLayoutRelationEqual, NSObjectToID(View), NSLayoutAttributeBottom, 1, 0));
LConstraint.setActive(True);
LConstraint := TNSLayoutConstraint.Wrap(TNSLayoutConstraint.OCClass.constraintWithItem(NSObjectToID(FBannerView),
NSLayoutAttributeLeft, NSLayoutRelationEqual, NSObjectToID(View), NSLayoutAttributeLeft, 1, 0));
LConstraint.setActive(True);
LConstraint := TNSLayoutConstraint.Wrap(TNSLayoutConstraint.OCClass.constraintWithItem(NSObjectToID(FBannerView),
NSLayoutAttributeRight, NSLayoutRelationEqual, NSObjectToID(View), NSLayoutAttributeRight, 1, 0));
LConstraint.setActive(True);
View.updateConstraints;
end;
destructor TiOSAdMobBannerAd.Destroy;
begin
FDelegate.Free;
inherited;
end;
function TiOSAdMobBannerAd.AdControl: TOpenAdMobBannerAd;
begin
Result := TOpenAdMobBannerAd(Control);
end;
procedure TiOSAdMobBannerAd.AdClicked;
begin
AdControl.DoAdClicked;
end;
procedure TiOSAdMobBannerAd.AdClosed;
begin
AdControl.DoAdClosed;
end;
procedure TiOSAdMobBannerAd.AdFailedToLoad(const AAdError: NSError);
var
LError: TAdError;
begin
LError.ErrorCode := AAdError.code;
LError.Message := NSStrToStr(AAdError.localizedDescription);
AdControl.DoAdFailedToLoad(LError);
end;
procedure TiOSAdMobBannerAd.AdImpression;
begin
AdControl.DoAdImpression;
end;
procedure TiOSAdMobBannerAd.AdLoaded;
begin
AdControl.Height := GetAdSize.size.height;
AdControl.DoAdLoaded;
end;
procedure TiOSAdMobBannerAd.AdOpened;
begin
AdControl.DoAdOpened;
end;
function TiOSAdMobBannerAd.DefineModelClass: TDataModelClass;
begin
Result := TCustomAdMobBannerAdModel;
end;
function TiOSAdMobBannerAd.GetAdSize: GADAdSize;
begin
case Model.AdSize of
TAdMobBannerAdSize.Banner:
Result := kGADAdSizeBanner;
TAdMobBannerAdSize.FullBanner:
Result := kGADAdSizeFullBanner;
TAdMobBannerAdSize.LargeBanner:
Result := kGADAdSizeLargeBanner;
TAdMobBannerAdSize.LeaderBoard:
Result := kGADAdSizeLeaderboard;
TAdMobBannerAdSize.MediumRectangle:
Result := kGADAdSizeMediumRectangle;
TAdMobBannerAdSize.Adaptive:
Result := GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(ParentView.frame.Size.Width);
else
Result := kGADAdSizeBanner;
end;
Result := GADAdSizeFromCGSize(Result.size);
end;
function TiOSAdMobBannerAd.GetModel: TCustomAdMobBannerAdModel;
begin
Result := inherited GetModel<TCustomAdMobBannerAdModel>;
end;
function TiOSAdMobBannerAd.GetObjectiveCClass: PTypeInfo;
begin
Result := TypeInfo(IGADBannerView);
end;
procedure TiOSAdMobBannerAd.AdUnitIdChanged;
begin
if Model.TestMode then
FBannerView.setAdUnitID(StrToNSStr(cTestAdUnitIdBanner))
else
FBannerView.setAdUnitID(StrToNSStr(Model.AdUnitID));
end;
procedure TiOSAdMobBannerAd.AdSizeChanged;
var
LAdSize: GADAdSize;
begin
LAdSize := GetAdSize;
Control.Size.Size := TSizeF.Create(LAdSize.size.width, LAdSize.size.height);
View.layoutSubviews;
FBannerView.setNeedsLayout;
FBannerView.layoutIfNeeded;
FBannerView.setAdSize(LAdSize);
end;
procedure TiOSAdMobBannerAd.MMAdSizeChanged(var AMessage: TDispatchMessage);
begin
AdSizeChanged;
end;
procedure TiOSAdMobBannerAd.MMAdUnitIdChanged(var AMessage: TDispatchMessage);
begin
AdUnitIdChanged;
end;
procedure TiOSAdMobBannerAd.MMLoadAd(var AMessage: TDispatchMessage);
begin
FBannerView.loadRequest(TGADRequest.Create);
end;
procedure TiOSAdMobBannerAd.MMTestModeChanged(var AMessage: TDispatchMessage);
begin
AdUnitIdChanged;
end;
procedure TiOSAdMobBannerAd.PMGetAdjustedSize(var AMessage: TDispatchMessageWithValue<TSizeF>);
begin
AMessage.Value := GetAdSize.size.ToSizeF;
end;
procedure TiOSAdMobBannerAd.PMGetAdjustedType(var AMessage: TDispatchMessageWithValue<TAdjustType>);
begin
AMessage.Value := TAdjustType.FixedSize;
end;
procedure TiOSAdMobBannerAd.PMGetRecommendSize(var AMessage: TDispatchMessageWithValue<TSizeF>);
begin
//
end;
initialization
TPresentationProxyFactory.Current.Register(TAdMobBannerAd, TControlType.Platform, TiOSPresentationProxy<TiOSAdMobBannerAd>);
finalization
TPresentationProxyFactory.Current.Unregister(TAdMobBannerAd, TControlType.Platform, TiOSPresentationProxy<TiOSAdMobBannerAd>);
end.