jQuery Mobile is NOT triggering webViewDidStartLoad and webViewDidFinishLoad after leaving homepage in webView

☆樱花仙子☆ 提交于 2019-12-04 18:22:25

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.

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 
    }; 

Use it in javascript

$(document).on("pagebeforechange", function(eve, ui){
    logToIosConsole("pagebeforechange called on "+eve.currentTarget.URL);
});

Intercept this custom-url-scheme in your shouldStartLoadWithRequest

- (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; 
    }
}

webViewDidStartLoad and webViewDidFinishLoad are only invoked when window.location changes. UIWebView cannot intercept Ajax calls.

You can either turn-off JQM's Ajax navigation - globally by setting ajaxEnabled="false" or individual pages/links using data-ajax="false".

OR

If the problem is simply showing an activity indicator; you could use JQM's in-built activity indicator by invoking $.mobile.showPageLoadingMsg.

It is a problem with the way the jQuery Mobile site renders pages. They DO get called if you disable ajax navigation for the site. With Ajax ENABLED, the webViewDidStartLoad never gets called when clicking a link because since the page is requested through Ajax, the window location doesn't change, and therefore iOS doesn't register a page change or page load.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!