如何在 macOS 和 Windows 中使用默认应用程序打开 URL
2024-01-03 19:12:31
我目前正在将旧的 Delphi 5 桌面 VCL 应用程序更新为 Delphi 11.3 FMX。我想要提供的功能之一是能够从应用程序内启动多个网页。我想在主菜单中放置一个指向我的 YouTube 频道的链接,以便客户可以轻松观看产品视频。在“帮助”>“关于”框中还有一个指向我的网站的链接。
上次我使用 VCL 执行此操作时相当简单,因为我只需担心 Windows 方面的事情。然而,因为我希望这个应用程序能够在 Windows 和 macOS 上运行,所以它提出了一个挑战。
除非目标设置为 MacOS 64 位,否则 Delphi IDE 将无法识别 Macapi 命名空间
Harry Stahl 在他的书 Cross-Platform Development with Delphi 的第 98-99 页介绍了 COCOA API。他还给出了如何使用 Macapi.Appkit 的 NSWorkspace 对象的示例。但是,他没有展示如何设置 use 子句。
我还在 stackoverflow 上找到了 David Heffernan 于 2015 年撰写的精彩参考资料。但是,如果您正在寻找完整的答案,Heffernan 的文章有两个问题:
- 参考了 Malcolm Groves 的一篇博客文章,名为“在 OS X 中的默认应用程序中打开文件和 URL”,该文章不再可用或无法访问。
- 该示例并没有告诉您需要先以 MacOS 64 位平台为目标,然后 IDE 才能识别 Macapi 命名空间。
除非目标设置为 MacOS 64 位,否则 Delphi IDE 将无法识别 Macapi 命名空间。为我没有阅读 Embarcadero 文档而感到羞耻。我一直在思考如何使用 {$IFDEF MSWindows}和 {$IFDEF MACOS}有点棘手,但我最终还是明白了。
经过几个小时的反复研究适用于 Windows 但不适用于 macOS 的代码。对于 macOS 有效但不适用于 Windows 的代码,我终于让 Heffernan 的示例发挥作用了。
下一步是从主窗体中提取代码并将其放入其自己的单元中。这就是我今天与您分享的代码。我希望你觉得这有帮助。
这是源代码:
unit1.fmx
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 | object Form1: TForm1 Left = 0 Top = 0 Caption = 'Goto Website' ClientHeight = 238 ClientWidth = 478 Position = ScreenCenter FormFactor.Width = 320 FormFactor.Height = 480 FormFactor.Devices = [Desktop] DesignerMasterStyle = 0 object Text1: TText AutoSize = True Cursor = crHandPoint Position.X = 116.000000000000000000 Position.Y = 83.000000000000000000 Size.Width = 246.078125000000000000 Size.Height = 31.921875000000000000 Size.PlatformDefault = False Text = 'https://zilchworks.com' TextSettings.Font.Size = 24.000000000000000000 TextSettings.FontColor = claMediumblue OnClick = Text1Click OnMouseEnter = Text1MouseEnter OnMouseLeave = Text1MouseLeave endend |
Unit1.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 | unit Unit1;interfaceuses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects;type TForm1 = class(TForm) Text1: TText; procedure Text1MouseEnter(Sender: TObject); procedure Text1MouseLeave(Sender: TObject); procedure Text1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.fmx}uses u.OpenURL;{ TForm1 }procedure TForm1.Text1Click(Sender: TObject);begin GotoWebsite(Text1.Text);end;procedure TForm1.Text1MouseEnter(Sender: TObject);begin Text1.Font.Style := Text1.Font.Style + [TFontStyle.fsUnderline];end;procedure TForm1.Text1MouseLeave(Sender: TObject);begin Text1.Font.Style := Text1.Font.Style - [TFontStyle.fsUnderline];end;end. |
u.OpenUrl.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 | {+-------------------------------------------------------------------------------¦ Filename: u.OpenURL Unit¦ Author: Michael J. Riley¦ Website: https://zilchworks.com¦ YouTube: https://www.youtube.com/@CapeCodGunny¦ Copyright: © 2023-2024 by Michael J. Riley. All Rights Reserved.+-------------------------------------------------------------------------------¦ Purpose: Opens a url using the default browser on the users system.¦¦ OS: Windows, macOS+-------------------------------------------------------------------------------¦ Developer References:¦¦ Book: Cross-Platform Development with Delphi 10.2 & FireMonkey¦ Author: Harry Stahl¦ ISBN: https://isbnsearch.org/isbn/9781549545764¦ Notes: See pages 98-99.¦¦ Websites: https://stackoverflow.com/q/28858392/195983¦ https://stackoverflow.com/a/28859000/195983+-------------------------------------------------------------------------------¦ DISCLAIMER:¦¦ This source code is provided "as is" and without any warranty. Use it at your¦ own risk. The author(s) make no guarantees or assurances regarding the¦ correctness or functionality of the code, and disclaim any liability for¦ damages resulting from its use.¦¦ It is advisable to thoroughly review and test the code before deploying it in¦ any production environment.¦¦ By using this code, you agree to these terms and acknowledge that any issues¦ arising from its use are solely your responsibility.+-------------------------------------------------------------------------------}unit u.OpenURL;interface{$IFDEF MSWindows}uses Winapi.ShellAPI, Winapi.Windows;{$ENDIF}{$IFDEF MACOS}uses Macapi.AppKit, Macapi.Foundation, Macapi.Helpers; procedure macOSGotoWebsite(URL: string);{$ENDIF} procedure GotoWebsite(URL: string);implementationprocedure GotoWebsite(URL: string);begin {$IFDEF MSWindows} ShellExecute(GetDesktopWindow, 'open', PChar(URL), '', '', SW_SHOWNORMAL) {$ENDIF} {$IFDEF MACOS} macOSGotoWebsite(URL); {$ENDIF}end;{$IFDEF MACOS}procedure macOSGotoWebsite(URL: string);var macURL: NSURL; macWorkspace: NSWorkspace;begin macURL := TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSStr(URL))); macWorkspace := TNSWorkspace.Wrap(TNSWorkspace.OCClass.sharedWorkspace); macWorkspace.openURL(macURL);end;{$ENDIF}end. |
享受!
麻袋迈克
https://zilchworks.com
https://zilchworks.com

