Swift - 在WebView中禁用链接

时间:2014-09-26 09:47:48

标签: javascript ios webview swift

我目前正在寻找一种在我的WebView中禁用链接的方法,这是我的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"为了这? 目前我正在使用

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

1 个答案:

答案 0 :(得分:3)

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

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

对于swift read here

夫特:

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