SwiftModalWebVC.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // SwiftModalWebVC.swift
  3. //
  4. // Created by Myles Ringle on 24/06/2015.
  5. // Transcribed from code used in SVWebViewController.
  6. // Copyright (c) 2015 Myles Ringle & Oliver Letterer. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol SwiftModalWebVCDelegate: class {
  10. func didStartLoading()
  11. func didReceiveServerRedirectForProvisionalNavigation(url: URL)
  12. func didFinishLoading(success: Bool, url: URL)
  13. }
  14. public class SwiftModalWebVC: UINavigationController {
  15. public weak var delegateWeb: SwiftModalWebVCDelegate?
  16. public enum SwiftModalWebVCTheme {
  17. case lightBlue, lightBlack, dark, custom
  18. }
  19. weak var webViewDelegate: UIWebViewDelegate? = nil
  20. public convenience init(urlString: String) {
  21. self.init(pageURL: URL(string: urlString)!)
  22. }
  23. public convenience init(urlString: String, theme: SwiftModalWebVCTheme) {
  24. self.init(pageURL: URL(string: urlString)!, theme: theme)
  25. }
  26. public convenience init(urlString: String, theme: SwiftModalWebVCTheme, color: UIColor, colorText: UIColor, doneButtonVisible: Bool, hideToolbar: Bool = false) {
  27. self.init(pageURL: URL(string: urlString)!, theme: theme, color: color, colorText: colorText, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar)
  28. }
  29. public convenience init(pageURL: URL) {
  30. self.init(request: URLRequest(url: pageURL))
  31. }
  32. public convenience init(pageURL: URL, theme: SwiftModalWebVCTheme, color : UIColor = UIColor.clear, colorText: UIColor = UIColor.black, doneButtonVisible: Bool = false, hideToolbar: Bool = false) {
  33. self.init(request: URLRequest(url: pageURL), theme: theme, color: color, colorText: colorText, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar)
  34. }
  35. public init(request: URLRequest, theme: SwiftModalWebVCTheme = .dark, color: UIColor = UIColor.clear, colorText: UIColor = UIColor.black, doneButtonVisible: Bool = false, hideToolbar: Bool = false) {
  36. let webViewController = SwiftWebVC(aRequest: request, hideToolbar: hideToolbar)
  37. webViewController.storedStatusColor = UINavigationBar.appearance().barStyle
  38. super.init(rootViewController: webViewController)
  39. let doneButton = UIBarButtonItem(image: SwiftWebVC.bundledImage(named: "SwiftWebVCDismiss"),
  40. style: UIBarButtonItemStyle.plain,
  41. target: webViewController,
  42. action: #selector(SwiftWebVC.doneButtonTapped))
  43. switch theme {
  44. case .lightBlue:
  45. doneButton.tintColor = nil
  46. webViewController.buttonColor = nil
  47. webViewController.titleColor = UIColor.black
  48. UINavigationBar.appearance().barStyle = UIBarStyle.default
  49. case .lightBlack:
  50. doneButton.tintColor = UIColor.darkGray
  51. webViewController.buttonColor = UIColor.darkGray
  52. webViewController.titleColor = UIColor.black
  53. UINavigationBar.appearance().barStyle = UIBarStyle.default
  54. case .dark:
  55. doneButton.tintColor = UIColor.white
  56. webViewController.buttonColor = UIColor.white
  57. webViewController.titleColor = UIColor.groupTableViewBackground
  58. UINavigationBar.appearance().barStyle = UIBarStyle.black
  59. case .custom:
  60. doneButton.tintColor = colorText
  61. webViewController.buttonColor = colorText
  62. webViewController.titleColor = colorText
  63. self.navigationBar.isTranslucent = false
  64. UINavigationBar.appearance().barTintColor = color
  65. self.toolbar.isTranslucent = false
  66. self.toolbar.barTintColor = color
  67. }
  68. if (doneButtonVisible == true) {
  69. if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
  70. webViewController.navigationItem.leftBarButtonItem = doneButton
  71. }
  72. else {
  73. webViewController.navigationItem.rightBarButtonItem = doneButton
  74. }
  75. }
  76. webViewController.delegate = self
  77. }
  78. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  79. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  80. }
  81. required public init(coder aDecoder: NSCoder) {
  82. fatalError("init(coder:) has not been implemented")
  83. }
  84. override public func viewWillAppear(_ animated: Bool) {
  85. super.viewWillAppear(false)
  86. }
  87. }
  88. extension SwiftModalWebVC: SwiftWebVCDelegate {
  89. public func didStartLoading() {
  90. self.delegateWeb?.didStartLoading()
  91. }
  92. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  93. self.delegateWeb?.didReceiveServerRedirectForProvisionalNavigation(url: url)
  94. }
  95. public func didFinishLoading(success: Bool) {
  96. print("Finished loading. Success: \(success).")
  97. }
  98. public func didFinishLoading(success: Bool, url: URL) {
  99. self.delegateWeb?.didFinishLoading(success: success, url: url)
  100. }
  101. }