20

I want to create an alert that redirects the user to Settings App after they denied camera usage for the first time, but the only way that I've seen so far uses UIKit and

let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: {action in
        UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
    })

In SwiftUI, there is an actions option within alert, how would I be able to correctly open settings through SwiftUI's version of alert?

.alert(isPresented: $alertVisible) { () -> Alert in Alert (title: Text("Camera access required to take photos"), message: Text("Go to Settings?"), 
     primaryButton: .default(Text("Settings"), action: UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)), 
     secondaryButton: .default(Text("Cancel"))
Alexis Wei
  • 215
  • 2
  • 6

3 Answers3

39

Here it is

.alert(isPresented: $alertVisible) {
     Alert (title: Text("Camera access required to take photos"),
            message: Text("Go to Settings?"),
            primaryButton: .default(Text("Settings"), action: {
                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
            }),
            secondaryButton: .default(Text("Cancel")))
        }

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
8

In shortest form you can use something like this:

Link("Open settings ?", destination: URL(string: UIApplication.openSettingsURLString)!)

This will create you a simple looking button that will lead directly to the Settings app. Of course, you can customize the look further using view modifiers.


Alternatively if you need more control, you can use openURL @Environment property wrapper in your view, like so:

struct SomeView: View {
    @Environment(\.openURL) var openURL

    var body: some View {
        Button(action: openSettings) {
            Label("Open settings?", systemImage: "gear")
        }
    }

    private func openSettings() {
        openURL(URL(string: UIApplication.openSettingsURLString)!)
    }
}        
Vexy
  • 1,274
  • 1
  • 12
  • 17
2

Here is the solution (see answer @rmaddy at [link][1]):

.alert(isPresented: $showLibraryPicker, content: {
            Alert(title: Text("Camera access required to take photos"),
                  message: Text("Go to Settings?"),
                  primaryButton: .default(Text("Settings"),
                                          action: {
                                            let url: String
                                            #if targetEnvironment(macCatalyst)
                                            url = "x-apple.systempreferences:com.apple.preference.security?Privacy_Photos"
                                            #else
                                            url = UIApplication.openSettingsURLString
                                            #endif
                                            UIApplication.shared.open(URL(string: url)!)
                                          }), secondaryButton: .default(Text("Cancel")))
        })


  [1]: https://stackoverflow.com/questions/58330598/is-there-a-way-to-send-the-user-to-the-apps-privacy-settings-under-macos-like-w