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()

    }


}






+ Recent posts