Knowing when AJAX has loaded in UIWebView(知道何时在 UIWebView 中加载了 AJAX)
问题描述
我有一个 UIWebView 并且一个页面有一个 AJAX 组件,当用户单击按钮时,它的内容会发生变化.我如何知道内容何时加载到 UIWebView 中?
I have a UIWebView and one page has an AJAX component whose content changes when the user clicks a button. How can I know when the content has loaded inside the UIWebView?
推荐答案
你需要做两件事:
在您的 JavaScript 代码中,使用
XMLHTTPRequest信号表明 AJAX 请求已通过自定义 URL 完成:
In your JavaScript code, have the
XMLHTTPRequestsignal that the AJAX request is complete with a custom URL:
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200) {
window.location = "myspecialurl:foo"
}
}
};
在您的本机代码中,您需要实现一个 UIWebView 委托来侦听此自定义 URL:
In your native code, you'll need to implement a UIWebView delegate that listens for this custom URL:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if([[[request URL] absoluteString] isEqualToString:@"myspecialurl:foo"]) {
// Your code for when the AJAX request completes.
return NO;
}
return YES;
}
不保证此代码可以解析/编译,但应该让您知道如何去做.
No assurances this code parses/compiles but should give you the idea how to do it.
这篇关于知道何时在 UIWebView 中加载了 AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:知道何时在 UIWebView 中加载了 AJAX
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
