Integration Of Pedometer In Swift IOS Development

Integration of Pedometer in Swift iOS Development

by — 4 years ago in Development 3 min. read
3616

Pedometer Introduction

Pedometeris one of the iOS feature which can measure, track, calculate distance. It can calculate and count the steps for walking, staircase, heart rate calculation, orientation, rotation, and altitude of the person.

Pedometer measures mainly all accelerometer and gyroscope. Which helps to identify many things. Core motion is core library that is used to be used to calculate various properties of accelerometer and gyroscope.

This library is designed in such a way that it will be fully supported by apple hardware because whatever calculated by code it depends on hardware also. Both hardware and software communicate with each other provide the appropriate data to the users.

Pedometer Framework iOS

  • Core Motion
  • Health kit

Use of Library

  • Walking, Moving, and running distance calculation.
  • Heartbeat tracking.
  • Counting steps per day.
  • Data can be kept secure for 6-7 days.
  • Historical data can be maintained for various activities.
  • Finding and Calculate the rotation altitude and acceleration of the device when users are moving from one place to another
  • Motion detection
  • Direction calculation and finding the compass

Perquisite for Pedometer integration

  • Framework added in the build settings Properly
  • Plist file needs to configure properly for settings

Integration of Pedometer in Swift iOS Development 1

  • Important things for Pedometer

CMPedometer:- It will capture the data with an inbuilt motion sensor in the device and shared in the software system.

CMPedometerData:- It is supported by a hardware device that detects the step counting overrunning, walking and moving. Provide data from hardware to software. It validates and captures the moving data from hardware in iOS devices.

CMPedometerEvent:-  iOS device monitors the distance moved by users step

CMStepCounter:-  iOS has the capability to count the steps taken by user’s but it is possible only when user carry the device with

Code Steps to implementation

1. Step count will be done in the view will appear where calculation will be done with time and date

 override funcviewWillAppear(_ animated: Bool) {  
 super.viewWillAppear(animated)  
     guard let startDate = startDate else { return }  
 pedometerStepsCountUpdate(startDate: startDate)  
   }  

2. Pedometer will start updating the data the step count and motion count

   @objcPublicfuncdidTapStartButton() {  
 pedometerStartUpdating = !pedometerStartUpdating  
 pedometerStartUpdating ? (pedometerStart()) : (pedometerStop())  
   }  

3. Authorization needs to provide from app side where user will get popup message to confirm the

 PublicfuncpedometerStart() {  
 pedometerAuthorizationState()  
 pedometerUpdate()  
   }  

4. Pedometer can be stopped and start from the app side to reset the settings from the app.

 PublicfuncpedometerStop() {  
 stopUpdating()  
   }  

5. Pedometer can be updated from the app side where activity indicator will be running over the code

 PublicfuncpedometerUpdate() {  
     if CMMotionActivityManager.isActivityAvailable() {  
 startTrackingActivityType()  
     } else {  
 activityTypeLabel.text = "Pedometer available"  
     }  
     if CMPedometer.isStepCountingAvailable() {  
 startCountingSteps()  
     } else {  
 stepsCountLabel.text = "Pedometer available"  
     }  
   }  

6. Pedometer authorization is denied or approved from the app side where pedometer can be authorized.

 PublicfuncpedometerAuthorizationState() {  
     switch CMMotionActivityManager.authorizationStatus() {  
     case CMAuthorizationStatus.denied:  
 pedometerStop()  
 activityTypeLabel.text = "Pedometer available"  
 stepsCountLabel.text = "Pedometer available"  
 default:break  
     }  
   }  

7. Pedometer stop updating the step count and reset the event activity.

 PublicfuncstopUpdating() {  
 activityManager.stopActivityUpdates()  
 pedometer.stopUpdates()  
 pedometer.stopEventUpdates()  
   }  

8. Handle error to find the pedometer activity.

Publicfunc on(error: Error) {

//handle error

}

9. Pedometer Steps Count can be Updated with hardware to software so that it will reflect the count in the UI.

 PublicfuncpedometerStepsCountUpdate(startDate: Date) {  
 pedometer.queryPedometerData(from: startDate, to: Date()) {  
       [weak self] pedometerData, error in  
       if let error = error {  
 self?.on(error: error)  
       } else if let pedometerData = pedometerData {  
 DispatchQueue.main.async {  
           self?.stepsCountLabel.text = String(describing: pedometerData.numberOfSteps)  
         }  
       }  
     }  
   }  

10. Pedometer tracking activity calculated with moving, walking, running and Stationary by step count.

 PublicfuncstartTrackingActivityType() {  
 activityManager.startActivityUpdates(to: OperationQueue.main) {  
       [weak self] (activity: CMMotionActivity?) in  
       guard let activity = activity else { return }  
 DispatchQueue.main.async {  
         if activity.walking {  
           self?.activityTypeLabel.text = "Pedometer Walking"  
         } else if activity.stationary {  
           self?.activityTypeLabel.text = "Pedometer Stationary"  
         } else if activity.running {  
           self?.activityTypeLabel.text = "Pedometer Running"  
         } else if activity.automotive {  
           self?.activityTypeLabel.text = "Pedometer Automotive"  
         }  
       }  
     }  
   }  

11. Pedometer step can be calculated and counting with the help android application development services.

 PublicfuncstartCountingSteps() {  
 pedometer.startUpdates(from: Date()) {  
       [weak self] pedometerData, error in  
       guard let pedometerData = pedometerData, error == nil else { return }  
 DispatchQueue.main.async {  
         self?.stepsCountLabel.text = pedometerData.numberOfSteps.stringValue  
       }  
     }  
   }  

At Netmobility with our experienced and updated android application development services, enterprise mobility management services, we create smarter and robust mobile apps which bring out your business charactersitics in an attractive way for its customers.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Copyright © 2018 – The Next Tech. All Rights Reserved.