Advertisement
Guest User

as

a guest
Jul 18th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.71 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  project4
  4. //
  5. //  Created by VSAA on 7/16/19.
  6. //  Copyright © 2019 VSAA. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import WebKit
  11.  
  12.  
  13. class ViewController: UIViewController, WKNavigationDelegate {
  14.    
  15.     var webView: WKWebView!
  16.     var progressView: UIProgressView!
  17.     var webSites = ["apple.com", "hackingwithswift.com"]
  18.    
  19.     override func loadView() {
  20.         webView = WKWebView()
  21.         webView.navigationDelegate = self
  22.         view = webView
  23.     }
  24.     override func viewDidLoad() {
  25.         super.viewDidLoad()
  26.        
  27.        
  28.        
  29.        
  30.         let url = URL(string: "https://" + webSites[0])!
  31.         webView.load(URLRequest(url: url))
  32.        
  33.         webView.allowsBackForwardNavigationGestures = true
  34.        
  35.         navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped))
  36.        
  37.         progressView = UIProgressView(progressViewStyle: .default)
  38.         progressView.sizeToFit()
  39.        
  40.         let progressButton = UIBarButtonItem(customView: progressView)
  41.        
  42.         let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  43.         let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
  44.        
  45.         toolbarItems = [progressButton, spacer, refresh]
  46.         navigationController?.isToolbarHidden = false
  47.        
  48.          webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
  49.     }
  50.    
  51.     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  52.         if keyPath == "estimatedProgress" {
  53.             progressView.progress = Float(webView.estimatedProgress)
  54.         }
  55.     }
  56.    
  57.  
  58.    
  59.     @objc func openTapped() {
  60.         let ac = UIAlertController(title: "Open page...", message: nil, preferredStyle: .actionSheet)
  61.         for website in webSites {
  62.             ac.addAction(UIAlertAction(title: website, style: .default, handler: openPage))
  63.         }
  64.        
  65.         ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
  66.         ac.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
  67.         present(ac, animated: true)
  68.     }
  69.    
  70.     func openPage(action: UIAlertAction) {
  71.         let url = URL(string: "https://" + action.title!)!
  72.         webView.load(URLRequest(url: url))
  73.     }
  74.     func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  75.         title = webView.title
  76.     }
  77.     func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  78.         let url  = navigationAction.request.url
  79.        
  80.         if let host = url?.host {
  81.             for website in webSites {
  82.                 if host.contains(website) {
  83.                     decisionHandler(.allow)
  84.                     return
  85.                 }
  86.             }
  87.         }
  88.         decisionHandler(.cancel)
  89.  
  90. /// Вот так вот сразу крашится во время загрузки(появляется Alert и все, выкидывает исключение) Без кода ниже все работает.
  91.        
  92. ///     let alertController = UIAlertController(title: "Blocked", message: "The url was blocked", preferredStyle: .alert)
  93. ///     let alertAction = UIAlertAction( title : "Ok" ,
  94.                                          style : .default, handler: nil)
  95. ///     alertController.addAction(alertAction)
  96. ///     self.present(alertController, animated: true, completion: nil)
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement