SwiftModalWebVC.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. func loginWebClose()
  14. }
  15. public class SwiftModalWebVC: UINavigationController {
  16. public weak var delegateWeb: SwiftModalWebVCDelegate?
  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, doneButtonVisible: Bool, hideToolbar: Bool = false) {
  28. self.init(pageURL: URL(string: urlString)!, theme: theme, color: color, colorText: colorText, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar)
  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, doneButtonVisible: Bool = false, hideToolbar: Bool = false) {
  34. self.init(request: URLRequest(url: pageURL), theme: theme, color: color, colorText: colorText, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar)
  35. }
  36. public init(request: URLRequest, theme: SwiftModalWebVCTheme = .dark, color: UIColor = UIColor.clear, colorText: UIColor = UIColor.black, doneButtonVisible: Bool = false, hideToolbar: Bool = false) {
  37. let webViewController = SwiftWebVC(aRequest: request, hideToolbar: hideToolbar)
  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. webViewController.view.backgroundColor = color
  65. self.navigationBar.isTranslucent = false
  66. UINavigationBar.appearance().barTintColor = color
  67. self.toolbar.isTranslucent = false
  68. self.toolbar.barTintColor = color
  69. }
  70. if (doneButtonVisible == true) {
  71. if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
  72. webViewController.navigationItem.leftBarButtonItem = doneButton
  73. }
  74. else {
  75. webViewController.navigationItem.rightBarButtonItem = doneButton
  76. }
  77. }
  78. webViewController.delegate = self
  79. }
  80. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  81. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  82. }
  83. required public init(coder aDecoder: NSCoder) {
  84. fatalError("init(coder:) has not been implemented")
  85. }
  86. override public func viewWillAppear(_ animated: Bool) {
  87. super.viewWillAppear(false)
  88. }
  89. public override func viewWillDisappear(_ animated: Bool) {
  90. super.viewDidDisappear(animated)
  91. }
  92. public override func viewDidDisappear(_ animated: Bool) {
  93. super.viewDidDisappear(animated)
  94. }
  95. }
  96. extension SwiftModalWebVC: SwiftWebVCDelegate {
  97. public func didStartLoading() {
  98. self.delegateWeb?.didStartLoading()
  99. }
  100. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  101. self.delegateWeb?.didReceiveServerRedirectForProvisionalNavigation(url: url)
  102. }
  103. public func didFinishLoading(success: Bool) {
  104. print("Finished loading. Success: \(success).")
  105. }
  106. public func didFinishLoading(success: Bool, url: URL) {
  107. self.delegateWeb?.didFinishLoading(success: success, url: url)
  108. }
  109. public func loginWebClose() {
  110. self.delegateWeb?.loginWebClose()
  111. }
  112. }