1

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!

Community
  • 1
  • 1
AWSSET
  • 397
  • 1
  • 4
  • 12
  • 2
    You don't actually ask anyone for the users permission it is the Apple code that asks for the permission. The only way to get it to ask multiple times is if the user goes to the settings and turns it off themselves and even then I'm not sure about whether it will ask them again. – Popeye Sep 08 '14 at 07:22
  • This link will be helpfull to you http://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/ – rptwsthi Sep 08 '14 at 07:24
  • @Popeye Application will ask again if the user has turn off the bluetooth from the settings. Whether the user wants to turn on the bluetooth or not? – Arpit Sep 08 '14 at 07:26
  • 1
    @Arpit Haven't got a device so could find out whether that would happen or not so thank you for clarifying. As for the rest it still stands that it is the Apple code that asks for permission not the developers code and I get a feeling there requirement is to ask every time the user needs Bluetooth or GPS location whether it turned on for the application or not. – Popeye Sep 08 '14 at 07:30
  • 1
    @Popeye Ya, this thing cannot be handle from developer end. – Arpit Sep 08 '14 at 07:39
  • @Popeye I see, so Apple code automatically does the asking... but what will happen if for example the GPS was turned off by the user but the permission to use the application is already granted? Do we implement something like this [LINK HERE](http://stackoverflow.com/questions/21676835/objective-c-check-if-bluetooth-availability-changed)? – AWSSET Sep 08 '14 at 07:47

2 Answers2

2

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::

Rog
  • 17,070
  • 9
  • 50
  • 73
  • be aware that reforwarding the user to bluetooth permissions when pressing "yes" by using open url :root will result in apple reject your app – João Serra Jan 02 '19 at 10:41
0

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98