Is there a way/plugin to check for iOS permissions?

2,293 views
Skip to first unread message

Kaan Soral

unread,
Apr 26, 2014, 4:11:39 PM4/26/14
to phon...@googlegroups.com
If this was a plugin, I think it would be a major/heavily used one, but there is no such plugin.

Only this thread: https://groups.google.com/forum/#!searchin/phonegap/permission/phonegap/Fbe7Nx94nrI/2CZZu8yyXhsJ (the guy says "I guess I'll have to write my first PhoneGap plugin :)" -- I feel the same thing)

Am I correct - or is there a way or plugin to check for iOS permissions / permission states (denied / not asked etc.)

Thanks in Advance

jcesarmobile

unread,
Apr 26, 2014, 6:50:04 PM4/26/14
to phon...@googlegroups.com
I do native iOS programming and you don't usually check the permissions directly, you can, but you don't do it, you just try to use something that need permissions and then if it fails you check the reason. I haven't checked all the cordova code, but I think they do it the way I told you, if the error callback is fired you have the error code

Kaan Soral

unread,
Apr 26, 2014, 8:55:44 PM4/26/14
to phon...@googlegroups.com
Thanks for the information

For example for photos, that routine may yield good results, you take a photo with the native cordova plugin, it asks for the photos permission, the user would understand and probably accept with a high probability, since s/he just snapped a photo explicitly

But for the third party push plugin for example, a crafted request routine seems to be a must, the user will probably deny the request if it just pops up

It seems to be a good idea to manually make sure each permission exists, or if it doesn't, asks for the user to manually activate the permission

It's hard to develop the flow of these requests (requesting the permission, or manual addition) without knowing the state of the permission

For example I have no idea what happens when the photo permission gets denied after the camera or file plugin asks for the permission, I will check it soon
(It would be a great idea to detect if the photos permission was denied before and ask the user to manually add the permission before triggering the photo routine to be failed again)

My .02, I will probably inspect some of the existing cordova plugins (to easily adopt the plugin format) and objective C routines to check permissions at stackoverflow etc. and morph them together into a custom plugin, shouldn't be too hard it seems, it would be great if it was already done tho :)

Kaan Soral

unread,
Apr 28, 2014, 12:16:09 PM4/28/14
to phon...@googlegroups.com
I've researched cordova's plugin structure and the ios permissions, It seems the permission system is messy regardless of cordova/native.
(And slightly off topic, ios plugin system seems to be pretty simple, android's is a bit messy - android/java seems messier than objective-c/ios, it was great to finally start developing plugins)
Just depending on the response of the calls to the routines that require permission and assume the permission is missing if the call doesn't go through might cover all the cases.
However it's obviously much better and much safer to just check for the permission, as the response from the plugins are unknown until you test them, the delay is unknown, all of these cases and the delays have to be factored in to develop a complete permission logic

Anyway, long story short, here is the function to check for "push"/"location" permissions, it's not hard to wrap it up as a plugin, if anyone is interested I might share a wrapped version (the easiest thing to do is to get any plugin and refactor plugin.xml/.h/.m files to do what you want)

- (void)check_ios_permissions:(CDVInvokedUrlCommand*)command
{
   
       
NSString *callbackId = command.callbackId;
       
NSString *push=@"no";
       
NSString *location=@"no";
       
CDVPluginResult* pluginResult;

     
@ try {
       
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
       
if (types & UIRemoteNotificationTypeAlert) push=@"yes";

       
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) location=@"denied";
       
else if(![CLLocationManager locationServicesEnabled]) location=@"disabled";
       
else location=@"yes";

       
NSDictionary *jsonObj = [ [NSDictionary alloc]
                               initWithObjectsAndKeys
:
                                 location
, @"location",
                                 push
, @"push",
                                 
nil
                           
];

        pluginResult
= [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary : jsonObj];

   
} @catch (NSException * e) {

       pluginResult
= [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[e reason]];

   
} @finally {

       
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
   
}
   
}


jcesarmobile

unread,
Apr 28, 2014, 4:47:39 PM4/28/14
to phon...@googlegroups.com
These are the possible location permissions:

typedef enum {
kCLAuthorizationStatusNotDetermined = 0,
kCLAuthorizationStatusRestricted,
kCLAuthorizationStatusDenied,
kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;


You haven't considered all of them

Kaan Soral

unread,
Apr 28, 2014, 5:05:01 PM4/28/14
to phon...@googlegroups.com
Thanks a lot for the heads up

Honestly I have no Objective-C experience left, read/applied the Deitel&Deitel book 2-3 years ago and forgot it all after years, so I didn't dive into things/documentation myself

The code is gathered from Stack Overflow answers, and interestingly those permission states weren't mentioned in those answers, you could get a lot of upvotes :)

I hoped for a central access to all permission states, but it seems each library handles the permissions separately, otherwise it would be great to wrap all permission states into a plugin, however as it is, it seems a bit complicated as the plugin will have to include all libraries/frameworks, I'm unsure what kind of overhead that would create, that's why I didn't pursue
Reply all
Reply to author
Forward
0 new messages