Forum > Cocoa
Minimum code for implementing WebView (deprecated) or WKWebView
MISV:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TmyWebBrowserCocoa = class(TWinControl)protectedpublic constructor Create(AOwner: TComponent); destructor Destroy; override; procedure Init_WebView; procedure Navigate(const AURL: string):end; TmyWebBrowserCocoaDelegate = objcclass(NSObject)public // implement those we needend; constructor TmyWebBrowserCocoa.Create(AOwner: TComponent);begin inherited Create(AOwner);end; procedure TmyWebBrowserCocoa.Init_WebView;var TmpWebView: WebView; TmpDelegate: TmyWebBrowserCocoaDelegate; TmpParams: TCreateParams;begin TmpParams.WndParent := Self.Handle; TmpParams.X := 0; TmpParams.Y := 0; TmpParams.Width := Self.ClientWidth; TmpParams.Width := Self.ClientHeight; TmpWebView := WebView(NSView(WebView.alloc).lclInitWithCreateParams(TmpParams)); TmpDelegate := TmyWebBrowserCocoaDelegate.alloc.init; TmpWebView.setFrameLoadDelegate(TmpDelegate);end; destructor TmyWebBrowserCocoa.Destroy;begin WebView(Self.Handle).frameLoadDelegate.release; NSObject(Self.Handle).release; inherited Destroy;end; procedure TmyWebBrowserCocoa.Navigate(const AURL: string);var ANSURL : NSURL; ANSReq : NSURLRequest;begin // to keep simple we are testing using https://example.com to avoid any charset and encoding problems ANSURL := NSURL.URLWithString(NSSTR(PAnsiChar(AURL))); ANSReq := NSURLRequest.RequestWithURL(ANSURL); WebView(Self.Handle).mainFrame.loadRequest(ANSReq); // SIGABRT hereend;
Created and called like this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var WebBrowser: TmyWebBrowserCocoabegin FWebBrowser := TmyWebBrowserCocoa.Create(FBrowserHostParent); FWebBrowser.Parent :=FBrowserHostParent; FWebBrowser.Visible := False; FWebBrowser.Align := alClient; FWebBrowser.Init_WebView; FWebBrowser.Visible := True; FWebBrowser.Navigate('https://example.com/');end;
However, I am getting SIGABRT inside navigate... probably a handle issue
Phil:
--- Quote from: MISV on July 24, 2018, 03:23:05 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.
--- End quote ---
TWebBrowser is "widgetset compatible code".
https://macpgmr.github.io
What are you trying to do that isn't already covered in TWebBrowser?
MISV:
--- Quote from: Phil on July 24, 2018, 03:33:49 pm ---
--- Quote from: MISV on July 24, 2018, 03:23:05 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.
--- End quote ---
TWebBrowser is "widgetset compatible code".
https://macpgmr.github.io
What are you trying to do that isn't already covered in TWebBrowser?
--- End quote ---
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.
MISV:
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:
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...
Navigation
[0] Message Index
[#] Next page