{"id":24812,"date":"2020-07-26T16:14:03","date_gmt":"2020-07-26T10:44:03","guid":{"rendered":"https:\/\/www.the-next-tech.com\/?p=24812"},"modified":"2021-01-15T14:52:10","modified_gmt":"2021-01-15T09:22:10","slug":"integration-of-pedometer-in-swift-ios-development","status":"publish","type":"post","link":"https:\/\/www.the-next-tech.com\/development\/integration-of-pedometer-in-swift-ios-development\/","title":{"rendered":"Integration of Pedometer in Swift iOS Development"},"content":{"rendered":"<h3>Pedometer Introduction<\/h3>\n<p>Pedometeris one of the<a href=\"https:\/\/www.the-next-tech.com\/development\/apple-unveils-ios-14-and-macos-big-sur-features-for-india-china-and-other-international-markets\/\"> iOS feature which can measure<\/a>, track, calculate distance. It can calculate and count the steps for walking, staircase, heart rate calculation, orientation, rotation, and altitude of the person.<\/p>\n<p>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.<\/p>\n<p>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 <a href=\"https:\/\/www.the-next-tech.com\/business\/customer-relationships-the-benefits-of-using-crm-software-for-startups\/\">software communicate<\/a> with each other provide the appropriate data to the users.<\/p>\n<h3>Pedometer Framework iOS<\/h3>\n<ul>\n<li>Core Motion<\/li>\n<li>Health kit<\/li>\n<\/ul>\n<p><strong><u>Use of Library<\/u><\/strong><\/p>\n<ul>\n<li>Walking, Moving, and running distance calculation.<\/li>\n<li>Heartbeat tracking.<\/li>\n<li>Counting steps per day.<\/li>\n<li>Data can be kept secure for 6-7 days.<\/li>\n<li>Historical data can be maintained for various activities.<\/li>\n<li>Finding and Calculate the rotation altitude and acceleration of the device when users are moving from one place to another<\/li>\n<li>Motion detection<\/li>\n<li>Direction calculation and finding the compass<\/li>\n<\/ul>\n<h2><strong>Perquisite for Pedometer integration<\/strong><\/h2>\n<ul>\n<li>Framework added in the build settings Properly<\/li>\n<li>Plist file needs to configure properly for settings<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-24813 size-full\" src=\"https:\/\/s3.amazonaws.com\/static.the-next-tech.com\/wp-content\/uploads\/2020\/07\/19152553\/pedometer.png\" alt=\"\" width=\"626\" height=\"156\" srcset=\"https:\/\/s3.amazonaws.com\/static.the-next-tech.com\/wp-content\/uploads\/2020\/07\/19152553\/pedometer.png 626w, https:\/\/s3.amazonaws.com\/static.the-next-tech.com\/wp-content\/uploads\/2020\/07\/19152553\/pedometer-300x75.png 300w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/p>\n<ul>\n<li><strong><u>Important things for Pedometer<\/u><\/strong><\/li>\n<\/ul>\n<p>CMPedometer:- It will capture the data with an inbuilt motion sensor in the device and shared in the software system.<\/p>\n<p>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.<\/p>\n<p>CMPedometerEvent:-\u00a0 iOS device monitors the distance moved by users step<\/p>\n<p>CMStepCounter:-\u00a0 iOS has the capability to count the steps taken by user\u2019s but it is possible only when user carry the device with<\/p>\n<h3>Code Steps to implementation<\/h3>\n<p>1. Step count will be done in the view will appear where\u00a0calculation will be done with time and date<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> override funcviewWillAppear(_ animated: Bool) {  \r\n super.viewWillAppear(animated)  \r\n     guard let startDate = startDate else { return }  \r\n pedometerStepsCountUpdate(startDate: startDate)  \r\n   }  \r\n<\/code><\/pre>\n<p>2. Pedometer will start updating the data the step count and motion count<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\">   @objcPublicfuncdidTapStartButton() {  \r\n pedometerStartUpdating = !pedometerStartUpdating  \r\n pedometerStartUpdating ? (pedometerStart()) : (pedometerStop())  \r\n   }  \r\n<\/code><\/pre>\n<p>3. Authorization needs to provide from app side where user will get popup message to confirm the<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncpedometerStart() {  \r\n pedometerAuthorizationState()  \r\n pedometerUpdate()  \r\n   }  \r\n<\/code><\/pre>\n<p>4. Pedometer can be stopped and start from the app side to reset the settings from the app.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncpedometerStop() {  \r\n stopUpdating()  \r\n   }  \r\n<\/code><\/pre>\n<p>5. Pedometer can be updated from the app side where activity indicator will be running over the code<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncpedometerUpdate() {  \r\n     if CMMotionActivityManager.isActivityAvailable() {  \r\n startTrackingActivityType()  \r\n     } else {  \r\n activityTypeLabel.text = \"Pedometer available\"  \r\n     }  \r\n     if CMPedometer.isStepCountingAvailable() {  \r\n startCountingSteps()  \r\n     } else {  \r\n stepsCountLabel.text = \"Pedometer available\"  \r\n     }  \r\n   }  \r\n<\/code><\/pre>\n<p>6. Pedometer authorization is denied or approved from the app side where pedometer can be authorized.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncpedometerAuthorizationState() {  \r\n     switch CMMotionActivityManager.authorizationStatus() {  \r\n     case CMAuthorizationStatus.denied:  \r\n pedometerStop()  \r\n activityTypeLabel.text = \"Pedometer available\"  \r\n stepsCountLabel.text = \"Pedometer available\"  \r\n default:break  \r\n     }  \r\n   }  \r\n<\/code><\/pre>\n<p>7. Pedometer stop updating the step count and reset the event activity.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncstopUpdating() {  \r\n activityManager.stopActivityUpdates()  \r\n pedometer.stopUpdates()  \r\n pedometer.stopEventUpdates()  \r\n   }  \r\n<\/code><\/pre>\n<p>8. Handle error to find the pedometer activity.<\/p>\n<p>Publicfunc on(error: Error) {<\/p>\n<p>\/\/handle error<\/p>\n<p>}<\/p>\n<p>9. Pedometer Steps Count can be Updated with hardware to software so that it will reflect the count in the UI.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncpedometerStepsCountUpdate(startDate: Date) {  \r\n pedometer.queryPedometerData(from: startDate, to: Date()) {  \r\n       [weak self] pedometerData, error in  \r\n       if let error = error {  \r\n self?.on(error: error)  \r\n       } else if let pedometerData = pedometerData {  \r\n DispatchQueue.main.async {  \r\n           self?.stepsCountLabel.text = String(describing: pedometerData.numberOfSteps)  \r\n         }  \r\n       }  \r\n     }  \r\n   }  \r\n<\/code><\/pre>\n<p>10. Pedometer tracking activity calculated with moving, walking, running and Stationary by step count.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncstartTrackingActivityType() {  \r\n activityManager.startActivityUpdates(to: OperationQueue.main) {  \r\n       [weak self] (activity: CMMotionActivity?) in  \r\n       guard let activity = activity else { return }  \r\n DispatchQueue.main.async {  \r\n         if activity.walking {  \r\n           self?.activityTypeLabel.text = \"Pedometer Walking\"  \r\n         } else if activity.stationary {  \r\n           self?.activityTypeLabel.text = \"Pedometer Stationary\"  \r\n         } else if activity.running {  \r\n           self?.activityTypeLabel.text = \"Pedometer Running\"  \r\n         } else if activity.automotive {  \r\n           self?.activityTypeLabel.text = \"Pedometer Automotive\"  \r\n         }  \r\n       }  \r\n     }  \r\n   }  \r\n<\/code><\/pre>\n<p>11. Pedometer step can be calculated and counting with the help android application development services.<\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000;\"> PublicfuncstartCountingSteps() {  \r\n pedometer.startUpdates(from: Date()) {  \r\n       [weak self] pedometerData, error in  \r\n       guard let pedometerData = pedometerData, error == nil else { return }  \r\n DispatchQueue.main.async {  \r\n         self?.stepsCountLabel.text = pedometerData.numberOfSteps.stringValue  \r\n       }  \r\n     }  \r\n   }  \r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pedometer Introduction Pedometeris one of the iOS feature which can measure, track, calculate distance. It can calculate and count the<\/p>\n","protected":false},"author":1280,"featured_media":24880,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[133],"tags":[2284,3265],"_links":{"self":[{"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/posts\/24812"}],"collection":[{"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/users\/1280"}],"replies":[{"embeddable":true,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/comments?post=24812"}],"version-history":[{"count":6,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/posts\/24812\/revisions"}],"predecessor-version":[{"id":24883,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/posts\/24812\/revisions\/24883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/media\/24880"}],"wp:attachment":[{"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/media?parent=24812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/categories?post=24812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.the-next-tech.com\/rest\/wp\/v2\/tags?post=24812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}