3

I've been working on a cross-platform app built with Expo and I've been having trouble with the Pedometer in iOS. It works fine on android, but on iOS while it does ask for permissions it fails to retrieve values with a promise rejection.

I've tried going back over the docs to see if there's been changes I need to implement. The docs do indeed seem to have changed to import from expo-sensors instead of expo. I tried this, it does not change the promise rejection on iOS. Additionally, using this import give an error for android that using dates has not been implemented yet. I checked the expo-sensors docs and the standalone expo-sensors has been deprecated and is part of the main branch, though the expo docs say to use this import now. In addition, you get the same errors on the code examples in the docs on their emulator but there's no notes about iOS being broken that I can find.

https://docs.expo.io/versions/latest/sdk/pedometer/

The import syntax I'm using:

import {Pedometer} from 'expo';

And how I'm using the Pedometer:

Pedometer.isAvailableAsync().then(
          result => {
            this.setState({
              pedometerAvailability: String(result)
            });
          },
          error => {
            this.setState({
              pedometerAvailability: "No pedometer available: " + error
            });
          }
        );

        //start and end times for the day
        //Take date by the the diary date
        let endDay = new Date(Moment(diaryDate).toDate().valueOf());
        let startDay = new Date(Moment(diaryDate).toDate().valueOf());
        let today = new Date();
        //are we checking today? If not end day should be set as the end of the day
        if (today.getDay() !== endDay.getDay()){
            this.setTime(endDay, 23, 59, 59);
        }

        //setting time for startDay
        this.setTime(startDay, 0, 0, 0);

        //making call
        Pedometer.getStepCountAsync(startDay, endDay).then(
            result => {
                this.setState({
                    stepCount:result.steps
                });
            },
            error => {
                this.setState({
                    stepCount:"Could not get stepCount:" + error
                });
            }
        );
    };

    setTime = (date, h, m, s) =>{
        date.setHours(h);
        date.setMinutes(m);
        date.setSeconds(s);
    };

iOS should behave like Android according to the docs, but maybe I'm going wrong somewhere?

AkhinLinks
  • 51
  • 5
  • you can run `expo install expo-sensors` – hong developer Jul 09 '19 at 14:15
  • You can and I did. However importing from expo-sensors doesn't fix the problem on iOS and breaks Android. Additionally, the expo-sensors docs say it has been deprecated in favour of expo/expo: https://github.com/expo/expo-sensors – AkhinLinks Jul 09 '19 at 14:24
  • Note: to access DeviceMotion stats on iOS, the NSMotionUsageDescription key must be present in your Info.plist. – hong developer Jul 09 '19 at 14:28
  • import { Pedometer } from 'expo-sensors'; – hong developer Jul 09 '19 at 14:29
  • I'm afraid that isn't the problem. This isn't an ejected app, it's still an Expo app and expo-sensors comes preinstalled. The only Info.plist files are in my node modules so that isn't applicable here I'm afraid. The problem is that niether the imports in the Expo docs currently or the syntax that they gave a few months ago when I first started using the pedometer work for iOS and the currently suggested syntax seems to work for neither iOS or Android. – AkhinLinks Jul 09 '19 at 14:46

0 Answers0