1

我正在从 UIWebView 内的服务器加载 HTML 文件文件。在 HTML 文件中,我们有要打开的外部链接,并编写了一个 Javascript 函数来处理该事件。我想在应用程序内的一个单独的新 web 视图中打开该超链接。

有没有什么方法可以让服务器端 javascript 方法通知到 Objective-C 或任何将在 Objective-C 中调用的回调函数,然后我可以在我的代码中做一些事情?我已经看到了 WEBViewJavaScriptBridge 在 javascript 和目标 C 之间进行通信的示例。但是他们正在使用本地 HTML 文件来加载和通信。Bt 我的 HTML 文件在服务器端。如果有人能在这方面提供帮助,那就太好了。

我在这里放了一个示例 HTML 文件。在点击打开按钮时,我们有两个超链接“打开”和“关闭”,调用了一个显示警报的函数。所以我想将retuen回调传递给objective-C代码,而不是警报。

这里是:-

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width = device-width, initial-scale = 1.0, maximum-scale = 1.0, minimum-scale = 1.0, user-scalable=no"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Test HTML</title>
<script type="text/javascript">

function open(url, offset){
alert("Open Webview with url:"+ url + " & Offset: " + offset);
}

function close(url, offset){
alert("close webview");
}

</script>
</head>
<body>
<a href="javascript:open('http://www.tcm.com', '55')">Open</a><br>
<a href="javascript:close()">Close</a>
</body>
</html>
4

2 回答 2

1

您可以使用以下 webView delagate 方法从目标 c 与您的 HTML 进行通信...

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

请通过这些方法的使用...我希望这可以帮助你

于 2012-12-01T05:43:09.040 回答
1

我使用 webviewjavascriptbridge 来交流 javascript 和目标 C 代码。

在此示例代码中,请注意 bridge 的全局变量。

<!doctype html>
<html><head>
<style type='text/css'>
    html { font-family:Helvetica; color:#222; }
    h1 { color:steelblue; font-size:24px; margin-top:24px; }
    button { margin:0 3px 10px; font-size:12px; }
    .logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; }
</style>
</head><body>
<h1>WebViewJavascriptBridge Demo</h1>
<script>
window.onerror = function(err) {
    alert('window.onerror: ' + err)
}
var bridge;
document.addEventListener('WebViewJavascriptBridgeReady', onBridgeReady, false)
function onBridgeReady(event){
    alert("Onbridge ready call");

    bridge = event.bridge
    var uniqueId = 1
    function log(message, data) {
        var log = document.getElementById('log')
        var el = document.createElement('div')
        el.className = 'logLine'
        el.innerHTML = uniqueId++ + '. ' + message + (data ? ': ' + JSON.stringify(data) : '')
        if (log.children.length) { log.insertBefore(el, log.children[0]) }
        else { log.appendChild(el) }
    }
    bridge.init(function(message) {
        log('JS got a message', message)
    })

    bridge.registerHandler('open', function(data, response) {
                           log('JS handler testJavascriptHandler was called', data)
                           response.respondWith({ 'Javascript Says':'open open open!' })
                           })



    bridge.registerHandler('testJavascriptHandler', function(data, response) {
        log('JS handler testJavascriptHandler was called', data)
        response.respondWith({ 'Javascript Says':'Right back atcha!' })
    })

    var button = document.getElementById('buttons').appendChild(document.createElement('button'))
    button.innerHTML = 'Send message to ObjC'
    button.ontouchstart = function(e) {
        e.preventDefault()
        bridge.send('Hello from JS button')
    }

    document.body.appendChild(document.createElement('br'))

    var callbackButton = document.getElementById('buttons').appendChild(document.createElement('button'))
    callbackButton.innerHTML = 'Fire testObjcCallback'
    callbackButton.ontouchstart = function(e) {
        e.preventDefault()
        log("Calling handler testObjcCallback")
        bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
            log('Got response from testObjcCallback', response)
        })
    }

}



function open(url, offset,e)
{
    alert(bridge);

    //alert("Open Webview with url:Yes Got it");



   //  alert(document.getElementById(offset).href);
    //  var bridge = event.bridge;
   // alert(bridge);

    window.location = url+'?offset='+offset//'myapp:myaction:url:offset'

    //requestFromObjc("buttonColor&someParam=1");
}


function close()
{
        alert("Open Webview with url:"+ url + " & Offset: " + offset);
}

function requestFromObjc(functionName, objcResult, callback)
{
    if (!objcResult)
    {
        window.location = 'myapp:myaction:param1:param2'
       // window.location = "myapp://objcRequest?function=" + functionName + "&callback=" + arguments.callee.name + "&callbackFunc=" + arguments.callee.caller.name;
    }
    else
    {
        window[callback](objcResult);
    }
}

</script>
<div id='buttons'></div> <div id='log'></div>

<body>
    <a id="55" href="javascript:open('http://www.tcm.com', '55',this)">Open</a><br>
    <a href="javascript:close()">Close</a>
</body>

</body></html>

参考:https ://github.com/marcuswestin/WebViewJavascriptBridge

于 2012-12-01T18:02:57.510 回答