IOS Webkit跟踪被点击的url和URL方案

标签 ios objective-c uiwebview wkwebview

我正在尝试将我的 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/

相关文章:

javascript - jQuery AJAX 适用于移动 safari,但不适用于 UIWebView?

ios - 播放音频时设置自定义元数据(控制中心、锁屏...)

ios - 当我从主屏幕重新进入应用程序时是哪个阶段

objective-c - 在 NSString 中填充一个数字

ios - 是否可以在没有 React-Native 的情况下为 iPad 创建 React WebApp?

ios - 来自类型 id 的对象的属性列表

objective-c - 调用继承的私有(private)方法的完美解决方案

ios - 无法从 UITextField 中退出 FirstResponder

ios - Xcode:如何为标签文本添加边距/填充?

ios - 文件读取无法在设备中访问,但在模拟器中