Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Can a UIWebView interact (communicate) with the app?

I went to use UIWebView to display dynamic content, instead of doing it natively using UI-elements. Is it possible to trigger native app functions from simply hitting links inside the UIWebView? Example: hitting a link which then switches current View?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Yes, it's possible. In your html, you write a JS to load a URL with a fake scheme such as

window.location = "request_for_action://anything/that/is/a/valid/url/can/go/here";

Then, in your iOS code, assign a delegate to your webView, and in your delegate, handle

webView:shouldLoadWithRequest:navigationType

with something like

if( [request.URL.scheme isEqualToString: @"request_for_action"] )
{
   // parse your custom URL to extract parameter, use URL parts or query string as you like
   return NO; // return NO, so webView won't actually try to load this fake request
}

--

Just an aside, you can do the other way, let iOS code invoke some JS codes in your html by using

NSString* returnValue = [self.webView stringByEvaluatingJavaScriptFromString: "someJSFunction()"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...