Webview无法使用Delphi XE2在MacOS中显示

Phi*_*oux 73 delphi macos webkit freepascal delphi-xe2

我已经开始转换要在Delphi中使用的Webview接口.我已设法加载webkit库,并且调用的接口方法似乎正常工作,但是,我似乎无法在主窗体上显示Webview.

下面是我声明的接口

  WebFrameClass = interface(NSObjectClass)
  ['{7BE750C8-DFEC-4870-851A-12DBCB0B78F6}']
  end;

  WebFrame = interface(NSObject)
  ['{BCFA04BE-41AB-4B78-89C0-3330F12C7695}']
    procedure loadRequest(request: NSURLRequest); cdecl;
  end;
  TWebFrame = class(TOCGenericImport<WebFrameClass, WebFrame>)  end;

  WebViewClass = interface(NSViewClass)
  ['{0D9F44B7-09FD-4E35-B96E-8DB71B9A2537}']
    {class} function canShowMIMEType(MIMEType: NSString): Boolean; cdecl;
  end;

  WebView = interface(NSView)
  ['{C36D8016-2FCB-49F0-BA1C-C9913A37F9AC}']
    procedure clos; cdecl;
    procedure setHostWindow(hostWindow: NSWindow); cdecl;
    function initWithFrame(frame: NSRect; frameName: NSString; groupName: NSString): Pointer; cdecl;
    function mainFrame: WebFrame; cdecl;
  end;
  TWebView = class(TOCGenericImport<WebViewClass, WebView>)  end;
Run Code Online (Sandbox Code Playgroud)

以下是构建WebView的代码:

procedure TForm2.Button1Click(Sender: TObject);
var
  PWebView: Pointer;
  FwkMod: HMODULE;
  MyWebView: WebView;
  urlStr: NSURL;
  urlreq: NSURLRequest;
const
  WebKitFWK: string = '/System/Library/Frameworks/WebKit.framework/WebKit';
begin
  FwkMod := System.SysUtils.LoadLibrary(PWideChar(WebKitFWK));
  PWebView := TWebView.Alloc.initWithFrame(MakeNSRect(10, 10, 300, 300), nil, nil);
  MyWebView := TWebView.Wrap(PWebView);

  urlStr := TNSURL.Create;
  urlstr.initWithString(NSSTR('http://google.com.au/'));
  urlreq := TNSURLRequest.Create;
  urlreq.initWithURL(urlstr);
  MyWebView.mainFrame.loadRequest(urlreq);
end;
Run Code Online (Sandbox Code Playgroud)

代码执行时不会引发任何异常,但只是不想出现.在Delphi中需要做些什么?我找到的目标C的例子看起来很简单:

我见过的一些客观的C例子提到了IBOutlets.看起来这与Delphi无关.

如何使WebView OSX Xcode项目在启动时加载URL?

谢谢.

mos*_*o-x 7

Getting the NSWindow of a FMX form
将TForm引用转换为NSWindow
设置主机窗口.
MyWebView.setHostWindow(MyNSWindow)

procedure TForm2.Button1Click(Sender: TObject);
var
[...]
 ObjTOC: TOCLocal;
 MyNSWindow : NSWindow;
[...]  
 ObjTOC := (FmxHandleToObjC(Form2.Handle) as TOCLocal);
 MyNSWindow := NSWindow(TOCLocalAccess(ObjTOC).Super);

PWebView := TWebView.Alloc.initWithFrame(MakeNSRect(10, 10, 300, 300), nil, nil);
 MyWebView := TWebView.Wrap(PWebView);
 MyWebView.setHostWindow(MyNSWindow);

[...]
 MyWebView.mainFrame.loadRequest(urlreq);
end;
Run Code Online (Sandbox Code Playgroud)