Javascript 触发谷歌搜索结果的webViewDidFinishLoad
当页面在myJavascript 触发谷歌搜索结果的webViewDidFinishLoad,javascript,ios,objective-c,uiwebview,webviewdidfinishload,Javascript,Ios,Objective C,Uiwebview,Webviewdidfinishload,当页面在mywebViewDidFinishLoad中加载完毕时,我正在尝试运行代码。但是,如果我在我的UIWebView中访问google.com,然后键入一个搜索,第一个查询可以工作,但是所有其他搜索都不会触发webviewdiffinishload。根据其他文章,这是因为谷歌使用ajax请求。为了解决这个问题,在加载完成后,将javascript注入页面以触发iOS方法。然而,它似乎不适合谷歌,但适用于其他ajax网站,如雅虎 我是否正确地使用javascript?有人对如何让javasc
webViewDidFinishLoad
中加载完毕时,我正在尝试运行代码。但是,如果我在我的UIWebView
中访问google.com,然后键入一个搜索,第一个查询可以工作,但是所有其他搜索都不会触发webviewdiffinishload
。根据其他文章,这是因为谷歌使用ajax请求。为了解决这个问题,在加载完成后,将javascript注入页面以触发iOS方法。然而,它似乎不适合谷歌,但适用于其他ajax网站,如雅虎
我是否正确地使用javascript?有人对如何让javascript在ajax加载完成后调用Obj-C方法有什么建议吗
ajax\u handler.js(与应用程序捆绑)
WebView
#define CocoaJSHandler @"x3AjaxHandler"
static NSString *ajaxInjectionHandler;
+(void)initialize
{
ajaxInjectionHandler = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ajax_handler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"js: %@", ajaxInjectionHandler);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"SHOULD LOAD");
if ([[[request URL] scheme] caseInsensitiveCompare:CocoaJSHandler] == NSOrderedSame) {
NSString *requestedURLString = [[[request URL] absoluteString] substringFromIndex:[CocoaJSHandler length] + 3];
NSLog(@"ajax request: %@", requestedURLString);
[self webViewDidFinishLoad:webView];
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"INJECTING");
[webView stringByEvaluatingJavaScriptFromString:ajaxInjectionHandler];
}
您可以尝试创建并删除一个iFrame,其url架构和路径为
src
属性。设置窗口。位置对我不起作用。
到目前为止,这对我很有效:
function sendRequestToiOS(url) {
// Create and append an iFrame to the document and set it with the url schema
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "x3AjaxHandler://" + url);
// For some reason we need to set a non-empty size for the iOS6 simulator...
iframe.setAttribute("height", "1px");
iframe.setAttribute("width", "1px");
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
您在webViewDidFinishLoad
中尝试执行什么操作?根据这一点,尝试将搜索视为“单独页面加载”可能没有意义。您能对此进行更多的扩展吗?我不确定这是否能解决问题,因为没有触发控制台.log
。这几乎就好像send
在一段时间内没有被触发。在测试之后,它仍然具有与没有调用回调相同的效果
function sendRequestToiOS(url) {
// Create and append an iFrame to the document and set it with the url schema
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "x3AjaxHandler://" + url);
// For some reason we need to set a non-empty size for the iOS6 simulator...
iframe.setAttribute("height", "1px");
iframe.setAttribute("width", "1px");
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}