webkit 을 이용해서 웹브라우저 앱을 만들었습니다 뒤로 이동, 새로고침 앞으로 이동, 마지막 화면 저장했다가 앱 재실행 했을 경우 마지막 화면부터 시작하는 앱입니다.
//
// ViewController.swift
// MyWebBrowser
//
// Created by stayfoolish on 2018. 9. 26..
// Copyright © 2018년 stayfoolish. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet var webView: WKWebView!
@IBOutlet var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.webView.navigationDelegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let firstPageURL: URL?
if let lastURL: URL = UserDefaults.standard.url(forKey: lastPageURLDefaultKey){
firstPageURL = lastURL
}else {
firstPageURL = URL(string: "https://www.google.com")
}
guard let pageURL: URL = firstPageURL else {
return
}
let urlRequest: URLRequest = URLRequest(url: pageURL)
self.webView.load(urlRequest)
}
@IBAction func goBack(_ sender: UIBarButtonItem) {
self.webView.goBack()
}
@IBAction func goForward(_ sender: UIBarButtonItem) {
self.webView.goForward()
}
@IBAction func refresh(_ sender: UIBarButtonItem) {
self.webView.reload()
}
func showNetworkingIndicators(){
self.activityIndicator.isHidden = false
self.activityIndicator.startAnimating()
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func hideNetworkingIndicators(){
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
print("did finish navigation")
if let appDelegate: AppDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.lastPageURL = webView.url
}
webView.evaluateJavaScript("document.title") { (value: Any? , error: Error?) in
if let error: Error = error {
print(error.localizedDescription)
return
}
guard let title: String = value as? String else {
return
}
self.navigationItem.title = title
}
self.hideNetworkingIndicators()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error){
print("did fail navigation")
print("\(error.localizedDescription)")
self.hideNetworkingIndicators()
let message: String = "오류발생!\n" + error.localizedDescription
let alert: UIAlertController
alert = UIAlertController(title: "알림", message: message, preferredStyle: UIAlertControllerStyle.alert)
let okayAction: UIAlertAction
okayAction = UIAlertAction(title: "확인", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(okayAction)
self.present(alert, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!){
print("did start navigation")
self.showNetworkingIndicators()
}
}
'Swift > 기초&문법' 카테고리의 다른 글
swift assert guard 스위프트 (0) | 2018.09.28 |
---|---|
swift autolayout 스위프트 오토레이아웃 (0) | 2018.09.27 |
swift library alamofire papago 번역 파파고 스위프트 라이브러리 (0) | 2018.09.25 |
스위프트 타입 캐스팅 swift type casting (0) | 2018.09.24 |
스위프트 옵셔널 체이닝과 nil 병합 swift optional (0) | 2018.09.23 |