Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct PermissionAlert: ViewModifier {
- var message: String
- @Binding var isPresented: Bool
- func body(content: Content) -> some View {
- content
- .alert(message, isPresented: $isPresented) {
- Button("Cancel", role: .cancel, action: {})
- Button("Go to Settings") { goToSettingsAction() }
- } message: { }
- }
- private func goToSettingsAction() {
- guard let url = URL(string: UIApplication.openSettingsURLString),
- UIApplication.shared.canOpenURL(url) else {
- return
- }
- UIApplication.shared.open(url, options: [:], completionHandler: nil)
- }
- }
- extension View {
- /// Presents an alert that gives an option to route to the app settings if some permission is denied.
- /// - Parameters:
- /// - message: The message to show on the alert.
- /// - isPresented: Binds to the presentation of the alert.
- func permissionAlert(_ message: String, isPresented: Binding<Bool>) -> some View {
- modifier(PermissionAlert(message: message, isPresented: isPresented))
- }
- /// Presents the alert if photo library access has not been given.
- func photoLibraryPermissionAlert(isPresented: Binding<Bool>) -> some View {
- permissionAlert("Photo Library access has not been granted.", isPresented: isPresented)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement