SwiftModalWebVC.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. var doneButtonHide: Bool! = true
  17. public enum SwiftModalWebVCTheme {
  18. case lightBlue, lightBlack, dark, custom
  19. }
  20. weak var webViewDelegate: UIWebViewDelegate? = nil
  21. public convenience init(urlString: String) {
  22. self.init(pageURL: URL(string: urlString)!)
  23. }
  24. public convenience init(urlString: String, theme: SwiftModalWebVCTheme) {
  25. self.init(pageURL: URL(string: urlString)!, theme: theme)
  26. }
  27. public convenience init(urlString: String, theme: SwiftModalWebVCTheme, color: UIColor, colorText: UIColor) {
  28. self.init(pageURL: URL(string: urlString)!, theme: theme, color: color, colorText: colorText)
  29. }
  30. public convenience init(pageURL: URL) {
  31. self.init(request: URLRequest(url: pageURL))
  32. }
  33. public convenience init(pageURL: URL, theme: SwiftModalWebVCTheme, color : UIColor = UIColor.clear, colorText: UIColor = UIColor.black) {
  34. self.init(request: URLRequest(url: pageURL), theme: theme, color: color, colorText: colorText)
  35. }
  36. public init(request: URLRequest, theme: SwiftModalWebVCTheme = .dark, color: UIColor = UIColor.clear, colorText: UIColor = UIColor.black) {
  37. let webViewController = SwiftWebVC(aRequest: request)
  38. webViewController.storedStatusColor = UINavigationBar.appearance().barStyle
  39. super.init(rootViewController: webViewController)
  40. let doneButton = UIBarButtonItem(image: SwiftWebVC.bundledImage(named: "SwiftWebVCDismiss"),
  41. style: UIBarButtonItemStyle.plain,
  42. target: webViewController,
  43. action: #selector(SwiftWebVC.doneButtonTapped))
  44. switch theme {
  45. case .lightBlue:
  46. doneButton.tintColor = nil
  47. webViewController.buttonColor = nil
  48. webViewController.titleColor = UIColor.black
  49. UINavigationBar.appearance().barStyle = UIBarStyle.default
  50. case .lightBlack:
  51. doneButton.tintColor = UIColor.darkGray
  52. webViewController.buttonColor = UIColor.darkGray
  53. webViewController.titleColor = UIColor.black
  54. UINavigationBar.appearance().barStyle = UIBarStyle.default
  55. case .dark:
  56. doneButton.tintColor = UIColor.white
  57. webViewController.buttonColor = UIColor.white
  58. webViewController.titleColor = UIColor.groupTableViewBackground
  59. UINavigationBar.appearance().barStyle = UIBarStyle.black
  60. case .custom:
  61. doneButton.tintColor = colorText
  62. webViewController.buttonColor = colorText
  63. webViewController.titleColor = colorText
  64. self.navigationBar.isTranslucent = false
  65. UINavigationBar.appearance().barTintColor = color
  66. self.toolbar.isTranslucent = false
  67. self.toolbar.barTintColor = color
  68. }
  69. if (doneButtonHide == false) {
  70. if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
  71. webViewController.navigationItem.leftBarButtonItem = doneButton
  72. }
  73. else {
  74. webViewController.navigationItem.rightBarButtonItem = doneButton
  75. }
  76. }
  77. webViewController.delegate = self
  78. }
  79. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  80. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  81. }
  82. required public init(coder aDecoder: NSCoder) {
  83. fatalError("init(coder:) has not been implemented")
  84. }
  85. override public func viewWillAppear(_ animated: Bool) {
  86. super.viewWillAppear(false)
  87. }
  88. }
  89. extension SwiftModalWebVC: SwiftWebVCDelegate {
  90. public func didStartLoading() {
  91. self.delegateWeb?.didStartLoading()
  92. }
  93. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  94. self.delegateWeb?.didReceiveServerRedirectForProvisionalNavigation(url: url)
  95. }
  96. public func didFinishLoading(success: Bool) {
  97. print("Finished loading. Success: \(success).")
  98. }
  99. public func didFinishLoading(success: Bool, url: URL) {
  100. self.delegateWeb?.didFinishLoading(success: success, url: url)
  101. }
  102. }