WKWebview:禁用交互并单击链接

Sec*_*ave 5 objective-c ios wkwebview

有没有办法禁用Web视图上的交互?这样,用户除了加载的Webview不能走得更远?

编辑:禁用UserInteractions不是解决方案,因为该网站仍然必须可滚动。

Vin*_*yne 5

实施WKNavigationDelegate协议:

@interface ViewController () <WKNavigationDelegate>
Run Code Online (Sandbox Code Playgroud)

设置您WKWebViewnavigationDelegate属性:

self.wkWebView.navigationDelegate = self;
Run Code Online (Sandbox Code Playgroud)

然后为您要限制的URL实施策略:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    if ([navigationAction.request.URL.absoluteString containsString:@"somedomain.com/url/here"]) {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    else {
        decisionHandler(WKNavigationActionPolicyCancel);
    }
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*yer 5

WKNavigationDelegate解决方案只会阻止用户点击链接。我还有一些表单控件,我想防止与之交互,同时仍然允许滚动页面。最终我发现这可以通过禁用 web 视图滚动视图的子视图来实现:

迅速

self.webView.scrollView.subviews.forEach { $0.isUserInteractionEnabled = false }
Run Code Online (Sandbox Code Playgroud)

目标-C

for (UIView *subview in self.webView.scrollView.subviews)
{
    subview.userInteractionEnabled = NO;
}
Run Code Online (Sandbox Code Playgroud)