# Permission **Repository Path**: zero-time/Permission ## Basic Information - **Project Name**: Permission - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-04 - **Last Updated**: 2021-06-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
Usage • Example • Installation • License
## Usage #### Permission > [`Permission.swift`](https://github.com/delba/Permission/blob/master/Source/Permission.swift) > [`PermissionStatus.swift`](https://github.com/delba/Permission/blob/master/Source/PermissionStatus.swift) ```swift let permission: Permission = .contacts print(permission.status) // PermissionStatus.NotDetermined permission.request { status in switch status { case .authorized: print("authorized") case .denied: print("denied") case .disabled: print("disabled") case .notDetermined: print("not determined") } } ``` ##### Supported Permissions > [`PermissionType.swift`](https://github.com/delba/Permission/blob/master/Source/PermissionType.swift) > [`PermissionTypes/`](https://github.com/delba/Permission/tree/master/Source/PermissionTypes) - [`AddressBook`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/AddressBook.swift) (Deprecated in iOS 9.0) - [`Bluetooth`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Bluetooth.swift) - [`Camera`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Camera.swift) - [`Contacts`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Contacts.swift) - [`Events`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Events.swift) - [`Motion`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Motion.swift) - [`Microphone`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Microphone.swift) - [`Notifications`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Notifications.swift) - [`Photos`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Photos.swift) - [`Reminders`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Reminders.swift) - [`LocationAlways`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/LocationAlways.swift) - [`LocationWhenInUse`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/LocationWhenInUse.swift) - [`MediaLibrary`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/MediaLibrary.swift) - [`SpeechRecognizer`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/SpeechRecognizer.swift) - [`Siri`](https://github.com/delba/Permission/blob/master/Source/PermissionTypes/Siri.swift) #### PermissionAlert > [`PermissionAlert.swift`](https://github.com/delba/Permission/blob/master/Source/PermissionAlert.swift) ##### Denied and disabled alerts When you first request a permission, a system alert is presented to the user. If you request a permission that was denied/disabled, a `PermissionAlert` will be presented. You might want to change the default `title`, `message`, `cancel` and `settings` text: ```swift let alert = permission.deniedAlert // or permission.disabledAlert alert.title = "Please allow access to your contacts" alert.message = nil alert.cancel = "Cancel" alert.settings = "Settings" ``` Set `permission.presentDeniedAlert = false` or `permission.presentDisabledAlert = false` if you don't want to present these alerts. ##### Pre-permission alerts In order not to burn your only chance of displaying the system alert, you can present a **pre-permission alert**. See this [article](http://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/) for more informations. ```swift permission.presentPrePermissionAlert = true let alert = permission.prePermissionAlert alert.title = "Let Foo Access Photos?" alert.message = "This lets you choose which photos you want to add to your Foo profile" alert.cancel = "Not now" alert.confirm = "Give Access" ``` The system alert will only be presented if the user taps "Give Access". #### PermissionSet > [`PermissionSet.swift`](https://github.com/delba/Permission/blob/master/Source/PermissionSet.swift) Use a `PermissionSet` to check the status of a group of `Permission` and to react when a permission is requested. ```swift let permissionSet = PermissionSet(.contacts, .camera, .microphone, .photos) permissionSet.delegate = self print(permissionSet.status) // PermissionStatus.NotDetermined // ... func permissionSet(permissionSet: PermissionSet, willRequestPermission permission: Permission) { print("Will request \(permission)") } func permissionSet(permissionSet: PermissionSet, didRequestPermission permission: Permission) { switch permissionSet.status { case .authorized: print("all the permissions are granted") case .denied: print("at least one permission is denied") case .disabled: print("at least one permission is disabled") case .notDetermined: print("at least one permission is not determined") } } ``` #### PermissionButton > [`PermissionButton`](https://github.com/delba/Permission/blob/master/Source/PermissionButton.swift) A `PermissionButton` requests the permission when tapped and updates itself when its underlying permission status changes. ```swift let button = PermissionButton(.photos) ``` `PermissionButton` is a subclass of `UIButton`. All the getters and setters of `UIButton` have their equivalent in `PermissionButton`. ```swift button.setTitles([ .authorized: "Authorized", .denied: "Denied", .disabled: "Disabled", .notDetermined: "Not determined" ]) // button.setAttributedTitles // button.setTitleColors // button.setTitleShadowColors // button.setImages // button.setBackgroundImages // etc. ``` #### Third-party libraries: - [sunshinejr/**RxPermission**](https://github.com/sunshinejr/RxPermission) RxSwift bindings for Permissions API in iOS. ## Example ```swift class PermissionsViewController: UIViewController, PermissionSetDelegate { override func viewDidLoad() { super.viewDidLoad() let label = UILabel() let contacts = PermissionButton(.contacts) let camera = PermissionButton(.camera) let microphone = PermissionButton(.microphone) let photos = PermissionButton(.photos) contacts.setTitles([ .notDetermined: "Contacts - NotDetermined" .authorized: "Contacts - Authorized", .denied: "Contacts - Denied" ]) contacts.setTitleColors([ .notDetermined: .black, .authorized: .green, .denied: .red ]) // ... let permissionSet = PermissionSet(contacts, camera, microphone, photos) permissionSet.delegate = self label.text = String(permissionSet.status) for subview in [label, contacts, camera, microphone, photos] { view.addSubview(subview) } } func permissionSet(permissionSet: PermissionSet, didRequestPermission permission: Permission) { label.text = String(permissionSet.status) } } ```