MyWebBrowser swift 스위프트 웹브라우저 앱
//
// ViewController.swift
// MyWebBrowser
//
// Created by stayfoolish on 07/11/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController {
// MARK: - Properties
// MARK: IBOutlets
@IBOutlet var webView: WKWebView!
@IBOutlet var activityIndicator: UIActivityIndicatorView!
// MARK: - Methods
// MARK: Life Cycle
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)
}
// MARK: IBActions
@IBAction func goBack(_ sender: UIBarButtonItem){
self.webView.goBack()
}
@IBAction func goForward(_ sender: UIBarButtonItem){
self.webView.goForward()
}
@IBAction func refresh(_ sender: UIBarButtonItem){
self.webView.reload()
}
// MARK: Custom Methods
func showNetworkingIndicators(){
self.activityIndicator.isHidden = false
self.activityIndicator.startAnimating()
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func hideNetworkingIndicators(){
self.activityIndicator.isHidden = true
self.activityIndicator.startAnimating()
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
extension ViewController: WKNavigationDelegate {
//MARK: WKNavigationsDelegate
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: UIAlertController.Style.alert)
let okayAction: UIAlertAction
okayAction = UIAlertAction(title: "확인", style: UIAlertAction.Style.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()
}
}
//
// AppDelegate.swift
// MyWebBrowser
//
// Created by stayfoolish on 07/11/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
// 마지막 페이지 주소를 UserDefaults에서 관리하기 위한 키 값
let lastPageURLDefaultKey: String = "lastURL"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: - Prperties
var window: UIWindow?
var lastPageURL: URL?
// MARK: - Methods
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.lastPageURL = UserDefaults.standard.url(forKey: lastPageURLDefaultKey )
return true
}
func applicationWillResignActive(_ application: UIApplication) {
let userDefaults: UserDefaults
userDefaults = UserDefaults.standard
userDefaults.set(self.lastPageURL, forKey: lastPageURLDefaultKey)
userDefaults.synchronize()
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
'Swift > 기초&문법' 카테고리의 다른 글
swift tableview 간단한 스위프트 테이블뷰 , 디테일 뷰 (0) | 2018.11.10 |
---|---|
swift tableview 간단한 테이블뷰 추가, 삭제 스위프트 (0) | 2018.11.08 |
스위프트 화면 전환 present dismiss push pop segue modal swift (0) | 2018.11.06 |
swift 스위프트 간단한 화면 전환 segue modaly (0) | 2018.11.02 |
꼼꼼한 재은씨의 스위프트 swift 실전편 api alamofire (0) | 2018.11.01 |