// // MCareUtil.swift // mcare-demo-InHouse // // Created by SungWon Lee on 27/10/2018. // Copyright © 2018 LemonHealthcare. All rights reserved. // import Foundation import MCareFramework class MCareUtil { static let shared = MCareUtil() private init() {} // 커스텀 알림창을 띄워준다.(중복해서 띄워주기 싫은 알림창은 Tag를 지정) func showCustomAlert(viewTag: Int = 0, title: String?, message: String, firstBtnTitle: String, secondBtnTitle: String?, titleIcon: String?, firstBtnAction: (() -> Void)?, secondBtnAction: (() -> Void)?) { DispatchQueue.main.async { // viewTag가 0이 아니고 뷰가 떠있으면 다시 띄워주지 않음 if viewTag != 0 && self.isViewWithTag(tag: viewTag) { return } var alertTitle: String? = title if alertTitle == nil { alertTitle = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "" } let alertView: MCareCustomAlerview = MCareCustomAlerview(viewTag: viewTag, title: alertTitle, message: message, firstBtnTitle: firstBtnTitle, secondBtnTitle: secondBtnTitle, titleIcon: titleIcon, firstBtnAction: { () -> Void in // firstbutton click handler if let _firstButtonAction = firstBtnAction { _firstButtonAction() } }, secondBtnAction: { () -> Void in // handler if let _secondButtonAction = secondBtnAction { _secondButtonAction() } }) alertView.show() } } // 뷰 태크로 해당 뷰가 뷰 히어라키에 존재하는지 찾는다. func isViewWithTag(tag: Int) -> Bool { guard let topView = UIApplication.shared.keyWindow?.subviews[0] else { return false } var result: Bool = false for item in topView.subviews { if item.tag == tag { result = true break } } return result } //MARK: =최상위 ViewController 반환 func topViewController() -> UIViewController? { let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate guard var vc = appDelegate.window?.rootViewController else { return nil } while let presentedViewController = vc.presentedViewController { vc = presentedViewController } return vc } //MARK: =앱을 종료 처리 func exitApplication(alertMessage: String?) { var alertMsg:String if let _alertMessage = alertMessage { alertMsg = _alertMessage + "\n" + NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.") } else { alertMsg = NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.") } let alertBtnTitle = NSLocalizedString("AlertOK", comment: "확인") showCustomAlert(title: nil, message: alertMsg, firstBtnTitle: alertBtnTitle, secondBtnTitle: nil, titleIcon: MCarePlusConstants.MCARE_ALERT_TITLE_ICON, firstBtnAction: { () -> Void in DispatchQueue.main.async { exit(0) } }, secondBtnAction: nil) } } extension Optional where Wrapped == String { var isNilOrEmpty: Bool { return self?.trimmingCharacters(in: .whitespaces).isEmpty ?? true } }