AWSSET
AWSSET

Reputation: 417

IOS - How to ask users for permissions?

Our current application we're developing requires the current location of the user and Bluetooth for the beacon. And we would like to ask the user for permission to use the GPS and Bluetooth (separately). We plan to ask the user's permission everytime he/she turns on the feature in the settings page of our application. Is there a way to do this?

So far I found this link: I want to trigger iOS7 to ask users permission to use Bluetooth and Twitter account

But it only ask the user once for the application's lifetime.

PS: we are also new to objective-c a detailed explanation would be much appreciated.

Thank you in advance!

Upvotes: 1

Views: 9587

Answers (2)

Rog
Rog

Reputation: 17170

You will have to construct your own dialog to ask for this permission if you want to ask every time.

By default the Apple triggered dialogs appear once (or twice in iOS8) when you first ask for access to the Location APIs. To have iOS ask for permission you need to do a couple of things (in iOS8):

add NSLocationAlwaysUsageDescription or requestWhenInUseAuthorization to your info.plist these keys hold a string which iOS uses i the dialog it presents when you preform the next step:

send requestAlwaysAuthorization or requestWhenInUseAuthorization to an instance of CLLocationManager. i.e.

CLLocationManager* myLocationManager = [[CLLocationManager alloc] init];
[myLocationManager requestAlwaysAuthorization];

At this point iOS will display the dialogs you are familiar with. iOS8 will also display another dialog to confirm this some time after the initial permission (about two days). It will not ask repeatedly.

if you want to construct your own dialog, you should do this:

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Use bluetooth"
                                                                   message:@"is it ok for this app to use Bluetooth" 
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action) {
                                                          // This code runs when the user presses YES.
                                                          // You should also add code to handle a NO button.
                                                      }];
    [alert addAction:yesButton];

You then need to display the alert. Probably using presentViewController::

Upvotes: 2

gnasher729
gnasher729

Reputation: 52592

There are certain ways how all applications handle that kind of permission.

Basically, your app asks iOS to do something, iOS decides whether this needs user permission, and asks the user for permission if needed. The user may refuse in which case your iOS call gets a suitable error. The user may have refused the last time the app was launched or may have turned off permission in preferences, in which case your call fails without UI. The user can turn on permission in preferences, or may have given permission the first time, and your call will succeed without any UI.

That's how everyone works. You can put up some alert or dialog to get a user's permission, but that won't get you anywhere. You can't get permission from the user without going through iOS itself. Even if you could, you wouldn't want to use a UI different from everyone else's. And even if you did, it would mean your app would very likely be rejected from the store.

Note: The Bluetooth permission notification that you linked to can be useful. Of course you shouldn't implement the alert at the end of the example code, that's only to demonstrate the code is working.

Upvotes: 0

Related Questions