-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the web view controller into view and view controller, trimmed …
…it down a little and fixed a couple if iPhone glitches, then moved other parts of the app over to using it.
- Loading branch information
Showing
7 changed files
with
193 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// WebView.swift | ||
// Unwrap | ||
// | ||
// Created by Paul Hudson on 02/05/2019. | ||
// Copyright © 2019 Hacking with Swift. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import WebKit | ||
|
||
class WebView: UIView { | ||
weak var delegate: WebViewController? { | ||
didSet { | ||
webView.uiDelegate = delegate | ||
webView.navigationDelegate = delegate | ||
} | ||
} | ||
|
||
let webView = WKWebView() | ||
let progressView = UIProgressView(progressViewStyle: .bar) | ||
private var estimatedProgressObserver: NSKeyValueObservation? | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
backgroundColor = .white | ||
|
||
webView.translatesAutoresizingMaskIntoConstraints = false | ||
webView.allowsBackForwardNavigationGestures = true | ||
addSubview(webView) | ||
|
||
NSLayoutConstraint.activate([ | ||
webView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), | ||
webView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor), | ||
webView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), | ||
webView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor) | ||
]) | ||
|
||
progressView.isHidden = true | ||
progressView.translatesAutoresizingMaskIntoConstraints = false | ||
addSubview(progressView) | ||
|
||
NSLayoutConstraint.activate([ | ||
progressView.heightAnchor.constraint(equalToConstant: 2.0), | ||
progressView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), | ||
progressView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), | ||
progressView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor) | ||
]) | ||
|
||
setupEstimatedProgressObserver() | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setupEstimatedProgressObserver() { | ||
estimatedProgressObserver = webView.observe(\.estimatedProgress, options: .new) { [weak self] webView, _ in | ||
self?.progressView.setProgress(Float(webView.estimatedProgress), animated: true) | ||
} | ||
} | ||
|
||
func load(_ url: URL) { | ||
let request = URLRequest(url: url) | ||
webView.load(request) | ||
} | ||
|
||
@objc func reload() { | ||
webView.reload() | ||
} | ||
|
||
func loadingDidStart() { | ||
progressView.progress = 0 | ||
progressView.isHidden = false | ||
} | ||
|
||
func loadingDidFinish() { | ||
progressView.isHidden = true | ||
} | ||
|
||
func getShareItems() -> [Any] { | ||
guard let url = webView.url else { return [] } | ||
var shareItems: [Any] = [url] | ||
|
||
if let webViewTitle = webView.title { | ||
shareItems.append(webViewTitle) | ||
} | ||
|
||
return shareItems | ||
} | ||
} |
Oops, something went wrong.