UIWebView and UIWebViewDelegate example in Swift
** Write in ViewController.swift
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var myTextField: UITextField!
@IBOutlet weak var myScrollView: UIScrollView!
@IBOutlet weak var myWebView: UIWebView!
@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myWebView.delegate = self
//1. Load web site into my web view
let myURL = NSURL(string: "https://www.google.co.in/");
let myURLRequest:NSURLRequest = NSURLRequest(url: myURL as! URL);
myWebView.loadRequest(myURLRequest as URLRequest);
//2. Load html string into web view
/*
let htmlString:String = "<html><body><p>Hello!</p></body></html>"
myWebView.loadHTMLString(htmlString, baseURL: nil)
*/
/*
//3. Load local html file into web view
let myProjectBundle:Bundle = Bundle.main;
let filePath:String = myProjectBundle.path(forResource: "sample", ofType: "html")!
let myURL = NSURL(string: filePath);
let myURLRequest:NSURLRequest = NSURLRequest(url: myURL! as URL);
myWebView.loadRequest(myURLRequest as URLRequest)
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView)
{
myActivityIndicator.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
myActivityIndicator.stopAnimating()
}
@IBAction func addButtonClicked(_ sender: UIButton){
myWebView.reload()
}
}
No comments:
Post a Comment