PermissionsKit
The convenient wrapper on macOS permissions API.
Current implementation supports permissions for:
Usage
Available types:
typedef NS_ENUM(NSUInteger, MPPermissionType) {
MPPermissionTypeCalendar = 0,
MPPermissionTypeReminders,
MPPermissionTypeContacts,
MPPermissionTypePhotos,
MPPermissionTypeFullDiskAccess
} NS_SWIFT_NAME(PermissionType);
Get permissions status:
/**
* Requests current authorization status for a given type.
*
* @discussion It is safe to call this method on system where permission type is not supported. MPAuthorizationStatusAuthorized will be returned.
*
* @param permissionType MPPermissionType
* @return MPAuthorizationStatus
*/
+ (MPAuthorizationStatus)authorizationStatusForPermissionType:(MPPermissionType)permissionType;
Ask for permissions:
/**
* Requests user authorization for a given permission. Completion will be invoked with a user decision. Completion queue is undefined.
* @discussion There will be no completion when requesting MPPermissionTypeFullDiskAccess, because its status is unknown. You should implement your own callback mechanism, for example - polling authorization. It is safe to call this method on system where permission type is not supported. MPAuthorizationStatusAuthorized will be returned.
*
* @param permissionType MPPermissionType
* @param completionHandler void (^)(MPAuthorizationStatus status)
*/
+ (void)requestAuthorizationForPermissionType:(MPPermissionType)permissionType withCompletion:(void (^)(MPAuthorizationStatus status))completionHandler;
Calendar
10.9+
NSCalendarsUsageDescription
key in info.plist is required.
Contacts
10.11+
NSContactsUsageDescription
key is required in Info.plist
Reminders
10.9+
NSRemindersUsageDescription
key is required in Info.plist
Photos
10.13+
NSPhotoLibraryUsageDescription
key is required in Info.plist
Full Disk Access
10.14+
Since there is no legal API to request Full Disk Access permissions on macOS 10.14, this is the only workaround to get and ask for it.
NSApp
foreground/background status).
Calling for permissions opens Preferences->Privacy with selected "Full Disk Access" section.
Application Sandbox:
PermissionsKit can be used in sandboxed applications. It uses multiple files to check for FDA status in case some of the tested files are unaccessible due to reasons unrelated to FDA (not present on older systems, UNIX permissions, etc.) The files are:
~/Library/Safari/Bookmarks.plist
~/Library/Safari/CloudTabs.db
/Library/Application Support/com.apple.TCC/TCC.db
/Library/Preferences/com.apple.TimeMachine.plist
Your app needs to be able to access those files under sandbox. You can do it using Security-Scoped Bookmarks flow(e.g. having a bookmark for root folder), more details in apple documentation Or for testing purposes you can add temporary-exception to your .entitlements file.
<key>com.apple.security.temporary-exception.files.home-relative-path.read-only</key>
<array>
<string>Library/Safari/Bookmarks.plist</string>
<string>Library/Safari/CloudTabs.db</string>
</array>
<key>com.apple.security.temporary-exception.files.absolute-path.read-only</key>
<array>
<string>/Library/Application Support/com.apple.TCC/TCC.db</string>
<string>/Library/Preferences/com.apple.TimeMachine.plist</string>
</array>
For more details please check Full Disk Access details.
Installation
CocoaPods
PermissionsKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'PermissionsKit-macOS'
Carthage
PermissionsKit is available through Carthage. To install it, simple add the following line to your Cartfile:
github "MacPaw/PermissionsKit"
Manual
For manual installation just choose the latest available Release and drag and drop the framework to you project. (You may also need to add it to Embedded Binaries).
Requirements
macOS 10.9+
However different kinds of permissions require different system version it is safe to call for authorization witout actual system check. MPAuthorizationStatusAuthorized
will be returned in this case.
Example
See PermissionsKitTestApp target
Useful links
License
MIT License
Copyright (c) 2018 MacPaw
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.