UIWebViewNavigationTypeLinkClicked is not triggered in iOS 11 beta softwares

Area: WebKit


Summary: Hi Team, Our customer app have web-view functionality and we are loading data into the web-view, also we have custom links created with in the page data. Clicking on links will auto scroll into the specific section in the page, with the help of apple API (-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType) it works well till iOS 10 versions. While we conduct iOS 11 compatibility testing we have found this API is not responding to the request, and the auto scroll don't get triggered. Hence our functionality won't work as expected. Please could you consider this issue, and provide a solutions as soon as possible.

Regards


Steps to Reproduce: 1) Create an app with a custom web-view and load data which consist of links (NSString *htmlHeader = [[[NSString alloc] initWithFormat:@"<a href=\"#%@\">%@</a><br/>", headerName, headerName] autorelease];) 2) Load the content and click the links created as above. 3) Implement (-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType) method to scroll into the link section.


Expected Results: web-view should auto scroll to the link section.


Observed Results: Nothing happened. Web-view still stand the same.


Version: iOS 11 beta (2,3 and 4)

I have a project that programatically load html file like this

webView.loadHTMLString(string, baseURL: nil)


Clicking an internal link does not work on iOS11.

Also JavaScript

window.location.href = '#ANCHOR'

does not work on iOS11.


Tested on both UIWebView and WKWebView.

,Hi Masatoshi


Thank you for your update, did you hear any further from Apple regarding this issue.


@All - Please feel free to share your thoughts / solutions / workaround for this issue.

Regards

I encountered the same problem on iOS 11. I used the debugger to have a look at the NSURLRequest that was being delivered to the UIWebView delegate method webView:shouldStarLoadWithRequest:navigationType. The method *does* get called after the link is clicked, but the file URL in the request is wrong. It looks like: /xxxxx/xxxxxx/AppName.app/#FRAGMENT. Note that the path to the document is missing—it's just the bundle path, a forward slash, then the fragment.


Here's the hack I wrote to work around the bug. It checks for a URL with a single hash mark in it and executes a bit of JavaScript to scroll to the element with an id equal to the string after the hash. Depending on what type of documents you're working with, you may want to be more careful about detecting a link with a fragment.


- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   //...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        if ([scheme isEqualToString: @"file"]) {
            NSArray<NSString*>* components = [request.URL.absoluteString componentsSeparatedByString:@"#"];
            if (components.count == 2) {
                NSString* anchor = components.lastObject;
                NSString* code = [NSString stringWithFormat:@"el = document.getElementById(\"%@\"); if (el) el.scrollIntoView();", anchor];
                (void) [webView stringByEvaluatingJavaScriptFromString:code];
                return NO;
            }
       }
       return YES;
    }
   //...
}
      
      


Hope this helps!


—Chris

Hi Chris,


I implemented this method into my webView:shouldStartLoadWithRequest method and it does not work on iOS 11. getElementById doesn't find anything, setting location.href does nothing, but things like scrollY and scrollTo(x,y) still function properly. Is there something I'm missing, or is there an alternative solution that will work on iOS 11?

Thanks Chris!

In my case, I need to change document.getElementsByName(\"%@\")[0]

Changed code:

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        if ([[[request URL] scheme] isEqualToString: @"file"]) {
            NSArray<NSString*>* components = [request.URL.absoluteString componentsSeparatedByString:@"#"];
            if (components.count == 2) {
                NSString* anchor = components.lastObject;
                NSString* code = [NSString stringWithFormat:@"el = document.getElementsByName(\"%@\")[0]; alert(el);if (el) el.scrollIntoView();", anchor];
                (void) [webView stringByEvaluatingJavaScriptFromString:code];
                return NO;
            }
        }
        return YES;
    }
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        if ([[[request URL] scheme] isEqualToString: @"file"]) {
            NSArray<NSString*>* components = [request.URL.absoluteString componentsSeparatedByString:@"#"];
            if (components.count == 2) {
                NSString* anchor = components.lastObject;
                NSString* code = [NSString stringWithFormat:@"el = document.getElementsByName(\"%@\")[0]; if (el) el.scrollIntoView();", anchor];
                (void) [webView stringByEvaluatingJavaScriptFromString:code];
                return NO;
            }
        }
        return YES;
    }

Hi,

While use your code, css effect not worked 😟. How can i get that too ?.

UIWebViewNavigationTypeLinkClicked is not triggered in iOS 11 beta softwares
 
 
Q