# 學習日誌 - Day1 (2019/7/18)
### 預計完成
- [x] ==1== UIWebView
- [x] ==2== 建立Line機器人 我的Line Bot ID: @120fwetj
- [ ] ==3== 建立Slack機器人 (建立完成但就是無法online)
- [x] ==4== 每日跑三公里 
## UIWebView
```gherkin=
//
// ViewController.swift
// ExUIWebView
//
// Created by Milo on 2019/7/17.
// Copyright © 2019 MUB. All rights reserved.
//
import WebKit //如果要使用WKWebView需要import WebKit, 而非預設的UIKit
class ViewController: UIViewController, UITextFieldDelegate { // 需加入UITextFieldDelegate如此在輸入時才會觸發事件
var myTextField = UITextField()
var myWebView = WKWebView() //iOS12之後WKWebView取代UIWebView,iOS12之後也無法在使用UIWebView
var myActivityIndicator = UIActivityIndicatorView()
let fullScreenSize = UIScreen.main.bounds.size
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black //將背景設為黑色
//建立五個Button分別為back, forward, reload, stop, go
var myButton = UIButton()
myButton.frame = CGRect(x: 0, y: 20, width: 50, height: 50)
myButton.setTitle("<", for: .normal)
myButton.titleLabel?.font = .systemFont(ofSize: 50)
myButton.setTitleColor(UIColor.white , for: .normal)
myButton.addTarget(self, action: #selector(ViewController.back), for: .touchUpInside)
myButton.center = CGPoint(x: fullScreenSize.width * 0.1, y: fullScreenSize.height * 0.05)
view.addSubview(myButton)
myButton = UIButton()
myButton.frame = CGRect(x: 0, y: 20, width: 50, height: 50)
myButton.setTitle(">", for: .normal)
myButton.titleLabel?.font = .systemFont(ofSize: 50)
myButton.setTitleColor(UIColor.white , for: .normal)
myButton.addTarget(self, action: #selector(ViewController.forward), for: .touchUpInside)
myButton.center = CGPoint(x: fullScreenSize.width * 0.3, y: fullScreenSize.height * 0.05)
view.addSubview(myButton)
myButton = UIButton()
myButton.frame = CGRect(x: 0, y: 20, width: 50, height: 50)
myButton.setTitle("◎", for: .normal)
myButton.titleLabel?.font = .systemFont(ofSize: 50)
myButton.setTitleColor(UIColor.white , for: .normal)
myButton.addTarget(self, action: #selector(ViewController.reload), for: .touchUpInside)
myButton.center = CGPoint(x: fullScreenSize.width * 0.5, y: fullScreenSize.height * 0.05)
view.addSubview(myButton)
myButton = UIButton()
myButton.frame = CGRect(x: 0, y: 20, width: 50, height: 50)
myButton.setTitle("■", for: .normal)
myButton.titleLabel?.font = .systemFont(ofSize: 50)
myButton.setTitleColor(UIColor.white , for: .normal)
myButton.addTarget(self, action: #selector(ViewController.stop), for: .touchUpInside)
myButton.center = CGPoint(x: fullScreenSize.width * 0.7, y: fullScreenSize.height * 0.05)
view.addSubview(myButton)
myButton = UIButton()
myButton.frame = CGRect(x: 0, y: 20, width: 100, height: 100)
myButton.setTitle("Go", for: .normal)
myButton.titleLabel?.font = .systemFont(ofSize: 50)
myButton.setTitleColor(UIColor.white , for: .normal)
myButton.addTarget(self, action: #selector(ViewController.go), for: .touchUpInside)
myButton.center = CGPoint(x: fullScreenSize.width * 0.9, y: fullScreenSize.height * 0.05)
view.addSubview(myButton)
//建立TextField
myTextField.frame = CGRect(x: 0, y: 0, width: fullScreenSize.width, height: 40)
myTextField.center = CGPoint(x: fullScreenSize.width * 0.5, y: fullScreenSize.height * 0.015)
myTextField.text = "https://www.google.com" //預設文字內容在這裡也是同時首頁的設定
myTextField.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
myTextField.clearButtonMode = .whileEditing //clearButtonMode是myTextField裡的清除按鈕,若沒有特別顯示則不會出現,whileEditing則是在編輯時才會出現(x)的符號
myTextField.returnKeyType = .default //.returnKeyType是改變return鍵的預設樣式,如改為join就會將原本default顯示為灰色底的return轉換為藍色底的join, 但像google,yahoo都會變成變成Search, 不像developer.apple.com/documentation/uikit/uireturnkeytype所寫會變成yahoo, google樣式
myTextField.delegate = self //delegate委任 設為self就是ViewController本身
//建立WKWebView
myWebView.frame = CGRect(x: 0, y: 0, width: fullScreenSize.width * 1, height: fullScreenSize.height * 0.9)
myWebView.center = CGPoint(x: fullScreenSize.width * 0.5, y: fullScreenSize.height * 0.549)
// myWebView.navigationDelegate = self
view.addSubview(myWebView)
//建立環狀進度條
myActivityIndicator = UIActivityIndicatorView(style: .gray) //將讀取條設為淺灰色
myActivityIndicator.color = UIColor.red
myActivityIndicator.center = CGPoint(x: fullScreenSize.width * 0.5, y: fullScreenSize.height * 0.5)
self.view.addSubview(myActivityIndicator)
myWebView.addSubview(myTextField) //在myWebView下新增一個subView, 也就是說myTextField是myWebView的superView,myWebView的superview是myTextField
self.go()
}
@objc func back(){
myWebView.goBack() //上一頁功能
}
@objc func forward(){
myWebView.goForward() //下一頁功能
}
@objc func reload(){
myWebView.reload() //重新讀取功能
}
@objc func stop(){
myWebView.stopLoading() //停止功能
// myActivityIndicator.stopAnimating()
}
@objc func go(){
self.view.endEditing(true)
let url = URL(string: myTextField.text!) //因為.text本身是String? 所以需要解包才能將值指派給url
if url != nil{ //因為url是optional所以如果把網址列弄成空白送出會造成程式Crash, 所以透過if檢查是否為nil值
let urlRequest = URLRequest(url: url!)
myWebView.load(urlRequest) //urlRequest設定取得的值是www.google.com, 因此WebView.load載入時會先跑到google首頁
}
else{
myTextField.text = "請輸入網址"
}
}
func textFieldShouldReturn(_ textField:UITextField) -> Bool{
self.go()
self.view.endEditing(true) //按下return鍵之後鍵盤就會自動收回
return true
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!){
myActivityIndicator.startAnimating()
}
func webView(_ vewView:WKWebView, didFinish navigation: WKNavigation!){
myActivityIndicator.stopAnimating()
if let currentURL = myWebView.url{
myTextField.text = currentURL.absoluteString
}
}
}
```