objective-c - 来自javascript文件的 objective-c 方法调用
<p><p>我想知道如何从 javascript 调用 objective-c方法。例如,我在 myFile.js 中定义了一个 javascript 函数,如下所示:</p>
<pre><code>function() {
alert('test');
.... // How i can an objective c function here ???
}
</code></pre>
<p>我还有一个 UIWebView :</p>
<pre><code>-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
....
// what to do here ??????
}
</code></pre>
<p>感谢您的回答</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需将位置设置为您识别的位置并检查 request.URL 是否匹配。</p>
<pre><code>function nav(f){
location.href = "methodCall";
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(){
//call method here
return false; //so it doesn't navigate anywhere
}
return true; //if you want every other link to work normally.
}
</code></pre>
<p>注意:我没有对此进行测试,但我想它会起作用,虽然 request.URL.relativeString 返回的 URL 可能有点不同,所以只需在此处设置一个断点,看看它是什么。</p></p>
<p style="font-size: 20px;">关于objective-c - 来自javascript文件的 objective-c 方法调用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10472523/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10472523/
</a>
</p>
页:
[1]