SwiftModalWebVC.swift 4.6 KB

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