Skip to content
main
Switch branches/tags
Code

PermissionsKit

It was called SPPermissions, what happen? I put together a team that deals with opensource and gave the library to the company. It is now under a universal clean name. It will help develop the library more actively, even when I can't do it myself. Now I am doing 9 version with more permissions and new interface. Share with me your ideas.

Universal API for querying the rarefaction and getting the current status .authorized, .denied & .notDetermined. Availalbe three ready-use interface - list, dialog & native. Supports iPad, dark mode and has ready localizations.

Community

Navigate

Permissions

Icon Permission Key for Info.plist Get Status Make Request
Bluetooth NSBluetoothAlwaysUsageDescription, NSBluetoothPeripheralUsageDescription
Calendar NSCalendarsUsageDescription
Camera NSCameraUsageDescription
Contacts NSContactsUsageDescription
FaceID NSFaceIDUsageDescription
Health NSHealthUpdateUsageDescription, NSHealthShareUsageDescription
Location Always NSLocationAlwaysAndWhenInUseUsageDescription
Location When In Use NSLocationWhenInUseUsageDescription
Media Library NSAppleMusicUsageDescription
Microphone NSMicrophoneUsageDescription
Motion NSMotionUsageDescription
Notification
Photo Library NSPhotoLibraryUsageDescription, NSPhotoLibraryAddUsageDescription
Reminders NSRemindersUsageDescription
Siri NSSiriUsageDescription
Speech Recognizer NSSpeechRecognitionUsageDescription
Tracking NSUserTrackingUsageDescription

Installation

Ready to use on iOS 11+. Supports iOS, tvOS, and SwiftUI.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/sparrowcode/PermissionsKit", .upToNextMajor(from: "8.0.0"))
]

Next choose the permissions you need. But don't add all of them, because apple will reject app.

CocoaPods:

This is an outdated way of doing things. I advise you to use SPM. However, I will continue to support Cocoapods for some time.

Cocoapods Instalation

CocoaPods is a dependency manager. For usage and installation instructions, visit their website. To integrate using CocoaPods, specify it in your Podfile:

pod 'PermissionsKit/NotificationPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'

Due to Apple's new policy regarding permission access you need to specifically define what kind of permissions you want to access using subspecs.

pod 'PermissionsKit/CameraPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/ContactsPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/CalendarPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/PhotoLibraryPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/NotificationPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/MicrophonePermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/RemindersPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/SpeechRecognizerPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/LocationWhenInUsePermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/LocationAlwaysPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/MotionPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/MediaLibraryPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/BluetoothPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/TrackingPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/FaceIDPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/SiriPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'
pod 'PermissionsKit/HealthPermission', :git => 'https://github.com/sparrowcode/PermissionsKit'

Request & Status

import PermissionsKit
import NotificationPermission

// Request permission.
Permission.notification.request {
    
    // Get status
    let authorized = Permission.notification.authorized
}

Ready-use Interface

PermissionsKit has three presentation styles: Dialog, List and Native. Each interface has delegates and a data source. If you want see an example app, open Example Apps/PermissionsKit.xcodeproj.

List

It's native UITableViewController. Use it when you have more than two permissions. An example of how it is used:

let controller = PermissionsKit.list([.calendar, .camera, .contacts])

// Ovveride texts in controller
controller.titleText = "Title Text"
controller.headerText = "Header Text"
controller.footerText = "Footer Text"

// Set `DataSource` or `Delegate` if need. 
// By default using project texts and icons.
controller.dataSource = self
controller.delegate = self

// If you want auto dismiss controler,
// when all permissions has any determinated state
// set dismiss mode `allPermissionsDeterminated`.
// By default dismiss controller happen only when all permission allowed.
controller.dismissCondition = .allPermissionsDeterminated

// Always use this method for present
controller.present(on: self)

Dialog

This is a modal alert. I recommend using this alert style when you have less than three requested permissions. Usage example:

let controller = PermissionsKit.dialog([.camera, .photoLibrary])

// Override texts in controller
controller.titleText = "Title Text"
controller.headerText = "Header Text"
controller.footerText = "Footer Text"

// Set `DataSource` or `Delegate` if need. 
// By default using project texts and icons.
controller.dataSource = self
controller.delegate = self

// If you want auto dismiss controler,
// when all permissions has any determinated state
// set dismiss mode `allPermissionsDeterminated`.
// By default dismiss controller happen only when all permission allowed.
controller.dismissCondition = .allPermissionsDeterminated

// Always use this method for present
controller.present(on: self)

Native

Request permissions with native UIAlertController. You can request many permissions at once:

let controller = PermissionsKit.native([.calendar, .camera, .contacts])

// Set `Delegate` if need. 
controller.delegate = self

// Always use this method for request. 
controller.present(on: self)

DataSource

For data source using protocol PermissionsDataSource. You can customize the permission cells and provide denied alert texts.

extension Controller: PermissionsDataSource {
    
    func configure(_ cell: PermissionTableViewCell, for permission: Permission) {
        
        // Here you can customise cell, like texts or colors.
        cell.permissionTitleLabel.text = "Title"
        cell.permissionDescriptionLabel.text = "Description"
        
        // If you need change icon, choose one of this:
        cell.permissionIconView.setPermissionType(.bluetooth)
        cell.permissionIconView.setCustomImage(UIImage.init(named: "custom-name"))
        cell.permissionIconView.setCustomView(YourView())
    }
}

Denied alert

If a permission is denied, you can provide an alert to the user with an option to open settings. An example of how you can customise the alert text:

let texts = PermissionDeniedAlertTexts()
texts.titleText = "Permission denied"
texts.descriptionText = "Please, go to Settings and allow permission."
texts.actionText = "Settings"
texts.cancelText = "Cancel"

Next implement the following method and return:

func deniedPermissionAlertTexts(for permission: Permission) -> PermissionDeniedAlertTexts? {
    
    // Custom texts:
    return texts
    
    // or default texts:
    // return .default
}

Delegate

To get hidden, allowed or denied events , set the delegate with protocol PermissionsDelegate:

extension Controller: PermissionsDelegate {
    
    func didHidePermissions(_ permissions: [Permission]) {}
    func didAllowPermission(_ permission: Permission) {}
    func didDeniedPermission(_ permission: Permission) {}
}

Localizations

PermissionsKit has ready-to-use localizations for:

  • English en
  • Arabic ar
  • German de
  • Spanish es
  • French fr
  • Polish pl
  • Portuguese pt
  • Ukrainian uk
  • Russian ru
  • Chinese Simplified Han zh_Hans
  • Italian it
  • Chinese Traditional zh_Hant
  • Persian fa

If you want to add more, please, create folder [language_id].lproj and make a pull request. If you want to use your custom strings, check the DataSource section.

Keys in Info.plist

You need to add some keys to the Info.plist file with descriptions, per Apple's requirement(s). You can get a plist of keys for permissions as follows:

let key = Permission.bluetooth.usageDescriptionKey

Do not use the description as the name of the key.

If you use xliff localization export, keys will be create automatically. If you prefer do the localization file manually, you need to create InfoPlist.strings, select languages on the right side menu and add keys as keys in plist-file. See:

"NSCameraUsageDescription" = "Here description of usage camera";

Apple Review

Apple changed its review guidelines in June 2021. When requesting permissions, apps should require users to always request and make a decision whether to allow or decline the permission. For this reason, the close button in PermissionsKit is hidden by default. If you want to force show the close button, run the following code:

// Show & hide close button
controller.showCloseButton = true

// Enable or Disable drag intercation
controller.allowSwipeDismiss = true

Also changed title for button. Instead of allow now using continue. The Apple Review Team asked for this. For details, check out this issue.

Apps Using