iOS-swift WebView浏览器

UIWebVIew(不支持ios10及其以上的版本)

import UIKit

class FourViewController: UIViewController,UIWebViewDelegate {
    //内部浏览器(webview)
    @IBOutlet var webview1: UIWebView!
    //网址输入框
    @IBOutlet var textFile1: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置webview控件的 Delegate 的监听
        webview1.delegate=self
        //打开一个外部的网页
        webview1.loadRequest(NSURLRequest(url: NSURL(string: "http://hao123.com")! as URL) as URLRequest)
        // Do any additional setup after loading the view.
    }
    //返回
    @IBAction func backButClick(_ sender: UIButton) {
        webview1.goBack()
    }
    //前进
    @IBAction func toButClick(_ sender: UIButton) {
        webview1.goForward()
    }
    //刷新
    @IBAction func reloadButClick(_ sender: UIButton) {
        webview1.reload()
    }
    //加载
    @IBAction func goButClick(_ sender: UIButton) {
        let text_url = textFile1.text
        webview1.loadRequest(NSURLRequest(url: NSURL(string: text_url!)! as URL) as URLRequest)
    }

    //网页开始加载监听
    func webViewDidStartLoad(_ webView: UIWebView) {
        print("开始加载")
    }
    //网页结束加载监听
    func webViewDidFinishLoad(_ webView: UIWebView) {
        print("结束加载")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

WKWebView(支持ios8以上的系统,比UIWebView性能更加的优化,用法跟UIWebView用法差不多)

import UIKit
import WebKit //引入该包

class WebViewController: UIViewController {
    //输入网址的文本输入框
    @IBOutlet var textfile1: UITextField!
    //浏览器控件
    @IBOutlet var webview2: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    //刷新
    @IBAction func refreshClick(_ sender: AnyObject) {
        webview2.reload()
    }

    //前进
    @IBAction func toClick(_ sender: AnyObject) {
        webview2.goForward()
    }
    //后退
    @IBAction func backClick(_ sender:  AnyObject) {
        webview2.goBack()
    }
    //搜索
    @IBAction func searchClick(_ sender: AnyObject) {
        let urls = textfile1.text
       // print(urls)
        webview2.load(NSURLRequest(url: NSURL(string: urls!)! as URL)as URLRequest)
    }
    //文本输入框输入完成后触发的事件
    @IBAction func textEndOnExit(_ sender: AnyObject) {
        //隐藏键盘(让文本框失去焦点)
        textfile1.resignFirstResponder()
    }
    //点击空白处让键盘消失(这个事件要将 View改为UIControl)
    @IBAction func touchUpInside(_ sender: UIControl) {
        //隐藏键盘(让文本框失去焦点)
        textfile1.resignFirstResponder()
    }

    //触摸事件,当一个或多个手指离开屏幕时触发(当手指离开输入框后键盘消失)
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        //隐藏键盘(让文本框失去焦点)
        textfile1.resignFirstResponder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

将 View改为UIControl
这里写图片描述
点击View在右边将 class改为UIControl
iOS-swift WebView浏览器_第1张图片

你可能感兴趣的:(ios)