0

I'm trying to create a healthcare application and am using Core Motion's CMPedometer to obtain live step count. However, I wanted the step count to be uploaded in real time to Firebase, as well as to return this data stored in Firebase to display the Total Number of Steps the user has walked in the history of using the Healthcare App.

I've already connected Firebase to my app using Cocoapods, but unlike data that is manually created and uploaded to a database - like text messages, it seems complicated to upload the live step count obtained from the CMPedometer class.

import UIKit
import CoreMotion
import Firebase

class FirstViewController: UIViewController {

    @IBOutlet weak var stepLabel: UILabel!

    var myPedometer = CMPedometer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.


        myPedometer = CMPedometer()

        myPedometer.startUpdates(from: NSDate() as Date, withHandler: { (pedometerData, error) in
            if let e = error {
                print(e.localizedDescription)
                return
            }
            guard let data = pedometerData else {
                return
            }
            let myStep = data.numberOfSteps
            self.stepLabel.text = "\(myStep)"
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    It's a little unclear what is meant by 'in real time'. In other words, how and when does the data get from the pedometer to the app. Does it send an event whenever a new step happens or does it send a set of data every X seconds or do you have to poll the device with the app to get that data. Or is it initiated by the user in some way? Can you clarify the question? Oh - and welcome to SO! – Jay May 29 '19 at 20:07
  • Thank you for your comment! When motion is detected of the user, the CMPedometer displays a step count, which gets updated when the user takes more steps. I was looking for a way in which this updated step count can be uploaded to Firebase every time the user takes more steps. – onlypedometers May 30 '19 at 06:39
  • Ok. That part of the question is clear. What is not clear is mentioned in my prior comment; how does the data get from the pedometer to the app? – Jay May 30 '19 at 13:54
  • Importing CoreMotion helps activate the CMPedometer, which then listens for any movement, and when it does, it records it as steps. – onlypedometers Jun 08 '19 at 08:24
  • So you get the step information in data, and you want to store that in Firebase. Something simple as `myFirebaseNode.setValue(data.numberOfSteps.intValue)' right after the `let myStep = data.numberOfSteps` line would record the data in Firebase. What specifically is the issue? Can you include the code that isn't working? As you can see, asking a complete question with all of the data points will help us to help you. – Jay Jun 08 '19 at 12:43

0 Answers0