Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: Event on success of requestAlwaysAuthorization? #99

Open
arcbus opened this issue Oct 14, 2015 · 3 comments
Open

Q: Event on success of requestAlwaysAuthorization? #99

arcbus opened this issue Oct 14, 2015 · 3 comments

Comments

@arcbus
Copy link

arcbus commented Oct 14, 2015

It does take and call success/failure callbacks, but appears to fire success simply on the display of the prompt. Whereas I need a callback on accepting of the permission to always monitor beacons.

Problem i'm having is that on first launch of the app, i'm executing the startMonitoringForRegion before the permission has been accepted by user, and then i get:

"Monitor error: The operation couldn’t be completed. (kCLErrorDomain error 4.)"

Whereas i'd like to wait for user acceptance before starting the monitor.

Is there some other way to capture when user has granted monitoring permission?

@arcbus
Copy link
Author

arcbus commented Oct 14, 2015

Below is my code around requesting authorization. Seems no matter what, i always get onAuth (success callback) fired as soon as requestAlwaysAuthorization is called. Whether i allow or don't allow, and on subsequent launches when permission doesn't exist.

Would be helpful if it called failure/success according to users selection, and if it waited till the user made a selection. Just a suggestion.

var onNoAuth = function() {
    console.log('user didnt authorize, cant monitor beacons');
  };

  var onAuth = function() {
    console.log('user did authorize, start monitoring beacons');
    estimote.beacons.startMonitoringForRegion(
            {}, 
            app.onMonitor,
            app.onError);
  };

estimote.beacons.requestAlwaysAuthorization(
    onAuth,
    onNoAuth
);

@arcbus
Copy link
Author

arcbus commented Oct 14, 2015

Well, answered my own question. Hopefully it helps someone else.

The success call back from requestAlwaysAuthorization merely says the check for auth succeeded, not that auth was granted. It takes a result, which is an int to indicate state of authentication, so if we see it hasn't been granted yet, we just keep checking after a delay. This repeats until we know if granted or not.

var onAuth = function(result) {
                var msg = "";

                // result == null for not yet asked, i think 0 means it was declined
                if ( result == 0  || result == null){
                    msg = "Requesting permission";
                    setTimeout(function() {
                        // check again after delay
                        estimote.beacons.authorizationStatus(
                            onAuth,
                            onNoAuth
                        );
                    }, 2000);
                }

                // result == 2 for never authorized
                if ( result == 2 )
                    msg = "Enable Location services for maximum joy!";

                // result == 3 for always (assuming 1 means while app is running)
                if ( result == 3 || result == 1){
                    msg = "Looking for beacons!";

                    // Start monitoring.
                    estimote.beacons.startMonitoringForRegion(
                        {},
                        app.onMonitor,
                        app.onError);
                    }

                var element = $("<div>"+msg+"</div>");
                $('.fixedheader').append(element);  
            };

@McGern
Copy link

McGern commented Nov 19, 2015

This confused the hell out of me too. Thanks for the post.

I am having an issue where startMonitoringForRegion will cause the app to crash if permission is not allowed, so will need to work around this using your solution.

Cheers,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants