Kyle Huender
Kyle Huender

Reputation: 85

Swiftui: Display alert popup programmatically via a function

I have a function that is can be called in several places in my app across several different views.

In essence, it checks the status of the notification authorization status. If it is 'authorized', it is okay and the function does nothing. If it is 'notDetermined' it prompts the user to authorize. Because the app will not 100% work without notification permissions I'd like to remind the user that they are required. Everything works according to plan, just trying to figure out either a way to prompt the user with an alert (that says go to Settings > Notifications > ) or direct to another page in the app.

I tried both directing to a page by calling that page in the function as well as trying to display an alert directly in the function, but it is my understanding that both of these options have to be tied to a view.

The app asks for notification permissions on initial start up and install of the app so most of this shouldn't be an issue. But as a safeguard I'd like to do the above.

Edit/Update:

import SwiftUI

struct ContentView: View {

@State var showAlert = false

// FUNCTION: Ask for Notification Permissions
func notificationPermissions() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    if settings.authorizationStatus == .authorized {
        // Notifications are authorized
        print("Notifications are AUTHORIZED")
    }
    else if settings.authorizationStatus == .denied {
        // ALERT: Notifications are denied
        print("ALERT: Notifications are DENIED")
        self.showAlert = true
        print(self.showAlert) // Even though above it is set to true, the print statement says false, the alert doesn't display
    }
    else {
        // Notifications are notDetermined
        print("Notifications are NOTDETERMINED")
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
            if success {
                print("User has accepted Notification Permissions")
            } else if let error = error {
                print(error.localizedDescription)
            }
        }
    }
}
}

// FUNCTION: Display Notification Reminder Alert
func notificationReminder() -> Alert {
Alert(
    title: Text("Notifications Required"),
    message: Text("Please authorize notifications by going to Settings > Notifications > Remindr"),
    dismissButton: .default(Text("Okay")))
}

In another view...

struct newRemindr: View {

// Alert Control
@Binding var showAlert: Bool

@State var notification = ""
var contentView = ContentView()

// View Controller
var body: some View {
NavigationView {
    Form {
        TextField("Write your notification...", text: $notification)
        
        Button(action: {
            self.contentView.notificationPermissions() // Calls the function above from the content view
        }) {
            Text("Submit")
        }
        
    }
}
}

Upvotes: 5

Views: 6193

Answers (1)

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

you can use alert modifier in your app's ContentView (root view).

struct ContentView: View {
    // pass this var as binding to other views
    @State var showAlert = false
    
    func notificationReminder() -> Alert {
            Alert(
                title: Text("Notifications Required"),
                message: Text("Please authorize notifications by going to Settings > Notifications > Remindr"),
                dismissButton: .default(Text("Okay")))
    }
    
    var body: some View {
        
        VStack {
            Text("this is my main view")
        }
        .alert(isPresented: self.$showAlert,
               content: { self.notificationReminder() })
        
    }
    
}

you can show that alert from anywhere in your app. you just have to set showAlert to true.

Upvotes: 4

Related Questions