Advertisement
periclase_software

Untitled

Jan 16th, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.38 KB | None | 0 0
  1.  
  2. struct PermissionAlert: ViewModifier {
  3.    
  4.     var message: String
  5.     @Binding var isPresented: Bool
  6.    
  7.     func body(content: Content) -> some View {
  8.         content
  9.             .alert(message, isPresented: $isPresented) {
  10.                 Button("Cancel", role: .cancel, action: {})
  11.                 Button("Go to Settings") { goToSettingsAction() }
  12.             } message: { }
  13.     }
  14.    
  15.     private func goToSettingsAction() {
  16.         guard let url = URL(string: UIApplication.openSettingsURLString),
  17.               UIApplication.shared.canOpenURL(url) else {
  18.             return
  19.         }
  20.         UIApplication.shared.open(url, options: [:], completionHandler: nil)
  21.     }
  22. }
  23.  
  24. extension View {
  25.     /// Presents an alert that gives an option to route to the app settings if some permission is denied.
  26.     /// - Parameters:
  27.     ///   - message: The message to show on the alert.
  28.     ///   - isPresented: Binds to the presentation of the alert.
  29.     func permissionAlert(_ message: String, isPresented: Binding<Bool>) -> some View {
  30.         modifier(PermissionAlert(message: message, isPresented: isPresented))
  31.     }
  32.    
  33.     /// Presents the alert if photo library access has not been given.
  34.     func photoLibraryPermissionAlert(isPresented: Binding<Bool>) -> some View {
  35.         permissionAlert("Photo Library access has not been granted.", isPresented: isPresented)
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement