我正在尝试将我的 iOS UIWebview 迁移到 Webkit,但在此过程中我遇到了很多问题。我在 webView:shouldStartLoadWithRequest:navigationType:
中有一段代码,之前我用来监视 url 和 url-scheem
,例如 tell:, exit:, refresh:, mailto :
并确保只有我的网址可以在 WebView 中打开。但是尝试使用 webkit 实现同样的方式却不起作用,我不确定我是否在 write 方法中执行此操作可以任何人帮助我。
/尝试使用 webkit 来实现/
- (BOOL)webView:(WKWebView *)inWeb decidePolicyForNavigationAction:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType{
NSLog(@"Event: %@", @"shouldStartLoadWithRequest inType called");
NSArray *schemeArray = @[@"share", @"map", @"rate", @"reload", @"exit"];
NSString *url = [[inRequest URL] query];
NSString *scheme = [[inRequest URL] scheme];
NSString *StrPurl = [NSString stringWithFormat:@"%@",url];
NSLog(@"[inRequest URL] == %@", StrPurl);
NSLog(@"[inRequest NSURL] == %@", url);
NSLog(@"[[inRequest URL] scheme] == %@", scheme);
if ([StrPurl containsString:@"mysite.com"]){
//coninue open
}else if ( [schemeArray containsObject:[[inRequest URL] scheme]] ){
//open share intent
NSLog(@"Event: Share URL %@", [[inRequest URL] scheme]);
}
NSLog(@"Event: Request URL %@", url);
return NO;
}
在 UIWebview 中使用上面的示例可以正常工作
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType{
if (self.validatedRequest || inType != UIWebViewNavigationTypeLinkClicked){
NSString *url = [[inRequest URL] query];
NSString *scheme = [[inRequest URL] scheme];
NSString *StrPurl = [NSString stringWithFormat:@"%@",url];
NSLog(@"[inRequest URL] == %@", StrPurl);
NSLog(@"[inRequest NSURL] == %@", url);
NSLog(@"[[inRequest URL] scheme] == %@", scheme);
}
}
我也有这个方法,但当我点击电话:或其他外部网址时它不会被调用
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSLog(@"Event: %@", @"shouldStartLoadWithRequest navigationType called");
//if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
if (navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked) {
}
NSString *url = [navigationAction.request.URL query];
NSLog(@"Event: Request URL %@", url);
decisionHandler(WKNavigationActionPolicyAllow);
}
最佳答案
尝试这个委托(delegate)方法:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
您需要调用 decisionHandler(WKNavigationActionPolicyAllow)
或 decisionHandler(WKNavigationActionPolicyCancel)
NO
您可以使用navigationAction.request
访问NSURLRequest
。
来源:https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc
关于IOS Webkit跟踪被点击的url和URL方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56139757/