Need help with SPPermissions?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

ivanvorobei
4.3K Stars 352 Forks MIT License 797 Commits 12 Opened issues

Description

Ask permissions on Swift. Available List, Dialog & Native interface. Can check state permission.

Services available

!
?

Need anything else?

Contributors list

SPPermissions

About

SPPermissions
is an API to ask for user permissions using Swift. The API provides for three UI options (list, dialog & native).

The UI/UX is in an Apple style and supports iPad, dark mode, & tvOS.

Also you can check the state permissions using the API.

If you like the project, don't forget to

put star ★
and follow me on GitHub:

https://github.com/ivanvorobei

Navigate

Requirements

Ready for use on iOS 11+

Installation

CocoaPods:

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate

SPPermissions
into your Xcode project using CocoaPods, specify it in your
Podfile
:
pod 'SPPermissions/Notification'

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

Camera
,
Location
&
Microphone
you define the following:
pod 'SPPermissions/Camera'
pod 'SPPermissions/Location'
pod 'SPPermissions/Microphone'
Available subspecs

pod 'SPPermissions/Camera'
pod 'SPPermissions/Contacts'
pod 'SPPermissions/Calendar'
pod 'SPPermissions/PhotoLibrary'
pod 'SPPermissions/Notification'
pod 'SPPermissions/Microphone'
pod 'SPPermissions/Reminders'
pod 'SPPermissions/SpeechRecognizer'
pod 'SPPermissions/Location'
pod 'SPPermissions/Motion'
pod 'SPPermissions/MediaLibrary'
pod 'SPPermissions/Bluetooth'
pod 'SPPermissions/Tracking'

Manually

If you prefer not to use any of dependency managers, you can integrate

SPPermissions
into your project manually. Put
Source/SPPermissions
folder in your Xcode project. Make sure to enable
Copy items if needed
and
Create groups
.

After it need add configuration. See example SPPermissionsConfiguration.xcconfig file or example project. If you don't know how add configuration file, see this short video.

Usage

This project had separate modules for the display UI options. The interfaces are:

Dialog
,
List
and
Native
. Each interface has delegates and a datasource. If you want see an example app, open
SPPermissions.xcodeproj
and choose the
Example
target.

Dialog

This is a Modal alert, which was used in the previous version (<5.x). I recomend the use of this alert style when your requested permissions are less than three. Usage example:

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

// 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

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

List

Native

UITableViewController
with support for the iPad. Use it with more than two permissions. An example of how it is used:
let controller = SPPermissions.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

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

Native

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

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

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

// Always use this method for request. // You can pass any controller, this request because need implement base protocol. controller.present(on: self)

Permissions

To check the state of any permission, call enum

SPPermission
:
let state = SPPermission.calendar.isAuthorized

Also available is the func

isDenied
. This returns false if the permission has not been requested before.

DataSource & Delegate

For a customized permssions view, implement

SPPermissionsDataSource
:
func configure(_ cell: SPPermissionTableViewCell, for permission: SPPermission) -> SPPermissionTableViewCell {
    return cell
}

Using a delegate, you can customize texts, colors, and icons. For a default view configure with the default values. After configuration return the cell.

You can customize:

// Titles
cell.permissionTitleLabel.text = "Notifications"
cell.permissionDescriptionLabel.text = "Remind about payment to your bank"
cell.button.allowTitle = "Allow"
cell.button.allowedTitle = "Allowed"

// Colors cell.iconView.color = .systemBlue cell.button.allowedBackgroundColor = .systemBlue cell.button.allowTitleColor = .systemBlue

// If you want set custom image. cell.set(UIImage(named: "IMAGE-NAME")!)

Delegate

In the delegate you can implement these methods:

// Events
func didAllow(permission: SPPermission) {}
func didDenied(permission: SPPermission) {}
func didHide(permissions ids: [Int])

// Denied alert. Show alert if permission denied. func deniedData(for permission: SPPermission) -> SPPermissionDeniedAlertData?

You can detect permission values as follows:

let permissions = ids.map { SPPermission(rawValue: $0) }

Denied alert

If you don't want show an alert if a permission is denied, return

nil
in the delegate. You can set the text in the alert:
func deniedData(for permission: SPPermission) -> SPPermissionDeniedAlertData? {
    if permission == .notification {
        let data = SPPermissionDeniedAlertData()
        data.alertOpenSettingsDeniedPermissionTitle = "Permission denied"
        data.alertOpenSettingsDeniedPermissionDescription = "Please, go to Settings and allow permission."
        data.alertOpenSettingsDeniedPermissionButtonTitle = "Settings"
        data.alertOpenSettingsDeniedPermissionCancelTitle = "Cancel"
        return data
    } else {
        // If returned nil, alert will not show.
        return nil
    }
}

If you don't implement this method, the alert will appear with default text. To disable the alert you just need return

nil
.

Good Practices

I recommend that you show the user all of the permission options, even if some of them are already allowed. But if you want to request only non-allowed permissions, use this code:

let controller = SPPermissions.list([.notification, .reminders].filter { !$0.isAuthorized } )
controller.present(on: self)

A good way to check for the need to show a dialog: check that all permissions are currently authorized by the user:

let permissions = [.notification, .reminders].filter { !$0.isAuthorized }
if permissions.isEmpty {
    // No need show dialog
} else {
    // Show dialog
}

If you request location services, you can show both

.locationWhenInUse
&
.locationAlwaysAndWhenInUse
. If the user allowed
always
mode, they can also change to
when in use
mode:
let controller = SPPermissions.dialog([.locationWhenInUse, .locationAlwaysAndWhenInUse])
controller.present(on: self)

Keys in Info.plist

You need to add some keys to the

Info.plist
file with descriptions. List of keys:
  • NSCameraUsageDescription
  • NSContactsUsageDescription
  • NSCalendarsUsageDescription
  • NSMicrophoneUsageDescription
  • NSAppleMusicUsageDescription
  • NSSpeechRecognitionUsageDescription
  • NSMotionUsageDescription
  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription (iOS 10 and earlier)
  • NSBluetoothAlwaysUsageDescription
  • NSBluetoothPeripheralUsageDescription (iOS 12 and earlier)
  • NSUserTrackingUsageDescription

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

Localization keys

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 in the right side menu and add keys as keys in plist-file. See:
"NSCameraUsageDescription" = "Here description of usage camera";

Design of previous version

I developed

SPPermissions
in an 'Apple-way'. To accomplish this, I checked 30 apps to get UI-elements for this project. I then took screenshots and re-drew the elements in Sketch. For example, the project's
Allow
button is similar to the
Get
button in the AppStore. Check this timelapse to see how I designed the
4.0
version of
SPPermissions
:

Timelaps on YouTube

Other Projects

SPAlert

You can find this alerts in AppStore after feedback or after added song to library in Apple Music. Contains popular Done, Heart presets and many other. Done preset present with draw path animation like original. Also available simple present message without icon. Usage in one line code.

SPPerspective

Animation of widgets from iOS 14. 3D transform with dynamic shadow. Look video preview. Available deep customisation 3D and shadow. Also you can use static transform without animation.

SPDiffable

Simplifies working with animated changes in table and collections. Apple's diffable API required models for each object type. If you want use it in many place, you pass time to implement it and get over duplicates codes. This project help do it elegant with shared models and special cell providers. Support side bar iOS14 and already has native cell providers and views.

SparrowKit

Collection of native Swift extensions to boost your development. Support tvOS and watchOS.

Russian Community

В телеграм-канале Код Воробья пишу о iOS разработке. Помощь можно найти в нашем чате.

Видео-туториалы выклыдываю на YouTube:

Tutorials on YouTube

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.