Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of openSettingsURLString in SwiftUI?

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"))
like image 903
Alexis Wei Avatar asked Jun 03 '20 17:06

Alexis Wei


People also ask

Is there a Viewdidload in SwiftUI?

SwiftUI gives us equivalents to UIKit's viewDidAppear() and viewDidDisappear() in the form of onAppear() and onDisappear() . You can attach any code to these two events that you want, and SwiftUI will execute them when they occur.

Should I build my app in SwiftUI or UIKit?

So, to answer the question directly: yes, you should get busy learning SwiftUI because it is the future of app development on Apple's platforms, but at some point you still need to learn UIKit because those skills will be useful for years to come.

What code does SwiftUI use?

SwiftUI uses a declarative syntax, so you can simply state what your user interface should do.

Is SwiftUI same as Xcode?

Xcode and Swift are both software development products developed by Apple. Swift is a programming language used to create apps for iOS, macOS, tvOS, and watchOS. Xcode is an Integrated Development Environment (IDE) that comes with a set of tools that helps you build Apple-related apps.


3 Answers

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")))
        }

like image 83
Asperi Avatar answered Oct 26 '22 16:10

Asperi


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)!)
    }
}        
like image 3
Vexy Avatar answered Oct 26 '22 15:10

Vexy


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
like image 1
Cesare Piersigilli Avatar answered Oct 26 '22 17:10

Cesare Piersigilli