gpt4 book ai didi

javascript - 从 UIWebView Javascript 监听 Objective-C 中的通知

转载 作者:行者123 更新时间:2023-11-28 22:53:40 25 4
gpt4 key购买 nike

是否可以在 UIWebView 中监听 Javascript 发布的通知?还是类似的?我考虑过在 Objective-C 中重复一个函数来监视 JS 变量,但肯定有更好的方法吗?

我想监听特定图像何时加载到 UIWebView 中(我可以使用 jQuery 中的 .load 方法),当图像加载时我想隐藏 native UIActivityIndi​​cator。我不想在 WebView 中使用 gif!

$('img.someImage').load(function(){
//fire something for obj-c to listen to!
});

最佳答案

将事件从您的 html 页面传递到 iPhone 应用程序的唯一方法是使用 url,

当你想传递一个特殊事件时,让你的 js 将你的导航更改为一个新的 url,例如,"MyCustomURl://DoSomeThing"

现在在您的 iPhone 应用程序中,将 webView 的委托(delegate)设置为 self

并添加如下函数

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *absolute = [request.URL absoluteString];

if ([absolute isEqualToString:@"MyCustomURl://DoSomeThing"]) {
//Do what you want here
return NO;//this will not load the url
}

return YES;
}

关于javascript - 从 UIWebView Javascript 监听 Objective-C 中的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com