Forum > Cocoa

Minimum code for implementing WebView (deprecated) or WKWebView

<< < (3/13) > >>

wittbo:
Finished work. Try the demo.
Implemented close of the webview
Implemented the newer WKWebView.

The latter wasn't a real problem, since the cocoaint webkit unit now includes the wkwebview.inc include. For creating and closing no change. Loading of a page is a little bit easier, there is no mainframe necessary. WKWebView has some new magnification methods to read and set the magnification of the page.

The demo program contains both WebView and WKWebView methods; you can choose between them by setting the compiler defines at line 15/16.

I will use the WKWebView to integrate a PDF Viewer into my app and it's really quite easy, you only need the URL-variant of the PDF file.

If anyone knows something similar for MS Windows (10 or newer), I would be interested as well.

BTW: Something seems to be wrong with line 16 of .../wkwebview.inc:
{$if defined(TARGET_OS_IPHONE)defined(interface)defined(WKWebView)defined(UIView)}
When loading this file into the Lazarus ide, you get an error at line 16: "Error: operator missing". Since I could compile and run my program anyway, I did not pursue the message further.

MISV:
Just for completeness here is my original code:


I have a TWinControl hosting the browser contorl


So when parent control resizes I do


--- 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";}};} ---  if FWebView_Cocoa <> nil then    begin      TmpPoint.x := Self.Left;      TmpPoint.y := Self.Top;      FWebView_Cocoa.setFrameOrigin(TmpPoint);      TmpSize.width := Self.ClientWidth;      TmpSize.height := Self.ClientHeight;      FWebView_Cocoa.setFrameSize(TmpSize); 
To initialize I do:


--- 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";}};} ---  if FWebView_Cocoa = nil then    begin//(*      TmpParams.WndParent := Self.Handle;      TmpParams.X := 0;      TmpParams.Y := 0;      TmpParams.Width := Self.ClientWidth;      TmpParams.Height := Self.ClientHeight;      TmpParams.Style := WS_VISIBLE;      FWebView_Cocoa := WebView( NSView(WebView.alloc).lclInitWithCreateParams(TmpParams) );        FDelegate_Cocoa_Frame := Tmsdv_cocoanative_WebBrowserCocoaDelegate_Frame.alloc.init;      FDelegate_Cocoa_Frame.FBrowserCtrlRef := Self;      FWebView_Cocoa.setFrameLoadDelegate(FDelegate_Cocoa_Frame); 

Then 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";}};} ---  Tmsdv_cocoanative_WebBrowserCocoaDelegate_Frame = objcclass(NSObject)  private    FBrowserCtrlRef: TmsdvWebBrowserCocoa;  public  public    procedure webView_didFinishLoadForFrame(sender: WebView; frame: WebFrame); override;    procedure webView_didStartProvisionalLoadForFrame(sender: WebView; frame: WebFrame); override;    procedure webView_didFailProvisionalLoadWithError_forFrame(sender: WebView; error: NSError; frame: WebFrame); override;    procedure webView_decidePolicyForNewWindowAction_request_newFrameName_decisionListener(webView_: WebView; actionInformation: NSDictionary; request: NSURLRequest; frameName: NSString; listener: WebPolicyDecisionListenerProtocol); override;  end; 

Hope this helps


(Great work if wkwebview can be implemented now! I wonder if one needs ti support both for newer/older Mac OS and how to select between them)

trev:
* WKWebView requires macOS 10.10+
* WebView is available in macOS 10.3–10.14

As for selecting between them, something 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";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  aSEL : SEL;begin    // Check whether OS version responds to this selector    aSEL := objcselector('operatingSystemVersion');    if NSProcessInfo.processInfo.respondsToSelector(aSEL) then      ProcessInfo // procedure    // Otherwise fallback to the deprecated Gestalt Manager    else      GestaltInfo; // procedureend;

wittbo:
I have done some experiments with WKWebView in the meantime. The minimal use of the methods to open a WKWebView and load a web page are even a bit easier than with the WebView. However, I am not getting anywhere with the implementation of the delegate methods to check the loading process. With WebView you simply had to create an objcclass(NSObject) for the desired delegate methods and pass it to the Webview with setFrameLoadDelegate. This doesn't work with WKWebView now (or I didn't understand it). Here I need a WKNavigationDelegate protocol object. And that's where my options end, as I don't know how to implement this. Maybe someone has an idea?

MISV:
I will try return to this as well... I have begun initial work. I just have so little time, but it at top of priority for my software (however, personal matters in my life is taking higher priority than my software so this is no promise... but I will try see if I can somewhere. I have a ton of things I need to get working before I can replace the old webview)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version