// This delegate method is used to grab any links used to "talk back" to Objective-C code from the html/JavaScript
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
// In the event we see our URL scheme pass by, we deal with it, otherwise we let other URL schemes pass by
// You may choose to handle other UIWebViewNavigationType values as your app requires
if (inType == UIWebViewNavigationTypeOther || inType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [inRequest URL];
NSString *scheme = [URL scheme];
if ([scheme isEqualToString:@"abc"]) {
// This is the point where we are communicated with - the resourceSpecifier contains anything after the
// abc: in the URL. We can parse it as needed. In this case we simple NSLog it.
NSLog(@"%@", [URL resourceSpecifier]);
// Let the webView know we handled it
return NO;
}
}
// Let the webView handle everything else
return YES;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css">body{font-family:"Avenir", serif;} a:link{color:gray;}</style>
<title>hello</title>
</head>
<body>
<h3>JavaScript-UIWebView</h3>
<p>A simple example showing how to communicate via UIWebView between Obj-C and JavaScript and back</p>
<script type="text/javascript" charset="utf-8">
function doButton() {
a = '%inject%'; // This is replaced by text generated from our Obj-C code
if (confirm(a)) {
// When this is set, our UIWebView delegate is called in the Obj-C code
location.href = 'abc://test?param=cool&another=hot';
}
}
</script>
<input type="button" onclick="doButton();" value="Click Me First" />
<br /><br />
<!-- Clicking this link also calls the UIWebView delegate -->
<a href="abc://well hello there!">Here is another link to try clicking - check the NSLog output</a>
<br /><br />
<a href="http://zobcode.com">A normal link just to show they still work as expected</a>
</body>
</html>