在WebView中禁用链接

在WebView中禁用链接

本文介绍了Swift - 在WebView中禁用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种方法来禁用我的WebView中的链接,这是我的WebView

I m currently looking for a way to disable Links in my WebView, this is my WebView

import UIKit

class ViewController_webView_news: UIViewController {
    @IBOutlet weak var WebViewNews: UIWebView!
    @IBOutlet weak var TBB_news: UITabBarItem!
    @IBOutlet weak var activity: UIActivityIndicatorView!
    override func viewDidLoad() {
        super.viewDidLoad()

        var TBB_news_img = UIImage(named: "TabbarNews.png") as  UIImage
        TBB_news.selectedImage = TBB_news_img
        var newsurl = "http://google.com"
        let newsViewUrl = NSURL(string: newsurl)
        let request = NSURLRequest(URL: newsViewUrl)
        WebViewNews.loadRequest(request)




    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        }
    @IBAction func BThome(sender: AnyObject) {

        var newsurl = "http://google.com"
        let newsViewUrl = NSURL(string: newsurl)
        let request = NSURLRequest(URL: newsViewUrl)
        WebViewNews.loadRequest(request)
    }
    func webViewDidStartLoad(_ : UIWebView){activity.startAnimating()
    }
    func webViewDidFinishLoad(_ : UIWebView){activity.stopAnimating()}

有没有办法为此使用UIWebViewNavigationType.LinkClicked?
目前正在使用

Is there a way to use "UIWebViewNavigationType.LinkClicked" for this?Currently i m using

let result = WebViewNews.stringByEvaluatingJavaScriptFromString("window.location.href = \"http://google.de\";")


推荐答案

使用UIWebViewDelegate方法并将其设置为始终返回NO。这样它永远不会加载任何其他网址。这是客观的c代码,你需要将其更改为swift。

Use UIWebViewDelegate method and set it return to NO always. this way it will never load any other url. This is objective c code you need change it to swift.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return NO;
}

对于swift

Swift:

func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
return false;
}

这篇关于Swift - 在WebView中禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:27