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"))
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.
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.
SwiftUI uses a declarative syntax, so you can simply state what your user interface should do.
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.
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")))
}
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)!)
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With