I have a webview
in which I pass a url, it usually works fine, but there are other times that I get this error: libc++abi.dylib: terminating with uncaught exception of type NSException
The problem is that I do not know where the error is.
I added a try-catch
, but it does not solve anything
let url:URL? = URL(string: self.url!)
var urlRequest: URLRequest?
if let actualUrl = url {
do {
urlRequest = try URLRequest(url: actualUrl)
} catch {
fatalError("Unhandled error: \(error)")
}
self.webView.loadRequest(urlRequest!)
}
The error appears once you have loaded the web
How can I know where the error is?
The complete code:
import UIKit
class WebViewViewController: UIViewController, UIWebViewDelegate {
var url: String?
let progressAlert: UIAlertController = UIAlertController(title: "Cargando Contenido", message: "Espere, por favor", preferredStyle: UIAlertControllerStyle.alert)
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var btnVolver: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
print("webviewcontroller")
let attributes = [NSFontAttributeName: UIFont.fontAwesomeOfSize(20)] as Dictionary!
btnVolver.setTitleTextAttributes(attributes, for: .normal)
btnVolver.title = String.fontAwesomeIconWithName(.ArrowLeft)
btnVolver.tintColor = UIColor.white
//btnVolver.action = #selector(volver)
btnVolver.target = self
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationItem.leftBarButtonItem = btnVolver
webView.delegate = self
self.navigationController!.navigationBar.isHidden = false
}
@IBAction func volver(_ sender: UIBarButtonItem) {
self.performSegue(withIdentifier: "volver", sender: nil)
}
func webViewDidStartLoad(_ webView: UIWebView) {
loadingIndicator.center = CGPoint(x: progressAlert.view.bounds.size.width/2, y: progressAlert.view.bounds.size.height - 10)
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating()
progressAlert.view.addSubview(loadingIndicator)
self.present(progressAlert, animated: true, completion: nil)
//progressAlert.present(self, animated: true, completion: nil)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
progressAlert.dismiss(animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
if let actualArticle = self.articleToDisplay {
let url = NSURL(string: actualArticle.enlace)
if let actualUrl = url {
let urlRequest = NSURLRequest(url: actualUrl as URL)
self.webView.loadRequest(urlRequest as URLRequest)
}
}
else if let actualArticle = self.tweetToDisplay {
let url = NSURL(string: actualArticle.enlace)
if let actualUrl = url {
let urlRequest = NSURLRequest(url: actualUrl as URL)
self.webView.loadRequest(urlRequest as URLRequest)
}
}
else if let enlace = self.url {
let url = NSURL(string: enlace)
if let actualUrl = url {
let urlRequest = NSURLRequest(url: actualUrl as URL)
self.webView.loadRequest(urlRequest as URLRequest)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}