MCareUtil.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // MCareUtil.swift
  3. // mcare-demo-InHouse
  4. //
  5. // Created by SungWon Lee on 27/10/2018.
  6. // Copyright © 2018 LemonHealthcare. All rights reserved.
  7. //
  8. import Foundation
  9. import MCareFramework
  10. class MCareUtil {
  11. static let shared = MCareUtil()
  12. private init() {}
  13. // 커스텀 알림창을 띄워준다.(중복해서 띄워주기 싫은 알림창은 Tag를 지정)
  14. func showCustomAlert(viewTag: Int = 0, title: String?, message: String, firstBtnTitle: String, secondBtnTitle: String?, titleIcon: String?, firstBtnAction: (() -> Void)?, secondBtnAction: (() -> Void)?) {
  15. DispatchQueue.main.async {
  16. // viewTag가 0이 아니고 뷰가 떠있으면 다시 띄워주지 않음
  17. if viewTag != 0 && self.isViewWithTag(tag: viewTag) {
  18. return
  19. }
  20. var alertTitle: String? = title
  21. if alertTitle == nil {
  22. alertTitle = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""
  23. }
  24. let alertView: MCareCustomAlerview = MCareCustomAlerview(viewTag: viewTag, title: alertTitle, message: message, firstBtnTitle: firstBtnTitle, secondBtnTitle: secondBtnTitle, titleIcon: titleIcon, firstBtnAction: { () -> Void in
  25. // firstbutton click handler
  26. if let _firstButtonAction = firstBtnAction {
  27. _firstButtonAction()
  28. }
  29. }, secondBtnAction: { () -> Void in
  30. // handler
  31. if let _secondButtonAction = secondBtnAction {
  32. _secondButtonAction()
  33. }
  34. })
  35. alertView.show()
  36. }
  37. }
  38. // 뷰 태크로 해당 뷰가 뷰 히어라키에 존재하는지 찾는다.
  39. func isViewWithTag(tag: Int) -> Bool {
  40. guard let topView = UIApplication.shared.keyWindow?.subviews[0] else {
  41. return false
  42. }
  43. var result: Bool = false
  44. for item in topView.subviews {
  45. if item.tag == tag {
  46. result = true
  47. break
  48. }
  49. }
  50. return result
  51. }
  52. //MARK: =최상위 ViewController 반환
  53. func topViewController() -> UIViewController? {
  54. let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
  55. guard var vc = appDelegate.window?.rootViewController else { return nil }
  56. while let presentedViewController = vc.presentedViewController {
  57. vc = presentedViewController
  58. }
  59. return vc
  60. }
  61. //MARK: =앱을 종료 처리
  62. func exitApplication(alertMessage: String?) {
  63. var alertMsg:String
  64. if let _alertMessage = alertMessage {
  65. alertMsg = _alertMessage + "\n" + NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.")
  66. }
  67. else {
  68. alertMsg = NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.")
  69. }
  70. let alertBtnTitle = NSLocalizedString("AlertOK", comment: "확인")
  71. showCustomAlert(title: nil, message: alertMsg, firstBtnTitle: alertBtnTitle, secondBtnTitle: nil, titleIcon: MCarePlusConstants.MCARE_ALERT_TITLE_ICON, firstBtnAction: { () -> Void in
  72. DispatchQueue.main.async {
  73. exit(0)
  74. }
  75. }, secondBtnAction: nil)
  76. }
  77. }
  78. extension Optional where Wrapped == String {
  79. var isNilOrEmpty: Bool {
  80. return self?.trimmingCharacters(in: .whitespaces).isEmpty ?? true
  81. }
  82. }