Skip to content

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

License

Notifications You must be signed in to change notification settings

jminutaglio/SPPermissions

 
 

Repository files navigation

SPPermissions

About

API to ask permissions on Swift. Available list, dialog & native. I do UI in Apple style, support iPad, dark mode & tvOS. Also you can check state permissions.

Visit my store for iOS developers:

xcode-shop.com

If you like the project, do not forget to put star ★ and follow me on GitHub:

https://github.com/ivanvorobei

For help project, see Сooperation section or our chat.

Pay attention

Now I migrate SPPermissions to new 5.0 vesion. If you found any bugs or need old functionality - please, create issue or write me. If you want install old version, add this to your Podfile:

pod 'SPPermissions', '4.1.4'

I recomended install new version and create issue. I resolve all issues in 24-48 hours.

Navigate

Requirements

Swift 4.2 & 5.0. 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'

Swift Package Manager

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

To integrate SPPermissions into your Xcode project using Xcode 11, specify it in File > Swift Packages > Add:

https://github.com/ivanvorobei/SPPermissions

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

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SPPermissions into your Xcode project using Carthage, specify it in your Cartfile:

github "ivanvorobei/SPPermissions"

By default available all permissions. You can provide custom build flags before building the dynamic framework to only compile with permissions you request. Open file SPPermissionsConfiguration.xcconfig in Source/SPPermissions/Supporting Files, comment unusable permissions and rebuild:

carthage build

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

Project separates to modules. For now available Dialog, List and Native interface. Each interface has delegates and datasource. If you want see example app, open SPPermissions.xcodeproj and choose Example target.

Dialog

Modal alert, which you can see in previous version. I recomend use it mode when permissions less than three. Usage will be simple:

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 iPad. Use it with more two permissions. How it use:

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. 
// Datasource never call, ignore it.
controller.delegate = self

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

Permissions

For check state of any permission, call enum SPPermission:

let state = SPPermission.calendar.isAuthorized

Also available func isDenied. This return false, if permission not requested before.

DataSource & Delegate

You have one method for pass data for each permission. If you return nil, using default parametrs.

func data(for permission: SPPermission) -> SPPermissionData? {
    return nil
}

If you don't want show alert if permission denied, set showAlertOpenSettingsWhenPermissionDenied to false:

let notificationData = SPPermissionData(name: "Notification", description: "Remind about new orders for your account.", image: nil, allowTitle: "Allow", allowedTitle: "Allowed")
notificationData.showAlertOpenSettingsWhenPermissionDenied = false

If you want show this alert, need configure texts:

notificationData.alertOpenSettingsDeniedPermissionTitle = "Notifiaction denied"
notificationData.alertOpenSettingsDeniedPermissionDescription = "Please, go to Settings and allow permission."
notificationData.alertOpenSettingsDeniedPermissionButtonTitle = "Settings"
notificationData.alertOpenSettingsDeniedPermissionCancelTitle = "Cancel"

In delegate you can implement three methods:

func didAllow(permission: SPPermission) {}
func didDenied(permission: SPPermission) {}
func didHide() {}

Good Practices

I recommend show the all list of permissions, even if some of them are allowed. But if you want to request only none-allowed permissions, use this code:

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

A good way to check or need to show a dialog, check or all permissions are received:

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

If you request locations, you can show twice .locationWhenInUse & .locationAlwaysAndWhenInUse. If user allowed always mode, also change 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 description. List of keys:

  • NSCameraUsageDescription
  • NSContactsUsageDescription
  • NSCalendarsUsageDescription
  • NSMicrophoneUsageDescription
  • NSAppleMusicUsageDescription
  • NSSpeechRecognitionUsageDescription
  • NSMotionUsageDescription
  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription (iOS 10 and earlier)

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 localization file manually, you need create InfoPlist.strings, select languages in right side menu and add keys as keys in plist-file. See:

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

Сooperation

The development of this project is completely free. If you can make a contribution, it will help development. Here list of task what need do:

  • Fix mistakes in this Readme and docs. My English so bad. Good create pull request.
  • Add docs in source files. Description of public methods and parametrs.
  • Subtitles for video in any your native languages, where I tell you how to connect a configuration file.
  • Help me translate my app Debts - Spending tracker for other languages.
  • Add icon for tv os example target.

Design previous version

I develop SPPermissions in Apple-way. For this, I check 30 apps to get UI-elements for it project. I am take screenshoot and draw it in Sketch. For example, Allow button is similar to Get button in the AppStore. Check timelapse to see how I am design 4.0 version of SPPermissions:

Timelaps on YouTube

License

SPPermissions is released under the MIT license. Check LICENSE for details.

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Swift 97.8%
  • Ruby 1.4%
  • Objective-C 0.8%