Unable to use call a delegate


#1

I’m working on a plugin for WKWebView… and I got this weird warning message when using that plugin in my project.

/// wkwebview.ios.ts
/// <reference path="./references.d.ts" />

import { View } from "ui/core/view";

declare var NSURLRequest: any, NSURL: any, WKNavigationDelegate: any, WKWebView: any;

class WKNavigationDelegateImpl extends NSObject implements WKNavigationDelegate {
  public static ObjCProtocols = [WKNavigationDelegate];

  private _owner:WeakRef<NSWKWebView>;

  public static initWithOwner(owner: WeakRef<NSWKWebView>): WKNavigationDelegateImpl {
    let delegate = <WKNavigationDelegateImpl>WKNavigationDelegateImpl.new();
    delegate._owner = owner;
    return delegate;
  }

  public webViewdidStartProvisionalNavigation(webView: WKWebView, navigation: WKNavigation) {
    console.log("webViewdidStartProvisionalNavigation");
  }

  public webViewdidFinishNavigation(webView: WKWebView, navigation: WKNavigation) {
    console.log("webViewdidFinishNavigation");
  }

  public webViewdidFailNavigationwithError(webView: WKWebView, navigation: WKNavigation, error: NSError) {
    console.log("webViewdidFailNavigationwithError");
  }

  public webViewdecidePolicyForNavigationActiondecisionHandler(webView: WKWebView, navigationAction: WKNavigationAction, decisionHandler: WKNavigationActionPolicy) {
    console.log("webViewdecidePolicyForNavigationActiondecisionHandler");
  }
}

export class NSWKWebView extends View {

  private _ios: WKWebView;
  private _navigationDelegate: any;

  constructor() {
    super();

    this._ios = WKWebView.new();
    this._navigationDelegate = WKNavigationDelegateImpl.initWithOwner(new WeakRef(this));
  }

  get ios(): WKWebView {
    return this._ios;
  }

  public loadUrl(url: string) {
    this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url)));
  }

  public loadHTMLStringBaseURL(htmlString: string, baseUrl: string) {
    this._ios.loadHTMLStringBaseURL(htmlString, NSURL.URLWithString(baseUrl));
  }

  public onLoaded() {
    super.onLoaded();

    console.log("Loaded - 1");

    this._ios.navigationDelegate = this._navigationDelegate;
  }

  public onUnloaded() {
    this._ios.navigationDelegate = null;
    super.onUnloaded();
  }
}

/// webview.component.ts
...
import {registerElement} from "nativescript-angular/element-registry";
registerElement("WKWebView", () => require("nativescript-wkwebview").NSWKWebView);
...
// webview.html
<GridLayout rows="75 *">
  <GridLayout row="0" rows="*" columns="50 *" class="form">
    <Button class="btn btn-primary btn-active" row="0" col="0" text="&#xea44;" (tap)="goBack()"></Button>   
    <TextField id="urlField" #tf row="0" col="1" hint="Enter URL" [(ngModel)]="url"  returnKeyType="done"
        (returnPress)="submit(tf.text)" autocorrect="false" verticalAlignment="center" class="input input-border m-t-0"
        autocapitalizationType="none" ></TextField>
  </GridLayout>
  <WKWebView row="1" id="wv"></WKWebView>
</GridLayout>

As soon as WebViewComponent is loaded, I get this error.

The TypeScript constructor "WKNavigationDelegateImpl" will not be executed.

What does this exactly mean?? When requesting some URL to WKWebView, it actually loads something, but no logs I defined in WKNavigationDelegateImpl are printed.


#2

The “warning” The TypeScript constructor "WKNavigationDelegateImpl" will not be executed. is just telling you that the constructor will never be called; it is a completely normal warning generated because you are using TypeScript to extend a NSObject. If you were using JavaScript you wouldn’t see this warning. :slight_smile:

So unless you were actually using a constructor; this warning you can totally ignore. (Your code snippit you are using initWithOwner, so you aren’t using a constructor – so you are totally fine and you can just ignore the warning).


#3

Good to know that I could just ignore it :slight_smile: Thank you.