功能介绍:
使用UIWebView和UIToolBar创建一个简单的浏览器,熟悉这两个控件的使用
- 输入网址,点击“搜索”或者点击键盘return键进行搜索
- 加载过程有加载进度条显示
- 有后退、前进和重新加载功能
代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIWebViewDelegate {
var toolBar : UIToolbar!
var progressView : UIProgressView!
var textField : UITextField!
var searchButton : UIButton!
var webView : UIWebView!
var loadIndicator : UIActivityIndicatorView!
var timer : Timer!
override func viewDidLoad() {
super.viewDidLoad()
//创建工具条
setupToolBar()
//创建进度条
progressView = UIProgressView(progressViewStyle: .bar)
progressView.frame = CGRect(x: 0, y: self.toolBar.frame.maxY, width: self.view.bounds.width, height: 10)
progressView.progressViewStyle = .bar
//设置初始值
progressView.progress = 0
view.addSubview(progressView)
//创建定时器
timer = Timer(timeInterval: 0.2, target: self, selector: #selector(loadProgress), userInfo: nil, repeats: true)
timer.invalidate()
//创建go按钮
searchButton = UIButton(frame: CGRect(x: view.bounds.width - 20 - 40, y: progressView.frame.maxY + 5, width: 40, height: 40))
searchButton.setTitle("搜索", for: .normal)
searchButton.setTitleColor(UIColor.blue, for: .normal)
searchButton.addTarget(self, action: #selector(goButtonAction), for: .touchUpInside)
view.addSubview(searchButton)
//创建输入框
textField = UITextField(frame: CGRect(x: 10, y: progressView.frame.maxY + 5, width: view.bounds.width - 10 * 2 - 40 - 10, height: 40))
textField.layer.borderWidth = 1.0
textField.layer.borderColor = UIColor.black.cgColor
textField.placeholder = "输入网址"
textField.delegate = self
view.addSubview(textField)
//创建webView
webView = UIWebView(frame: CGRect(x: 0, y: textField.frame.maxY + 10, width: self.view.bounds.width, height: self.view.bounds.height - textField.frame.maxY - 10))
webView.delegate = self
view.addSubview(webView)
//创建加载圈
loadIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
loadIndicator.center = view.center
view.addSubview(loadIndicator)
}
func setupToolBar() {
//创建一个工具条
self.toolBar = UIToolbar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44))
self.view.addSubview(self.toolBar)
//后退按钮
let backButton = UIBarButtonItem(image:UIImage.init(named: "back") , style: .plain, target: self, action: #selector(backAction))
//第一个占位
let space1 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
//重新加载按钮
let reloadButton = UIBarButtonItem(image: UIImage.init(named: "reload"), style: .plain, target: self, action: #selector(reloadAction))
//第二个占位
let space2 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
//停止按钮
let stopButton = UIBarButtonItem(image: UIImage.init(named: "stop"), style: .plain, target: self, action: #selector(stopAction))
//第三个占位
let space3 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
//前进按钮
let forwardButton = UIBarButtonItem(image: UIImage.init(named: "forward"), style: .plain, target: self, action: #selector(forwardAction))
self.toolBar.items = [backButton, space1, reloadButton, space2, stopButton, space3, forwardButton]
}
// MARK: - events
//加载URL
func loadUrl(urlString:String) {
if urlString.isEmpty {
return
}
let url = URL(string: urlString)
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
//后退
@objc func backAction() {
webView.goBack()
}
//重新加载
@objc func reloadAction() {
webView.reload()
}
//停止
@objc func stopAction() {
webView.stopLoading()
}
//前进
@objc func forwardAction() {
webView.goForward()
}
//加载进度计算
@objc func loadProgress() {
//如果进度条已满
if progressView.progress >= 1.0 {
//停用定时器
timer.invalidate()
} else {
progressView.setProgress(progressView.progress + 0.02, animated: true)
}
}
@objc func goButtonAction() {
//收起键盘
textField.resignFirstResponder()
if (textField.text?.isEmpty)! {
let alert = UIAlertController(title: "警告", message: "请输入网址", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "好的", style: .default, handler: nil)
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
return
}
loadUrl(urlString: textField.text!)
}
//MARK: - UITextField Delegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
loadUrl(urlString: textField.text!)
return true
}
//MARK: - UIWebView Delegate
func webViewDidStartLoad(_ webView: UIWebView) {
progressView.setProgress(0, animated: false)
timer.fire()
loadIndicator.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
loadIndicator.stopAnimating()
progressView.setProgress(1, animated: false)
timer.invalidate()
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
let alertController = UIAlertController(title: "出错!",
message: error.localizedDescription,
preferredStyle: .alert)
let okAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
实现效果:
注意:
在iOS 9的时候,默认非HTTS的网络是被禁止的,我们可以在info.plist文件中添加NSAppTransportSecurity字典,将NSAllowsArbitraryLoads设置为YES来禁用ATS;