我目前正在寻找一种方法来禁用我的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\";")
最佳答案
使用UIWebViewDelegate方法并将其设置始终返回NO。这样,它将永远不会加载任何其他网址。这是客观的c代码,您需要将其快速更改。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return NO;
}
对于快速read here
迅速:
func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
return false;
}
关于javascript - Swift-禁用WebView中的链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26056670/