在webView中离开主页后,jQuery Mobile不会触发webViewDidStartLoad和webViewDidFinishLoad [英] jQuery Mobile is NOT triggering webViewDidStartLoad and webViewDidFinishLoad after leaving homepage in webView
问题描述
我有一个简单的iOS应用程序,其中包含一个包含UIWebView的视图。在我的webView中,我显示了我的jQuery Mobile网站。
I have a simple iOS app with a single view that contains a UIWebView. Within my webView, I display my jQuery Mobile website.
由于jQuery Mobile通过ajax加载页面, webViewDidStartLoad
和初始页面加载后,不会调用 webViewDidFinishLoad
方法。我在SO上发现了类似的问题,例如 这一个 ,但我不知道在哪里使用 window.location =localFunction
调用,而不是一个人还没有确认这确实有效。
Since jQuery Mobile loads pages via ajax, the webViewDidStartLoad
and webViewDidFinishLoad
methods are NOT called once the initial page loads. I've found similar questions on SO such as this one, but I'm not sure where to use the window.location = "localFunction"
call, and no one has yet to confirm this actually works.
这是我的问题(例子):
Here's my issue (example):
- 应用程序启动
-
调用shouldStartLoadWithRequest
-
webViewDidStartLoad
被调用,并显示网络活动指示符。 -
调用webViewDidFinishLoad
,隐藏网络活动指示符,webView将显示初始网页。 - 用户点击主页上的链接
-
shouldStartLoadWithRequest
被称为 - 显示第二页,但
webViewDidStartLoad
和
webViewDidFinishLoad
不再被调用
会话的持续时间。
- App Starts
shouldStartLoadWithRequest
is calledwebViewDidStartLoad
is called, and network activity indicator is shown.webViewDidFinishLoad
is called, network activity indicator is hidden, and webView displays the initial web page.- User clicks a link from the homepage
shouldStartLoadWithRequest
is called- The second page is displayed, but
webViewDidStartLoad
andwebViewDidFinishLoad
are not called again for the duration of the session.
有没有办法强制jQuery Mobile或iOS应用程序调用 webViewDidStartLoad
和 webViewDidFinishLoad
以便我可以执行一项简单的任务,例如在 webViewDidStartLoad时显示网络活动指示器调用
,并在调用 webViewDidFinishLoad
时隐藏它?
Is there a way to force jQuery Mobile or the iOS app to call webViewDidStartLoad
and webViewDidFinishLoad
so that I can do a simple task, such as show a network activity indicator when webViewDidStartLoad
is called, and hide it when webViewDidFinishLoad
is called?
推荐答案
如果您想要通知 UIWebView
的页面更改;您可以使用自定义方案启动选项,如问题中的SO帖子所述。
If you want to signal the UIWebView
of page changes; you can use the custom-scheme launching option as discussed in the SO post in your question.
定义 window.location
更改 pagebeforechange
处理程序:( JQM页面更改事件)
Define a window.location
change in the pagebeforechange
handler: (JQM Page change events)
logToIosConsole: function(msg){
console.log("logToIosConsole: log://"+msg);
var standalone = window.navigator.standalone,
userAgent = window.navigator.userAgent.toLowerCase(),
safari = /safari/.test( userAgent ),
ios = /iphone|ipod|ipad/.test( userAgent );
if( ios ) {
if ( !standalone && !safari ) {
//uiwebview
window.location = "log://"+msg;
};
} else {
//not iOS
};
在javascript中使用
Use it in javascript
$(document).on("pagebeforechange", function(eve, ui){
logToIosConsole("pagebeforechange called on "+eve.currentTarget.URL);
});
在 shouldStartLoadWithRequest $ c $中拦截此custom-url-scheme c>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *theAnchor=[[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
if([theAnchor hasPrefix:@"log:\/\/"]) {
NSString *logText=[theAnchor substringFromIndex:@"log:\/\/".length];
NSLog(@"LogMsg==>%@",logText);
return NO;
}
}
这篇关于在webView中离开主页后,jQuery Mobile不会触发webViewDidStartLoad和webViewDidFinishLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!