<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Esat Pllana on Medium]]></title>
        <description><![CDATA[Stories by Esat Pllana on Medium]]></description>
        <link>https://medium.com/@esatpllana?source=rss-abe035a013a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AlcftQowf8zBabWcpmyxTQ@2x.jpeg</url>
            <title>Stories by Esat Pllana on Medium</title>
            <link>https://medium.com/@esatpllana?source=rss-abe035a013a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 13:26:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@esatpllana/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Working with HealthKit sources]]></title>
            <link>https://medium.com/@esatpllana/working-with-healthkit-sources-7f3d15a21dc1?source=rss-abe035a013a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f3d15a21dc1</guid>
            <category><![CDATA[healthkit]]></category>
            <category><![CDATA[working]]></category>
            <category><![CDATA[source]]></category>
            <dc:creator><![CDATA[Esat Pllana]]></dc:creator>
            <pubDate>Fri, 13 Jan 2023 08:20:26 GMT</pubDate>
            <atom:updated>2023-01-13T08:20:26.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BAagbq7E7cESgweN.png" /></figure><h3>Working with HealthKit Sources in iOS Development with Swift</h3><p>Apple’s HealthKit framework provides developers with a powerful set of tools for integrating health and fitness data into their iOS apps. With HealthKit, developers can access and store data from various sources, including the Apple Watch, Fitbit, and other fitness trackers. Additionally, HealthKit can be used to access data from the iPhone’s built-in sensors, such as the accelerometer and gyroscope.</p><p>In this tutorial, we’ll walk through the basics of working with HealthKit sources in iOS development with Swift. We’ll cover how to request authorization to access health data, how to query for data, and how to store data in the HealthKit database. We’ll also provide some sample Swift or SwiftUI source code to help you get started.</p><h4>Requesting Authorization to Access Health Data</h4><p>Before you can access any health data from a HealthKit source, you must first request authorization from the user. To do this, you’ll need to create an instance of the HKHealthStore class and call the requestAuthorization(toShare:read:completion:) method. The requestAuthorization(toShare:read:completion:) method takes three arguments:</p><ul><li>toShare: An array of HKObjectType objects that represent the types of data you want to write to the HealthKit database.</li><li>read: An array of HKObjectType objects that represent the types of data you want to read from the HealthKit database.</li><li>completion: A closure that is called when the authorization request is completed. This closure takes two arguments: a Bool value that indicates whether the authorization request was successful, and an Error object that contains any error messages.</li></ul><p>Here’s an example of how to request authorization to read and write data to the HealthKit database:</p><pre>let healthStore = HKHealthStore()<br><br>let typesToShare: Set&lt;HKSampleType&gt; = [<br>    HKObjectType.workoutType(),<br>    HKObjectType.quantityType(forIdentifier: .stepCount)!<br>]<br><br>let typesToRead: Set&lt;HKObjectType&gt; = [<br>    HKObjectType.quantityType(forIdentifier: .height)!,<br>    HKObjectType.quantityType(forIdentifier: .bodyMassIndex)!<br>]<br><br>healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { (success, error) in<br>    if success {<br>        // Authorization was successful<br>    } else {<br>        // Handle the error here<br>    }<br>}</pre><h4>Querying for Data</h4><p>Once you’ve requested authorization to access health data, you can query the HealthKit database for data using the HKHealthStore class’s execute(_:) method. This method takes an HKQuery object as its argument. To create an HKQuery object, you’ll need to use one of the HKQuery subclasses, such as HKAnchoredObjectQuery, HKStatisticsQuery, or HKWorkoutQuery.</p><p>Here’s an example of how to use the HKStatisticsQuery class to query for the user’s step count over the last seven days:</p><pre>let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())!<br>let endDate = Date()<br><br>let quantityType = HKObjectType.quantityType(forIdentifier: .stepCount)!<br>let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])<br><br>let query = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: [.cumulativeSum]) { (query, result, error) in<br>    guard let result = result, let sum = result.sumQuantity() else {<br>        // Handle the error here<br>        return<br>    }<br><br>    let stepCount = sum.doubleValue(for: HKUnit.count())<br>    print(&quot;Step count: \(stepCount)&quot;)<br>}<br><br>healthStore.execute(query)</pre><h4>Storing Data in the HealthKit Database</h4><p>In addition to querying the HealthKit database for data, you can also store data in the database using the HKHealthStore class’s save(_:withCompletion:) method. This method takes two arguments: an HKObject object that represents the data you want to store, and a closure that is called when the save operation is completed. This closure takes two arguments: a Bool value that indicates whether the save operation was successful, and an Error object that contains any error messages.</p><p>Here’s an example of how to use the save(_:withCompletion:) method to store a workout object in the HealthKit database:</p><pre>let workout = HKWorkout(activityType: .running, start: Date(), end: Date())<br><br>healthStore.save(workout) { (success, error) in<br>    if success {<br>        // The workout was saved successfully<br>    } else {<br>        // Handle the error here<br>    }<br>}</pre><h4>Conclusion</h4><p>In this tutorial, we’ve covered the basics of working with HealthKit sources in iOS development with Swift. We’ve seen how to request authorization to access health data, how to query for data, and how to store data in the HealthKit database. With the knowledge you’ve gained in this tutorial, you should be well on your way to creating powerful and engaging health and fitness apps with HealthKit.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f3d15a21dc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>