locked
Is there a way to get a web page title from WKWebView RRS feed

  • Question

  • User396610 posted

    Hi, I need to get the page title of the current web page displayed in WKWebView. Is there a way to do this? Any help would be greatly appreciated. There only seems to be info out there for getting local html data.

    Friday, September 4, 2020 11:01 AM

Answers

  • User369978 posted

    You could choose one of the following ways.

    1. Evaluates JavaScript string.

    string title = await YourWeb.EvaluateJavaScriptAsync("document.title");

    1. WKWebView has a property Title , you could get it from custom renderer .

    ```

    Create custom class in Forms project

    public class MyWeb : WebView { public string Title { get; set; } ```

    ```

    ios project

    [assembly: ExportRenderer(typeof(MyWeb), typeof(MyRenderer))] namespace App1.iOS { public class MyRenderer : WkWebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e);

            MyWeb web = Element as MyWeb;
            web.Title = this.Title;
            this.NavigationDelegate = new MyNavigation(web);
        }
    }
    
    public class MyNavigation: WKNavigationDelegate
    {
        MyWeb web;
        public MyNavigation(MyWeb _web)
        {
            web = _web;
        }
    
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            base.DidFinishNavigation(webView, navigation);
    
            web.Title = webView.Title;
        }
    }
    

    }

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 7, 2020 6:54 AM

All replies

  • User369978 posted

    You could choose one of the following ways.

    1. Evaluates JavaScript string.

    string title = await YourWeb.EvaluateJavaScriptAsync("document.title");

    1. WKWebView has a property Title , you could get it from custom renderer .

    ```

    Create custom class in Forms project

    public class MyWeb : WebView { public string Title { get; set; } ```

    ```

    ios project

    [assembly: ExportRenderer(typeof(MyWeb), typeof(MyRenderer))] namespace App1.iOS { public class MyRenderer : WkWebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e);

            MyWeb web = Element as MyWeb;
            web.Title = this.Title;
            this.NavigationDelegate = new MyNavigation(web);
        }
    }
    
    public class MyNavigation: WKNavigationDelegate
    {
        MyWeb web;
        public MyNavigation(MyWeb _web)
        {
            web = _web;
        }
    
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            base.DidFinishNavigation(webView, navigation);
    
            web.Title = webView.Title;
        }
    }
    

    }

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 7, 2020 6:54 AM
  • User396610 posted

    Thank you ColeX, you've answered my question perfectly.

    Monday, September 7, 2020 8:35 AM