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

requestPermissions always return true [iOS] #50

Closed
minhchienwikipedia opened this issue Apr 12, 2020 · 2 comments
Closed

requestPermissions always return true [iOS] #50

minhchienwikipedia opened this issue Apr 12, 2020 · 2 comments

Comments

@minhchienwikipedia
Copy link

When I'm tried using requestPermissions on iOS I'm getting issue it's always return true. When the user click Don't Allow it shows up an alert like this and then return true. After that, I tried using function isAuthorized and it always returns true.
Screen Shot 2020-04-12 at 4 20 06 PM

Anyone can help me resolve this issue?

@dmitov
Copy link
Contributor

dmitov commented Apr 12, 2020

TLDR: Unfortunately this is the default behaviour from iOS, check workaround.

Apple restricts basically every permission authorization in a similar way. The first time you call the method you will receive false which implicates "User has not been asked for permissions" and any subsequent call will return true, meaning "User has been asked for permissions".

Basically every iOS app developer (no matter Swift/ObjC or React Native) faces that issue, but it's Apple's way to restrict this popup screen you have on the screenshot.

Workaround

I'm currently using this flow and it's basically what everyone does in a way.

  1. Check if user has been asked for permissions
const hasBeenAsked = await Fitness.isAuthorized(permissions)
  1. Request permissions, if you can
if (! hasBeenAsked) {
  await Fitness.requestPermissions(permissions)
}
  1. Check if you can get some data
const stepsYesterday = await Fitness.getSteps({
  startDate: moment().subtract(1, 'day').format(),
  endDate: moment().format(),
})

If you couldn't get data

if (stepsYesterday.length === 0) {
  ...
}

This implicates some issue, it might be user has no data for that period, or has not shared permissions with your app.

At this point, most apps show a popup alert or a styled modal window instructing the user what to do.

Which is:

  1. Open the Health app
  2. Go to Profile -> Privacy -> Apps -> Your App
  3. Press Turn All Categories On

Side note: For me this doesn't work on a simulator, getting constant spinning loader in Apps screen.

If you could get data

All is good, no need to worry.

Closing thoughts

Dealing with this is a small pain for the developer, but big pain for the user. Unfortunately I didn't find a better workaround and I'm not sure there is another one.

What can I suggest is that you perform that check on every app start and/or state change (when app goes to background and then active again), because the users can disable this the same way you told them to enable it.

Also, you can take a look and inspiration from other fitness apps and how they handle this kind of situation.

Hope this helps!

@minhchienwikipedia
Copy link
Author

@dmitov Thanks for your help! I think that is the only way to check permission on iOS!

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

3 participants