-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPF.iOS.MFMessageCompose.pas
202 lines (176 loc) · 6.61 KB
/
DPF.iOS.MFMessageCompose.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
// ------------------------------------------------------------------------------
// DPF.iOS.MFMessageCompose Component
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: http://www.dpfaragir.com
//
// Developed By: Babak Yaghoobi
//
// Email #1: [email protected]
// EMail #2: [email protected]
// Email #3: [email protected]
//
// ------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ------------------------------------------------------------------------------
unit DPF.iOS.MFMessageCompose;
interface
{$I DPF.iOS.Defs.inc}
uses
System.SysUtils,
System.Classes,
System.Types,
System.UITypes,
System.Variants,
System.TypInfo,
DPF.iOS.BaseControl,
{$IFDEF IOS}
Macapi.ObjectiveC,
Macapi.ObjCRuntime,
IOSapi.CocoaTypes,
IOSapi.Foundation,
IOSapi.Uikit,
IOSapi.CoreGraphics,
IOSapi.CoreImage,
FMX.Platform.iOS,
DPF.iOS.Common,
DPF.iOS.MFMessageComposeViewController,
{$ENDIF}
FMX.Layouts, FMX.Memo,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls;
type
TDPFMessageCompose = class;
TDPFOnSendStatus = procedure( Sender: TObject; Result: Integer ) of object;
{$IFDEF IOS}
// ------------------------------------------------------------------------------
TMFMessageComposeViewControllerDelegate = class( TOCLocal, MFMessageComposeViewControllerDelegate )
private
FDPFMessageCompose: TDPFMessageCompose;
public
constructor Create( DPFMessageCompose: TDPFMessageCompose );
procedure messageComposeViewController( controller: MFMessageComposeViewController; didFinishWithResult: MFMessageComposeResult ); cdecl;
end;
{$ENDIF}
// ------------------------------------------------------------------------------
[ComponentPlatformsAttribute( PidWin32 or pidiOSDevice32 or pidiOSDevice64 or PidiOSSimulator )]
TDPFMessageCompose = class( TComponent )
private
FOnSendStatus: TDPFOnSendStatus;
{$IFDEF IOS}
FMainWindow : UIWindow;
FMessageCompose : MFMessageComposeViewController;
FMFMessageComposeViewControllerDelegate: TMFMessageComposeViewControllerDelegate;
RecipientsArr : NSMutableArray;
{$ENDIF}
protected
procedure ViewControllerCompletion;
public
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
function MessageCompose( Body: string; Recipients: array of string ): Boolean;
published
property OnSendStatus: TDPFOnSendStatus read FOnSendStatus write FOnSendStatus;
end;
// ------------------------------------------------------------------------------
implementation
// ------------------------------------------------------------------------------
{ TDPFMessageCompose }
// ------------------------------------------------------------------------------
constructor TDPFMessageCompose.Create( AOwner: TComponent );
begin
inherited Create( AOwner );
{$IFDEF IOS}
FMessageCompose := nil;
RecipientsArr := TNSMutableArray.Create;
FMFMessageComposeViewControllerDelegate := TMFMessageComposeViewControllerDelegate.Create( Self );
{$ENDIF}
end;
// ------------------------------------------------------------------------------
destructor TDPFMessageCompose.Destroy;
begin
{$IFDEF IOS}
RecipientsArr.release;
FMFMessageComposeViewControllerDelegate.DisposeOf;
if Assigned( FMessageCompose ) then
FMessageCompose.release;
{$ENDIF}
inherited;
end;
// ------------------------------------------------------------------------------
procedure TDPFMessageCompose.ViewControllerCompletion;
begin
end;
// ------------------------------------------------------------------------------
function TDPFMessageCompose.MessageCompose( Body: string; Recipients: array of string ): Boolean;
{$IFNDEF IOS}
begin
result := false;
end;
{$ELSE}
var
I: Integer;
begin
RecipientsArr.removeAllObjects;
result := TMFMessageComposeViewController.OCClass.canSendText;
if not Result then
begin
Exit;
end;
if not Assigned( FMessageCompose ) then
begin
FMessageCompose := TMFMessageComposeViewController.Create;
FMessageCompose.setMessageComposeDelegate( FMFMessageComposeViewControllerDelegate );
end;
FMessageCompose.setBody( NSSTR( Body ) );
// Recipients
if Length( Recipients ) > 0 then
begin
RecipientsArr := TNSMutableArray.Create;
for I := 0 to high( Recipients ) do
RecipientsArr.addObject( ( NSSTR( Recipients[I] ) as ILocalObject ).GetObjectID );
FMessageCompose.setRecipients( RecipientsArr );
end;
FMainWindow := GetSharedApplication.keyWindow;
if Assigned( FMainWindow ) and Assigned( FMainWindow.rootViewController ) then
FMainWindow.rootViewController.presentViewController( FMessageCompose, True, ViewControllerCompletion );
end;
// ------------------------------------------------------------------------------
{ TMFMessageComposeViewControllerDelegate }
constructor TMFMessageComposeViewControllerDelegate.Create( DPFMessageCompose: TDPFMessageCompose );
begin
inherited Create;
FDPFMessageCompose := DPFMessageCompose;
end;
// ------------------------------------------------------------------------------
procedure TMFMessageComposeViewControllerDelegate.messageComposeViewController( controller: MFMessageComposeViewController; didFinishWithResult: MFMessageComposeResult );
begin
case didFinishWithResult of
MessageComposeResultCancelled:
;
MessageComposeResultSent:
;
MessageComposeResultFailed:
;
end;
FDPFMessageCompose.FMainWindow.rootViewController.dismissModalViewControllerAnimated( True );
if Assigned( FDPFMessageCompose.FOnSendStatus ) then
FDPFMessageCompose.FOnSendStatus( FDPFMessageCompose, didFinishWithResult );
FDPFMessageCompose.FMessageCompose.release;
FDPFMessageCompose.FMessageCompose := nil;
end;
{$ENDIF}
// ------------------------------------------------------------------------------
end.