TResponse
TResponse

Reputation: 4125

Calling JavaScript function in Xamarin.Forms IOS WebViewRenderer

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?

Upvotes: 0

Views: 3162

Answers (1)

Art
Art

Reputation: 3167

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?

Upvotes: 1

Related Questions