Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

xamarin.ios - How I can call a javascript function with MonoTouch and vice versa?

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.

I believe that with objective c this problem is possible, but i need making it with monotouch.

Help me please.

any help is thank for advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...