import UIKit class ViewController: UIViewController, UIWebViewDelegate, UITextFieldDelegate { @IBOutlet weak var textField: UITextField! @IBOutlet weak var webView: UIWebView! @IBOutlet weak var backButton: UIBarButtonItem! @IBOutlet weak var forwardButton: UIBarButtonItem! @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView! override func viewDidLoad() { super.viewDidLoad() let topBorder = CALayer() topBorder.frame = CGRectMake(0, 0, self.webView.frame.size.width, 1) topBorder.backgroundColor = UIColor.lightGrayColor().CGColor self.webView.layer.addSublayer(topBorder) self.webView.delegate = self self.textField.delegate = self self.webView.delegate = self self.textField.delegate = self // string -> NSURL -> NSURLRequest -> webView.loadRequest let startUrl = "http://google.com" // if let url = NSURL(string: startUrl){ // let urlRequest = NSURLRequest(URL: url) // self.webView.loadRequest(urlRequest) // } self.jumpToUrl(startUrl) self.setupButtonsEnabled() self.activityIndicatorView.hidesWhenStopped = true } func jumpToUrl(urlString: String){ if let url = NSURL(string: urlString){ let urlRequest = NSURLRequest(URL: url) self.webView.loadRequest(urlRequest) } else { self.showAlert("Invalid URL") } } func showAlert(message: String){ let alertController = UIAlertController(title: "Error", message:message, preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alertController.addAction(defaultAction) self.presentViewController(alertController, animated: true, completion: nil) } func textFieldShouldReturn(textField: UITextField) -> Bool { var urlString = self.textField.text urlString = urlString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) if urlString == "" { // alert self.showAlert("please enter URL") } else { if urlString.hasPrefix("http://") && !urlString.hasPrefix("https://") { urlString = "http://" + urlString } self.jumpToUrl(urlString) self.setupButtonsEnabled() } self.textField.resignFirstResponder() return true } func setupButtonsEnabled() { self.backButton.enabled = self.webView.canGoBack self.forwardButton.enabled = self.webView.canGoForward } func webView(webView: UIWebView, didFailLoadWithError error: NSError) { self.webView.stopLoading() self.activityIndicatorView.stopAnimating() if error.code != NSURLErrorCancelled { self.showAlert("Network Error") } self.updateUrlLocation() } func webViewDidStartLoad(webView: UIWebView) { self.activityIndicatorView.startAnimating() } func updateUrlLocation(){ if let urlString = self.webView.request?.URL?.absoluteString { self.textField.text = urlString } } func webViewDidFinishLoad(webView: UIWebView) { self.activityIndicatorView.stopAnimating() self.setupButtonsEnabled() self.updateUrlLocation() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func goBack(sender: AnyObject) { self.webView.goBack() } @IBAction func goForward(sender: AnyObject) { self.webView.goForward() } @IBAction func reload(sender: AnyObject) { self.webView.reload() } }