2

我想知道如何从 javascript 调用一个客观的 c 方法。例如,我在 myFile.js 中定义了一个 javascript 函数,如下所示:

function() {
alert('test');
....          // How i can an objective c function here ???
}

我还有一个 UIWebView :

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
....
// what to do here ?????? 
}

感谢您的回答

4

1 回答 1

3

只需将位置设置为您识别的位置并检查 request.URL 是否匹配。

function nav(f){
 location.href = "methodCall";
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if([request.URL.relativeString isEqualToString:@"methodCall"]){
        //call method here
        return false; //so it doesn't navigate anywhere
    }
    return true; //if you want every other link to work normally.
}

注意:我没有对此进行测试,但我想它会起作用,尽管 request.URL.relativeString 返回的 URL 可能有点不同,所以只需在此处设置一个断点并查看它是什么。

于 2012-05-06T17:21:29.843 回答