How I can call a javascript function with MonoTouch and vice versa?

后端 未结 3 1703
孤独总比滥情好
孤独总比滥情好 2020-11-30 06:11

i\'m new using monotouch.

My problem is i want call a javascript function from a monotouch function and javascript function could call a monotouch function too.

相关标签:
3条回答
  • 2020-11-30 06:37

    I just released MonoTouch JsBridge to make this easier. It allows you to add event listeners and fire events between the native side and the UIWebView. It was inspired by Titanium.

    https://github.com/crdeutsch/MonoTouch-JsBridge

    0 讨论(0)
  • 2020-11-30 06:39

    To invoke Javascript code running in the UIWebView from your application, use the EvaluateJavascript method, like this:

    myView.EvaluateJavaScript ("a = 1;");
    

    To call back into your C# code, the only option is to hook up to the ShouldStartLoad property like this:

    myView.ShouldStartLoad = myHandler;
    
    [...]
    
    bool myHandler (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
    {
        // Determine what to do here based on the @request and @navType
    } 
    

    You can of course, also use anonymous methods if you want to access local variables easily:

    myView.ShouldStartLoad = (webView, request, navType) => {
         // Determine here what to do
    }
    

    In the Javascript side, if you want to call back to Mono, you then set the location.href property to a "special" url, like this:

    // Javascript code:
    location.href = "myapp://action?par1=abc&par2=def"
    

    The information will be available on the request object: request.Url.AbsoluteString

    0 讨论(0)
  • 2020-11-30 06:49

    I haven't tried it in monotouch, but it should be the same as in ObjC:

    UIWebView.EvaluateJavascript
    
    0 讨论(0)
提交回复
热议问题