SwiftModalWebVC.swift 4.5 KB

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