I am trying to navigate to a fragment identifier in a webpage in WKWebView. To do this, I have the following call in my WKNavigationDelegate's didFinishNavigation:
[webView evaluateJavaScript:@"window.location.hash = 'hash';"
completionHandler:^(NSString *result, NSError *error)
{
NSLog(@"Error %@",error);
NSLog(@"Result %@",result);
}];
The webpage did not scroll to the fragment. When I connected the Safari developer console to my ios device simulator for debugging, I could see that window.location.hash was set to the hash. Manually setting the hash in the developer console would not cause the WKWebView to scroll either.
At first I thought it was the javaScriptCanOpenWindowsAutomatically property of WKPreferences that was preventing me from manipulating the javascript window object, but even after setting that to true it wasn't working.
Does anyone know what's going on?
EDIT: The issue is that the webpage is dynamically loaded, and the fragment identifier that I am trying to jump to doesn't exist unless I scroll all the way to the bottom. For now, my work around is to scroll all the way down using javascript, and then go to the hash fragment identifier.
Anyone have a smarter solution?