0

I am using Xamarin.Forms to build a cross platform IOS Android and windows phone app. One of my views is a web view which calls a url pointing to a page that has a javascript function in it. I need to call this function from within the mobile app, and pass it a string value.

So far all good, I achieve this in WindowsPhone by using a WebViewRenderer with a custom view. In the OnElementPropertyChanged handler in the renderer I can get get access to my view and the properties I need like so:

protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if(e.PropertyName == SessionWebView.SessionIdProperty.PropertyName)
    {
        view = Element as SessionWebView;               

        Control.LoadCompleted += Control_LoadCompleted;         
        if (view != null)
        {
            _currentSessionId = view.SessionId;
        }
    }
}

Above I attach the the load complete handler of the Control which is the actual BrowserControl. Then below I can call the desired JavaScript function in the webpage.

void Control_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{       
    if (!scriptCalled)
    {
        Control.IsScriptEnabled = true;
        var param = new string[1];
        param[0] = _currentSessionId;
        Control.InvokeScript("initWebView", param);
        scriptCalled = true;
    }       
}

This all works perfectly in Windows Phone. In IOS - which is what this question is about - I do something similar with a custom Rendered, And I can get my view and its properties.

However - I am unable to get the Native browser control in IOS to be able to call a JavaScript function on it. This is what I have so far:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);
    if (_view == null && e.NewElement is SessionWebView)
    {
        _view = (SessionWebView) e.NewElement;
        // Attach to the PropertyChanged event on the view
        _view.PropertyChanged += _view_PropertyChanged;
    }
}

private void _view_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    // Look for the sessionId propery
    if (e.PropertyName == SessionWebView.SessionIdProperty.PropertyName)
    {
        if(_view != null && !string.IsNullOrEmpty(_view.SessionId))
        {
            // if we have a view set the sessionId
            _currentSessionId = _view.SessionId;
            // Now need Browser to attach to loaded or navaigated events...
        }
    }
}

Any idea on how I could access the Browser control to call the JavaScript function?

1 Answer 1

1

Have you had a look on XLabs HybridWebView? https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms.iOS/Controls/HybridWebView/HybridWebViewRenderer.cs It's based on UIWebView control and uses UIWebView.EvaluateJavascript method. Which native browser control do you use? Could you reveal the full renderer code?

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Artur I manage to get around this using a WebViewDelegate approach. which allowed me to get the UIWebView in the LoadingFinished event. I was then able to call a script and pass it parameters by accessing the webView.EvaluateJavascript(...) function. So far so good.
How is your approach different to the one XLabs use? I mean, does your rendere has the same "signature" public partial class HybridWebViewRenderer : ViewRenderer<HybridWebView, UIWebView> ?
I use this signature public class SessionWebViewRenderer : Xamarin.Forms.Platform.iOS.WebViewRenderer in the renderer and this in the delegate: internal class WebViewDelegate : UIWebViewDelegate. I'll take a look at XLabs for sure. Thanks.
Try thier approach, it gives you more flexebility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.