OvalMoney / react-native-fitness

A React Native module to interact with Apple Healthkit and Google Fit.
MIT License
343 stars 68 forks source link

requestPermissions always return true [iOS] #50

Closed minhchienwikipedia closed 4 years ago

minhchienwikipedia commented 4 years ago

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 commented 4 years ago

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)
  2. Request permissions, if you can

    if (! hasBeenAsked) {
    await Fitness.requestPermissions(permissions)
    }
  3. 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 commented 4 years ago

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