forked from JanDamek/AdMobPackage_Delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdMob.pas
159 lines (133 loc) · 3.51 KB
/
AdMob.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
unit AdMob;
interface
uses
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.StdCtrls,
FMX.Forms, FMX.Layouts,
DPF.iOS.Common
{$IFDEF iOS}
, FMX.Media.iOS, FMX.Media, iOSapi.AVFoundation, iOSapi.Foundation,
System.Types, Posix.Dlfcn, Macapi.ObjectiveC, Macapi.ObjCRuntime,
FMX.Platform.iOS, iOSapi.CocoaTypes, iOSapi.uikit, iOSapi.CoreGraphics,
DPF.iOS.MFMailComposeViewController, DPF.iOS.StoreKit, DPF.iOS.SCNetworks
{$ENDIF}
;
{$IFDEF iOS}
type
AdMobLibrary = interface(NSObject)
['{AAF116B8-58D7-4A28-B755-ADDA11442E09}']
procedure setAdMob_id(AdMob_Id: NSString); cdecl;
function getBannerView(GADAdSize: integer): UIView; cdecl;
end;
AdMobLibraryClass = interface(NSObjectClass)
['{689B04E2-F080-437F-8F49-26CE0AD2730F}']
end;
TAdMobLibraryClass = class(TOCGenericImport<AdMobLibraryClass, AdMobLibrary>)
end;
{$ENDIF}
const
kGADAdSizeBanner = 1;
kGADAdSizeMediumRectangle = 2;
kGADAdSizeFullBanner = 3;
kGADAdSizeLeaderboard = 4;
kGADAdSizeSkyscraper = 5;
kGADAdSizeSmartBannerPortrait = 6;
kGADAdSizeSmartBannerLandscape = 7;
kGADAdSizeInvalid = 8;
type
[ComponentPlatformsAttribute(pidiOSDevice { or pidAndroid } )]
TAdMob = class(TLayout)
private
{ Private declarations }
{$IFDEF iOS}
FAdMob: AdMobLibrary;
{$ENDIF}
FAdMobSize: integer;
FAdMObId: String;
procedure SetAdMObId(const Value: String);
function GetAdMobSize: integer;
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property AdMobSize: integer read GetAdMobSize write FAdMobSize;
property AdMobID: String read FAdMObId write SetAdMObId;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('AdMob', [TAdMob]);
end;
{$IFDEF iOS}
{$O-}
function fakeLoader(I: integer): integer; cdecl;
external 'libAdMobLibrary.a' name 'fakeLoader';
function _NSStringFromGADAdSize: NSString; cdecl;
external 'libGoogleAdMobAds.a' name 'NSStringFromGADAdSize';
procedure fakeAdSupport; cdecl;
external '/System/Library/Frameworks/AdSupport.framework/AdSupport' name
'OBJC_CLASS_$_ASIdentifierManager';
{$O+}
{$ENDIF}
{ TAdMob }
constructor TAdMob.Create(AOwner: TComponent);
begin
inherited;
{$IFDEF iOS}
//fakeLoader(1);
FAdMob := TAdMobLibraryClass.Create;
FAdMob.init;
if FAdMobSize = 0 then
begin
if IsIPad then
FAdMobSize := kGADAdSizeLeaderboard
else
FAdMobSize := kGADAdSizeBanner;
end;
{$ENDIF}
end;
destructor TAdMob.Destroy;
begin
inherited;
end;
function TAdMob.GetAdMobSize: integer;
begin
if FAdMobSize = 0 then
begin
if IsIPad then
result := kGADAdSizeLeaderboard
else
result := kGADAdSizeBanner;
end
else
result := FAdMobSize;
end;
procedure TAdMob.Paint;
{$IFDEF iOS}
var
banner: UIView;
{$ENDIF}
begin
inherited;
{$IFDEF iOS}
if Assigned(Owner) and Assigned(TCommonCustomForm(Owner).Handle) then
begin
banner := FAdMob.getBannerView(Ord(FAdMobSize));
WindowHandleToPlatform(TCommonCustomForm(Owner).Handle)
.View.addSubview(banner)
end;
{$ENDIF}
end;
procedure TAdMob.SetAdMObId(const Value: String);
begin
FAdMObId := Value;
{$IFDEF iOS}
FAdMob.setAdMob_id(NSSTR(Value));
{$ENDIF}
end;
end.