UIViewController.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // UIViewController.swift
  3. //
  4. // Created by Nathan Racklyeft on 1/16/16.
  5. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  6. //
  7. import UIKit
  8. extension UIViewController {
  9. /**
  10. Convenience method to present an alert controller on the active view controller
  11. - parameter title: The title of the alert
  12. - parameter message: The message of the alert
  13. - parameter animated: Whether to animate the alert
  14. - parameter completion: An optional closure to execute after the presentation finishes
  15. */
  16. public func presentAlertControllerWithTitle(_ title: String?, message: String, animated: Bool = true, completion: (() -> Void)? = nil) {
  17. let alert = UIAlertController(
  18. title: title,
  19. message: message,
  20. preferredStyle: .alert
  21. )
  22. let action = UIAlertAction(
  23. title: LocalizedString("com.loudnate.LoopKit.errorAlertActionTitle", tableName: "LoopKit", value: "OK", comment: "The title of the action used to dismiss an error alert"),
  24. style: .default,
  25. handler: nil
  26. )
  27. alert.addAction(action)
  28. alert.preferredAction = action
  29. presentViewControllerOnActiveViewController(alert, animated: animated, completion: completion)
  30. }
  31. /**
  32. Convenience method to display an error object in an alert controller
  33. - parameter error: The error to display
  34. - parameter animated: Whether to animate the alert
  35. - parameter completion: An optional closure to execute after the presentation finishes
  36. */
  37. public func presentAlertControllerWithError(_ error: Error, animated: Bool = true, completion: (() -> Void)? = nil) {
  38. // See: https://forums.developer.apple.com/thread/17431
  39. // The compiler automatically emits the code necessary to translate between any ErrorType and NSError.
  40. let castedError: NSError = error as NSError
  41. presentAlertControllerWithTitle(error.localizedDescription,
  42. message: castedError.userInfo[NSLocalizedRecoverySuggestionErrorKey] as? String ?? String(describing: error),
  43. animated: animated,
  44. completion: completion
  45. )
  46. }
  47. /**
  48. Convenience method to present a view controller on the active view controller.
  49. If the receiver is not in a window, or already has a presented view controller, this method will
  50. attempt to find the most appropriate view controller for presenting.
  51. - parameter viewControllerToPresent: The view controller to display over the view controller’s content
  52. - parameter animated: Whether to animate the presentation
  53. - parameter completion: An optional closure to execute after the presentation finishes
  54. */
  55. public func presentViewControllerOnActiveViewController(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
  56. var presentingViewController: UIViewController? = self
  57. if presentingViewController?.view.window == nil {
  58. presentingViewController = UIApplication.shared.delegate?.window??.rootViewController
  59. }
  60. while presentingViewController?.presentedViewController != nil {
  61. presentingViewController = presentingViewController?.presentedViewController
  62. }
  63. presentingViewController?.present(viewControllerToPresent, animated: animated, completion: completion)
  64. }
  65. }