javascript invoke object-c 2

// 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值