Getting CMPedometer data in background

801 views Asked by At

I just want to build a step counting app for iphones. My application runs correctly when in foreground and save the step data to the firebase but when user terminate the app, the step counting stops so I dug the internet to solve the problem and could not figure it out. Can you guys help me that keep counting the steps in the background and write them to firebase in the background?

import UIKit
import CoreMotion
import FirebaseAuth
import FirebaseDatabase
class MainViewController: UIViewController {

//MARK: - Constants and Variables
let pedometer = CMPedometer()
let userDefault = UserDefaults.standard
var ref : DatabaseReference!
let user = Auth.auth().currentUser
var someInt: Int?

//MARK: - IBOutlets and IBActions
@IBOutlet weak var stepCountLabel: UILabel!

@IBAction func logOutBtn(_ sender: UIButton) {
    logout()
}

//MARK: - viewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()
    stepCountLabel.text = "0"
    getFirebaseData()
    
}


//MARK: - Functions
func getFirebaseData() {
    ref = Database.database().reference()
    let uid = user!.uid
    ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
        
        // Get user value
        let value = snapshot.value as? [String : Any]
        let userDict = [
            "nickname" : value?["nickname"],
            "step_count" : value?["step_count"]
        ]
        
        
        DispatchQueue.main.async {
            self.someInt = userDict["step_count"] as? Int
            self.startPedometer(number: self.someInt ?? 0)
            self.stepCountLabel.text = String(self.someInt ?? 0)
        }
    })
    {
        (error) in
        print(error.localizedDescription)
    }
}

func startPedometer (number: Int) {
    pedometer.startUpdates(from: Date()) { (data, error) in
        if error == nil {
            self.getFirebaseData()
            //create data reference
            self.ref = Database.database().reference()
            //rewrite the step data on the child
            self.ref.child("users/ (self.user!.uid)/step_count").setValue(data!.numberOfSteps.intValue + number)
        } else{
            print("An error was occured: \(error!)")
        }
    }
}



func logout() {
    pedometer.stopUpdates()
    do {
        try Auth.auth().signOut()
        userDefault.setValue(false, forKey: "userSignedIn")
    }
    catch {
        print("already logged out")
    }
    navigationController?.popToRootViewController(animated: true)
  }
}
0

There are 0 answers