I have a function that I call to access the User's contact information. But, I need this function to prompt the User with an alert that can redirect them to their Settings page if the authorizationStatus is denied.
So far, I have code that successfully prompts the user for their Contact Information. But, I want to run the show_settings_alert function if the case ends up being .denied as shown below:
func requestContactPermissions(completion: @escaping (Bool) -> ()) {
let store = CNContactStore()
var authStatus = CNContactStore.authorizationStatus(for: .contacts)
switch authStatus {
case .denied:
show_settings_alert()
}
Here is the show_settings_alert function that I found within a handful of tutorials regarding this problem:
func show_settings_alert() {
let alertController = UIAlertController(title: "TITLE", message: "Please go to Settings and turn on the permissions", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in })
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
self.present(alertController, animated: true, completion: nil)
}
The issue is, I am attempting to run self.present within a function, and thus, I do not have access to self. I also recognize this is UIKit code, and I'm not entirely sure how to get the same functionality while working with SwiftUI.
I know there is an Alert class that comes with SwiftUI, but I'd prefer to not have this logic within my template view.