Top Related Projects
Laravel Eloquent roles and abilities.
Associate users with roles and permissions
Role-based Permissions for Laravel 5
Quick Overview
Permission is a Swift library that simplifies the process of requesting permissions in iOS applications. It provides a unified API for handling various system permissions, such as camera, microphone, location, and more, making it easier for developers to manage and request user permissions in their apps.
Pros
- Streamlines the permission request process with a clean and intuitive API
- Supports a wide range of system permissions
- Provides pre-defined permission sets for common use cases
- Offers customizable permission dialogs and alerts
Cons
- Limited to iOS platform only
- May require updates to keep up with changes in iOS permission handling
- Adds an additional dependency to the project
- Some advanced permission scenarios might still require custom implementation
Code Examples
- Requesting camera permission:
Permission.camera.request { status in
switch status {
case .authorized:
print("Camera permission granted")
case .denied:
print("Camera permission denied")
case .notDetermined:
print("Camera permission not determined")
case .notSupported:
print("Camera not supported on this device")
}
}
- Checking location permission status:
if Permission.location.status == .authorized {
// Proceed with location-based features
} else {
// Handle unauthorized access
}
- Requesting multiple permissions:
let permissions: [Permission] = [.camera, .microphone, .contacts]
Permission.request(permissions) { statuses in
for (permission, status) in statuses {
print("\(permission): \(status)")
}
}
Getting Started
- Install Permission using Swift Package Manager by adding the following to your
Package.swift
file:
dependencies: [
.package(url: "https://github.com/delba/Permission", from: "3.0.0")
]
- Import the library in your Swift file:
import Permission
- Use the
Permission
enum to request or check permissions:
Permission.camera.request { status in
if status == .authorized {
// Camera permission granted
}
}
Competitor Comparisons
Laravel Eloquent roles and abilities.
Pros of Bouncer
- More comprehensive and flexible role-based access control system
- Supports defining abilities for specific model instances
- Integrates well with Laravel's authorization system
Cons of Bouncer
- Steeper learning curve due to more complex API
- May be overkill for simpler permission requirements
- Requires more setup and configuration
Code Comparison
Bouncer:
Bouncer::allow('admin')->to('edit', Post::class);
Bouncer::allow('editor')->to('publish', Post::class);
if ($user->can('edit', $post)) {
// User can edit the post
}
Permission:
Permission.contacts.request { status in
switch status {
case .authorized:
// Access granted
case .denied:
// Access denied
}
}
Summary
Bouncer is a more powerful and flexible solution for Laravel applications, offering fine-grained control over permissions and roles. It's ideal for complex authorization scenarios but may be excessive for simpler needs. Permission, on the other hand, is a Swift library focused on iOS permissions, making it more suitable for mobile app development with a simpler API. The choice between the two depends on the specific project requirements and platform.
Associate users with roles and permissions
Pros of Laravel-permission
- Specifically designed for Laravel, offering seamless integration
- Extensive documentation and community support
- Supports caching for improved performance
Cons of Laravel-permission
- Limited to Laravel framework, less flexible for other PHP projects
- More complex setup compared to Permission's simplicity
- Heavier footprint due to additional features
Code Comparison
Permission:
let permission = Permission.contacts
permission.request { status in
switch status {
case .authorized: print("authorized")
case .denied: print("denied")
case .notDetermined: print("not determined")
case .restricted: print("restricted")
}
}
Laravel-permission:
$user->givePermissionTo('edit articles');
// or
$user->assignRole('writer');
$role->givePermissionTo('edit articles');
Key Differences
- Permission is Swift-based for iOS, while Laravel-permission is PHP-based for Laravel
- Permission focuses on iOS system permissions, Laravel-permission handles user roles and permissions within web applications
- Laravel-permission offers more granular control over user access rights
- Permission provides a simpler API for requesting and checking permissions
- Laravel-permission integrates with Laravel's authentication system and database
Both libraries serve different purposes and ecosystems, making direct comparison challenging. Choose based on your project requirements and platform.
Role-based Permissions for Laravel 5
Pros of Entrust
- More comprehensive role-based access control system
- Supports database-driven permissions and roles
- Includes artisan commands for easy management
Cons of Entrust
- More complex setup and configuration
- Heavier resource usage due to database interactions
- Less frequently updated (last update in 2019)
Code Comparison
Entrust:
$user->attachRole($admin);
$user->can('edit-post');
Permission:
Permission.camera.request { authorized in
print(authorized)
}
Summary
Entrust is a more robust solution for Laravel applications requiring complex role-based access control. It offers database-driven permissions and roles, making it suitable for large-scale applications with dynamic permission requirements.
Permission, on the other hand, is a lightweight Swift library focused on handling iOS system permissions. It provides a simple and intuitive API for requesting and checking various device permissions.
While Entrust excels in web application scenarios, Permission is tailored for iOS app development. The choice between the two depends on the specific platform and requirements of your project.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Permission exposes a unified API to request permissions on iOS.
Usage ⢠Example ⢠Installation ⢠License
Usage
Permission
let permission: Permission = .contacts
print(permission.status) // .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
Bluetooth
Camera
Contacts
Events
Motion
Microphone
Notifications
Photos
Reminders
LocationAlways
LocationWhenInUse
MediaLibrary
SpeechRecognizer
Siri
PermissionAlert
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:
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 for more informations.
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
Use a PermissionSet
to check the status of a group of Permission
and to react when a permission is requested.
let permissionSet = PermissionSet(.contacts, .camera, .microphone, .photos)
permissionSet.delegate = self
print(permissionSet.status) // .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
A PermissionButton
requests the permission when tapped and updates itself when its underlying permission status changes.
let button = PermissionButton(.photos)
PermissionButton
is a subclass of UIButton
. All the getters and setters of UIButton
have their equivalent in PermissionButton
.
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 RxSwift bindings for Permissions API in iOS.
Example
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(describing: 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)
}
}
Installation
Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Permission into your Xcode project using Carthage, specify it in your Cartfile
:
github "delba/Permission"
Configuration
Due to Apple's new policy regarding permission access, binaries may be rejected due to a perceived attempt to access privacy-sensitive data without a usage key, and then further rejected for not actually requesting permissions.
As a workaround, you can provide custom build flags before building the dynamic framework to only compile
with permissions you request. This is done by adding a configuration file named PermissionConfiguration.xcconfig
to the root of your project. For convenience, you can use
PermissionConfiguration.xcconfig
in the Permission/
repo directory. Just comment out the permissions
you want to use, and compile the framework.
To compile with only notifications and photos permissions:
PERMISSION_BLUETOOTH = // PERMISSION_BLUETOOTH
PERMISSION_CAMERA = PERMISSION_CAMERA
PERMISSION_CONTACTS = // PERMISSION_CONTACTS
PERMISSION_EVENTS = // PERMISSION_EVENTS
PERMISSION_LOCATION = // PERMISSION_LOCATION
PERMISSION_MICROPHONE = // PERMISSION_MICROPHONE
PERMISSION_MOTION = // PERMISSION_MOTION
PERMISSION_NOTIFICATIONS = PERMISSION_NOTIFICATIONS
PERMISSION_PHOTOS = // PERMISSION_PHOTOS
PERMISSION_REMINDERS = // PERMISSION_REMINDERS
PERMISSION_SPEECH_RECOGNIZER = // PERMISSION_SPEECH_RECOGNIZER
PERMISSION_MEDIA_LIBRARY = // PERMISSION_MEDIA_LIBRARY
// Do not modify this line. Instead, remove comments above as needed to enable the categories your app uses.
PERMISSION_FLAGS= $(PERMISSION_BLUETOOTH) $(PERMISSION_CAMERA) $(PERMISSION_CONTACTS) $(PERMISSION_EVENTS) $(PERMISSION_LOCATION) $(PERMISSION_MICROPHONE) $(PERMISSION_MOTION) $(PERMISSION_NOTIFICATIONS) $(PERMISSION_PHOTOS) $(PERMISSION_REMINDERS) $(PERMISSION_SPEECH_RECOGNIZER) $(PERMISSION_MEDIA_LIBRARY)
SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) $(PERMISSION_FLAGS)
Cocoapods
CocoaPods is a dependency manager for Cocoa projects.
You can install it with the following command:
$ gem install cocoapods
To integrate Permission into your Xcode project using CocoaPods, specify it in your Podfile
. 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 the Camera and the Notifications you define the following:
use_frameworks!
pod 'Permission/Camera'
pod 'Permission/Notifications'
Please see Permission.podspec
for more information about which subspecs are available.
License
Copyright (c) 2015-2019 Damien (http://delba.io)
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.
Top Related Projects
Laravel Eloquent roles and abilities.
Associate users with roles and permissions
Role-based Permissions for Laravel 5
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot