sparrowcode / PermissionsKit

Universal API for request permission and get its statuses.
https://x.com/sparrowcode_ios
MIT License
5.64k stars 462 forks source link

Add a permission request hook to allow permission request pre-processing #164

Closed nivanchikov closed 4 years ago

nivanchikov commented 4 years ago

This pull request adds a new SPPermissionsDelegate method which is called before the framework starts processing the permission request. This gives the API consumers a chance to manually attempt to request necessary permissions, e.g try to start a Bluetooth scan to force the system to display the permissions dialog. If the developer doesn't want to do any request pre-processing, they could just call the completion closure

Example:

func willAccess(permission: SPPermission, completion: @escaping () -> Void) {
   switch permission {
     case .bluetooth:
         BluetoothManager.requestBluetoothPermissions(completion)
    default:
        completion()
    }
}
ivanvorobei commented 4 years ago

Good day! Thanks for your work! You can describe when need this case?

nivanchikov commented 4 years ago

sure, we use SPPermissions in one of our projects and specifically needed the Bluetooth permission.

Since the actual permissions request wasn't available in the latest release (5.2.6) (see #134) we implemented this callback to trigger the Bluetooth scan on our end, which brings up the system permissions dialog. After that, the framework successfully validates the permission on its end.

So, in short, this pull request enables the user to do some work before the framework processes the permission

ivanvorobei commented 4 years ago

Maybe better add Bluetooth request to source code? For now it no implement, because I don't know how correct it request.

nivanchikov commented 4 years ago

I'm not sure it's quite correct to add the Bluetooth permissions to the framework itself, cause it involves creating a CBCentralManager object, starting the scan and waiting for the power state callback. We haven't found another way to trigger the permissions alert.

Would you really want to include such setup into the framework itself?

ivanvorobei commented 4 years ago

Need implement only request permission, you can do it?

nivanchikov commented 4 years ago

Again - the system won't show the permission dialog unless the app creates a CBCentralManager (and perhaps starts the scan). This can be implemented similar to SPRequestPermissionLocationHandler or the same feature can be provided as a pre-permission hook in the current solution.

If you want to encapsulate the permissions request handling inside the framework, we can try creating something similar to SPRequestPermissionLocationHandler.

ivanvorobei commented 4 years ago

Yes, I mean it. You can, please?

ivanvorobei commented 4 years ago

@nivanchikov any news?

bielikb commented 4 years ago

There are several ways how to invoke the bluetooth permissions, as @nivanchikov correctly pointed out, it's tied to creation of an instance of central, peripheral or any other manager that accesses the bluetooth services on device.

One of the possible code sample looks like this:

CBPeripheralManager(
            delegate: self,
            queue: nil,
            options: [ CBPeripheralManagerOptionShowPowerAlertKey: true ]
        )

it'd be clever to give a user of the SPPermissions API a way to provide the implementation that should be executed, e.g. when authorising bluetooth permissions. This way, SPPermissions library would be flexible and wouldn't be tied to one specific solution.