[iOS | SwiftUI] UIAlertController 띄우는 법

someng·2022년 10월 23일
0

iOS

목록 보기
13/33

🍟 문제 정의

앨범 접근 권한을 거부한 유저에게 '권한 설정' 페이지로 이동할 수 있도록 alert 창을 보여주는 상황이다.
SwiftUI에서 View를 사용하지 않고 프로그램 상으로 UIAlertController를 띄우는 방법이 필요하다.

🤡 해결 방법

  1. UIAlertController의 title, message 설정
  2. controller에 action 추가 (ex: 취소, 설정)
let viewController = UIApplication.shared.windows.first!.rootViewController!
viewController.present(alert, animated: true, completion: nil)

👩🏻‍💻 전체 코드

    func AuthSettingOpen(AuthString: String) {
        if let AppName = Bundle.main.infoDictionary!["CFBundleName"] as? String {
            let message = "\(AppName)이 \(AuthString) 접근 허용되어 있지않습니다. \r\n 설정화면으로 가시겠습니까?"
            let alert = UIAlertController(title: "설정", message: message, preferredStyle: .alert)
            
            alert.addAction(UIAlertAction(title: "취소", style: .cancel, handler: nil))
            alert.addAction(UIAlertAction(title: "설정", style: .default, handler: { _ in
                if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(settingsURL)
                }
            }))
            
            let viewController = UIApplication.shared.windows.first!.rootViewController!
            viewController.present(alert, animated: true, completion: nil)
            
        }
    }
profile
👩🏻‍💻 iOS Developer

0개의 댓글