-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuOpenViewUrl.pas
121 lines (112 loc) · 3.21 KB
/
uOpenViewUrl.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
unit uOpenViewUrl;
interface
// URLEncode is performed on the URL
// so you need to format it protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
implementation
uses
IdURI, SysUtils, Classes, FMX.Dialogs,
{$IFDEF MSWINDOWS}
ActiveX, ShellApi, Windows;
{$ENDIF MSWINDOWS}
{$IFDEF ANDROID}
FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.JavaTypes
, Androidapi.Helpers
;
{$ELSE}
{$IFDEF IOS}
iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF IOS}
{$ENDIF ANDROID}
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
Intent : JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI(URL));
{Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL)))); }
try
SharedActivity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
{$ELSE}
{$IFDEF IOS}
var
NSU: NSUrl;
begin
// iOS doesn't like spaces, so URL encode is important.
NSU := StrToNSUrl(TIdURI.URLEncode(URL));
if SharedApplication.canOpenURL(NSU) then
exit(SharedApplication.openUrl(NSU))
else
begin
if DisplayError then
ShowMessage('Error: Opening "' + URL + '" not supported.');
exit(false);
end;
end;
{$ELSE}
{$IFDEF MSWINDOWS}
procedure ShellExecute(const AWnd: HWND; const AOperation, AFileName: String; const AParameters: String = ''; const ADirectory: String = ''; const AShowCmd: Integer = SW_SHOWNORMAL);
var
ExecInfo: TShellExecuteInfo;
NeedUninitialize: Boolean;
begin
Assert(AFileName <> '');
NeedUninitialize := SUCCEEDED(CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE));
try
FillChar(ExecInfo, SizeOf(ExecInfo), 0);
ExecInfo.cbSize := SizeOf(ExecInfo);
ExecInfo.Wnd := AWnd;
ExecInfo.lpVerb := Pointer(AOperation);
ExecInfo.lpFile := PChar(AFileName);
ExecInfo.lpParameters := Pointer(AParameters);
ExecInfo.lpDirectory := Pointer(ADirectory);
ExecInfo.nShow := AShowCmd;
ExecInfo.fMask := SEE_MASK_NOASYNC { = SEE_MASK_FLAG_DDEWAIT äëÿ ñòàðûõ âåðñèé Delphi }
or SEE_MASK_FLAG_NO_UI;
{$IFDEF UNICODE}
ExecInfo.fMask := ExecInfo.fMask or SEE_MASK_UNICODE;
{$ENDIF}
{$WARN SYMBOL_PLATFORM OFF}
Win32Check(ShellExecuteEx(@ExecInfo));
{$WARN SYMBOL_PLATFORM ON}
finally
if NeedUninitialize then
CoUninitialize;
end;
end;
begin
try
ShellExecute(0, 'open', PChar(URL));
exit(true);
except
on e: Exception do
begin
if DisplayError then
ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
{$ELSE}
begin
raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}
{$ENDIF MSWINDOWS}
end.