Recent

Author Topic: Minimum code for implementing WebView (deprecated) or WKWebView  (Read 2110 times)

MISV

  • Hero Member
  • *****
  • Posts: 633
To get a better grib about how Cocoa works with LCL I decided I would like to show WebView in a control. I am aware Phil made something which I also used as a starting point. However - it seems when using widgetset compatible code things are a little different - or I am messing up badly. Either way, good learning experience.



So I have this

Code: Pascal  [Select]
  1. TmyWebBrowserCocoa = class(TWinControl)
  2. protected
  3. public
  4.   constructor Create(AOwner: TComponent);
  5.   destructor Destroy; override;
  6.   procedure Init_WebView;
  7.   procedure Navigate(const AURL: string):
  8. end;
  9.  
  10. TmyWebBrowserCocoaDelegate = objcclass(NSObject)
  11. public
  12.   // implement those we need
  13. end;
  14.  
  15. constructor TmyWebBrowserCocoa.Create(AOwner: TComponent);
  16. begin
  17.   inherited Create(AOwner);
  18. end;
  19.  
  20. procedure TmyWebBrowserCocoa.Init_WebView;
  21. var
  22.   TmpWebView: WebView;
  23.   TmpDelegate: TmyWebBrowserCocoaDelegate;
  24.   TmpParams: TCreateParams;
  25. begin
  26.   TmpParams.WndParent := Self.Handle;
  27.   TmpParams.X := 0;
  28.   TmpParams.Y := 0;
  29.   TmpParams.Width := Self.ClientWidth;
  30.   TmpParams.Width := Self.ClientHeight;
  31.   TmpWebView := WebView(NSView(WebView.alloc).lclInitWithCreateParams(TmpParams));
  32.   TmpDelegate := TmyWebBrowserCocoaDelegate.alloc.init;
  33.   TmpWebView.setFrameLoadDelegate(TmpDelegate);
  34. end;
  35.  
  36. destructor TmyWebBrowserCocoa.Destroy;
  37. begin
  38.   WebView(Self.Handle).frameLoadDelegate.release;
  39.   NSObject(Self.Handle).release;
  40.   inherited Destroy;
  41. end;
  42.  
  43. procedure TmyWebBrowserCocoa.Navigate(const AURL: string);
  44. var
  45.   ANSURL : NSURL;
  46.   ANSReq : NSURLRequest;
  47. begin
  48.   // to keep simple we are testing using https://example.com to avoid any charset and encoding problems
  49.   ANSURL := NSURL.URLWithString(NSSTR(PAnsiChar(AURL)));
  50.   ANSReq := NSURLRequest.RequestWithURL(ANSURL);
  51.   WebView(Self.Handle).mainFrame.loadRequest(ANSReq); // SIGABRT here
  52. end;
  53.  

Created and called like this

Code: Pascal  [Select]
  1. var
  2.   WebBrowser: TmyWebBrowserCocoa
  3. begin
  4.   FWebBrowser := TmyWebBrowserCocoa.Create(FBrowserHostParent);
  5.   FWebBrowser.Parent :=FBrowserHostParent;
  6.   FWebBrowser.Visible := False;
  7.   FWebBrowser.Align := alClient;
  8.   FWebBrowser.Init_WebView;
  9.   FWebBrowser.Visible := True;
  10.   FWebBrowser.Navigate('https://example.com/');
  11. end;



However, I am getting SIGABRT inside navigate... probably a handle issue
« Last Edit: July 26, 2018, 12:28:53 pm by MISV »

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Minimum code for implementing WebView
« Reply #1 on: July 24, 2018, 03:33:49 pm »
To get a better grib about how Cocoa works with LCL I decided I would like to show WebView in a control. I am aware Phil made something which I also used as a starting point. However - it seems when using widgetset compatible code things are a little different - or I am messing up badly. Either way, good learning experience.

TWebBrowser is "widgetset compatible code".

https://macpgmr.github.io

What are you trying to do that isn't already covered in TWebBrowser?

MISV

  • Hero Member
  • *****
  • Posts: 633
Re: Minimum code for implementing WebView
« Reply #2 on: July 24, 2018, 03:45:31 pm »
To get a better grib about how Cocoa works with LCL I decided I would like to show WebView in a control. I am aware Phil made something which I also used as a starting point. However - it seems when using widgetset compatible code things are a little different - or I am messing up badly. Either way, good learning experience.

TWebBrowser is "widgetset compatible code".

https://macpgmr.github.io

What are you trying to do that isn't already covered in TWebBrowser?

I explicit noted in my post your code was "widgetset compatible code" unlike mine :)

I am merely trying to learn where I have made an error in making a non-widgetset-compatible webview in cocoa.
« Last Edit: July 24, 2018, 03:47:28 pm by MISV »

MISV

  • Hero Member
  • *****
  • Posts: 633
Re: Minimum code for implementing WebView
« Reply #3 on: July 24, 2018, 11:50:30 pm »
Okay - I got it working.

Essentially not using the handle stuff but instead keep references (e.g. FWebView) and use them helped + some fixes along the way.

MISV

  • Hero Member
  • *****
  • Posts: 633
Re: Minimum code for implementing WebView (deprecated) or WKWebView
« Reply #4 on: July 26, 2018, 12:30:35 pm »
Quick note: You can resize your webview when the parent-wincontrol resize using setFrameOrigin + setFrameSize

One problem though...

Seems WebKit has been deprecated for a while in favour WKWebView, so WebKit support may be ending in a later macOS version...

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Minimum code for implementing WebView (deprecated) or WKWebView
« Reply #5 on: July 26, 2018, 02:04:32 pm »
Quick note: You can resize your webview when the parent-wincontrol resize using setFrameOrigin + setFrameSize

One problem though...

Seems WebKit has been deprecated for a while in favour WKWebView, so WebKit support may be ending in a later macOS version...

The WebView class has been deprecated in favor of the WKWebView class. The WebKit framework has not been deprecated.

https://developer.apple.com/documentation/webkit/wkwebview

FPC's ancient WebKit unit does not include the newer WKWebView class, but the MacOS_10_10 parsed headers here does:

https://github.com/genericptr

« Last Edit: July 26, 2018, 02:08:45 pm by Phil »