当webview加载一个新页面时,没有触发decisipolicyfornavigationaction。当所有东西都被初始加载时,它会工作,但之后就不会被触发了。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSLog(@"decidePolicyForNavigationActionnavigationAction.request.URL.absoluteString:%@|", navigationAction.request.URL.absoluteString);
if (kTestLocalHTML == 1) {
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
NSURL *url = navigationAction.request.URL;
if (url != nil) {
//
// TODO: Trim the url string below as well?
// No, because this is being trimmed already in the AppManager methods related to web view URL
//
NSString *urlString = url.absoluteString;
WKNavigationType navType = navigationAction.navigationType;
//
// Display the club mobile native login page and log the user out
// if the webview is about to load the blazor login page URL
//
if ([urlString isEqualToString:[AppManager getMobileLoginUrl]]) {
self.appManager.userLoginDetails = nil;
[self displayLoginPage];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
//
// Check if link is tapped from webview and if URL string contains the
// prepended identifier within the JavaScript injection which can be found in
// "webViewDidFinishLoad" delegate method of webview
//
if (navType == WKNavigationTypeLinkActivated && [AppManager isWebViewURL_containsTargetBlank:urlString]) {
//
// Make sure to ignore the prepended identifier on the URL string
// before opening it on an external browser
//
NSInteger targetBlankIdentifierLength = kIdentifier_TargetBlank.length;
NSURL *url = [NSURL URLWithString:[urlString substringFromIndex:targetBlankIdentifierLength]];
[[UIApplication sharedApplication] openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
//
// Check if url has custom scheme other than "http" & "https" (If is either one of the tags for Telephone "tel", Mail "mailto", SMS "sms", Web Calendar "webcal", etc.)
//
// Solution Reference: https://stackoverflow.com/questions/47040471/wkwebview-not-opening-custom-url-scheme-js-opens-custom-scheme-link-in-new-wind
// - (Answers of both "hstdt" & "mattblessed")
//
// NOTE: It seems loading telephone and mail links do not work when testing on an iOS simulator.
//
NSString *urlScheme = url.scheme;
BOOL isCustomURLscheme = ( ! [urlScheme isEqualToString:@"http"] && ! [urlScheme isEqualToString:@"https"]);
if (isCustomURLscheme) {
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
//
// Get reference to the current URL being requested so that if there
// will be failure in loading the webview, we have a reference to that URL.
//
self.currentUrlRequest = navigationAction.request;
// Check if web view URL contains a calendar file to download
if ([AppManager isWebViewURL_containsCalendarDownload:urlString]) {
//
// Download and process calendar file either from Events or Dining module
// and display "New Event" page where user can view its details, edit and save to device calendar
//
[self processCalendarFileAndDisplayEventPageWithURLrequest:self.currentUrlRequest];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
// Check if web view URL contains a PDF file attachment to download
else if ([AppManager isWebViewURL_containsPDFAttachmentDownload:urlString]) {
// Display document viewer page and pass few parameters for page customization (initial URL to load, navigation bar title)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:kStoryboardName bundle:nil];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:kStoryboardID_OverlayBrowserNavigationController];
OverlayBrowserViewController *vcOverlay = (OverlayBrowserViewController *)[navigationController topViewController];
vcOverlay.initialURLtoLoad = urlString;
vcOverlay.navigationBarTitle = [self getPDFDocumentNameFromURLString:urlString];
[self presentViewController:navigationController animated:YES completion:nil];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
这个函数应该触发并检测webview是否正在加载web登录页面,如果是这种情况,它将注销用户,而不是显示应用程序的本地登录页面。然而,在主屏幕加载完成后,该函数不会被触发。
"你确定这个网页实际上是在执行重定向,而不是像Angular那样可能没有更新URL的SPA吗?你是如何设置self。wkwebmain的?——RunLoop"
谢谢你,你是正确的,网页是作为一个SPA,这就是为什么函数会在初始加载时首先调用,而不是之后。