<?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 Dan Beech on Medium]]></title>
        <description><![CDATA[Stories by Dan Beech on Medium]]></description>
        <link>https://medium.com/@danbeech?source=rss-2318678bf990------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AfeHG-gpAFVYFDuBW2wvTA@2x.jpeg</url>
            <title>Stories by Dan Beech on Medium</title>
            <link>https://medium.com/@danbeech?source=rss-2318678bf990------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:17:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danbeech/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[How I Saved Time On Activity Classification Model Data Collection]]></title>
            <link>https://danbeech.medium.com/save-time-on-ml-data-collection-by-being-a-musician-df56366a5c1a?source=rss-2318678bf990------2</link>
            <guid isPermaLink="false">https://medium.com/p/df56366a5c1a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-collection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dan Beech]]></dc:creator>
            <pubDate>Thu, 18 Feb 2021 13:02:57 GMT</pubDate>
            <atom:updated>2021-02-19T17:33:12.051Z</atom:updated>
            <content:encoded><![CDATA[<p>So you want to make an activity classification model? If you want to streamline the data collection process then read on for a fun way to cut out time that I learned during the journey to creating mine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ACfcUPSXLpjlhWH7" /><figcaption>Photo by <a href="https://unsplash.com/@unsplash?utm_source=medium&amp;utm_medium=referral">Unsplash</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I used CreateML for this, the software is great and easy to use, but data collection can take <em>forever</em>. This method of data collection will apply to any use of time series motion data collection, so feel free to read if you aren’t using the same software.<br>When I first started the data collection process my plan was to use os_log to output the data from my Apple Watch, process that data, and I’d be done. Easy!<br>It turns out I needed a <strong><em>much </em></strong>larger set of data than I imagined, and to rub salt in the wound, the pandemic hit and due to an underlying health condition I was confined to my house. <br>That meant that I couldn’t pass off my Apple Watch to some friends, have them complete samples for me and sit back and relax. I was going to have to do all of them myself; and I really didn’t want to spend weeks spinning my arm around in circles. Y<em>eah, I know the results will be skewed because the model will be trained to recognise </em><strong><em>my specific movements</em></strong><em>; </em>but desperate times call for desperate measures and with deadlines slowly but steadily creeping up on me; these were indeed <em>desperate</em> times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*poZYr85HTv0AE_k6" /><figcaption>Photo by <a href="https://unsplash.com/@anckor?utm_source=medium&amp;utm_medium=referral">Julian O&#39;hayon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The New Plan</h3><p>I needed a way of getting many samples in a short time, so I put on my Watch and analysed where most of my time was lost during the collection.<br>Most of my time was spent stopping and starting the data collection between samples. So I decided to try getting a load of samples in a row to cut that down. <br>This was a step in the right direction, but it just <strong><em>wasn’t</em> <em>good enough</em></strong>. Samples were either completed too fast, or too slow and so I increased my processing time. It meant that I was still losing time ensuring that all the samples fit correctly into the prediction windows in CreateML.</p><p>That won’t do. I needed to refine the idea.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*msF3aNe34tdzS40r" /><figcaption>Photo by <a href="https://unsplash.com/@randomisgood?utm_source=medium&amp;utm_medium=referral">Shawn Sim</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So I went back to the drawing board; I liked the idea of collecting large sample sets in one session, but wanted to minimise the processing of the data to cut out as much time as possible.<br>Now anybody who knows me knows that I’m a musician, and I’ve spent<strong><em> way too much time</em></strong> using a metronome over the years. <br>Metronomes are a drummers best friend (<strong><em>sometimes</em></strong>) and when you’re recording music you get well acquainted with them. For those who don’t know what a metronome is; it clicks at a constant BPM (<strong>b</strong>eats <strong>p</strong>er <strong>m</strong>inute) and there’s one built into Google for you to use.</p><p>So I was going to use a metronome to keep my speed at a constant rate, but <strong><em>what about the processing time</em></strong>?</p><p>I searched the App Store for an app I could use on my Watch that would provide me with samples in CSV form, and where I could set my own sample rate.</p><p><a href="http://sensorlog.berndthomas.net/"><strong>SensorLog</strong></a> to the rescue!</p><p>This meant that I could collect the data at the sample rate that I wanted to use, and I could then use the samples per second (Hz) and the beats per minute of the metronome to work out how many rows of my CSV output would correspond to one sample, while using the clicks of the metronome to make sure that I hit my cues for starting and completing an action <em>every time</em>.</p><p>So for example if my action takes 1 second to complete, I can set my metronome to 60 BPM (<em>or 120 BPM if you want more intermediary clicks</em>), 60 BPM would equal one click per second. I set my sample rate to whatever Hz I want to feed into CreateML. <br>I put the metronome in 4/4, and count along with the clicks like this;</p><p>1… 2... 3... 4... 1... 2… etc.</p><p>I start the action on each 1 I count with the click of the metronome, and aim to finish it in time with when I count to 2, then start an action when I count 3 clicks, and finish that action when I count the 4th and final click. <br>So basically; start your action on odd numbers, and end it in time with the even numbers.</p><p>That means 30 samples per minute each minute you collect data!</p><p>You will need to change this to suit your samples and the time it takes to complete them, but it will allow you to keep your samples uniform in collection which means minimal processing!<br>If, like me, you’re collecting the samples only from yourself; try to vary how you do the action. It’s tough to avoid falling into a rhythm with the metronome clicking away but try to do some actions more pronounced, some more subtle. Do some that complete slightly faster than others, some slightly slower. This will aid the accuracy of your model during training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I_c0ryMt46QbfY2p" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=medium&amp;utm_medium=referral">Luke Chesser</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Using this method I went from collecting single samples of each action, to collecting multiple samples and processing them to ensure they were all correct… To dropping in CSV files that needed <strong><em>next to</em></strong> <strong><em>no processing</em></strong> whatsoever.<br>I would set a timer, put on the metronome, start up <a href="http://sensorlog.berndthomas.net/">SensorLog</a>, and spend 10 minutes spinning my arm around in a particular action. When you consider that each action was roughly 1 second to complete; that’s a lot of samples gathered <strong><em>much </em></strong>quicker!</p><p>If you trim off any excess rows that occurred before your first sample (why not give yourself a 4 beat count in like a band?), and any rows that occurred between your last sample and you pressing to stop collection; CreateML will chop up the CSV into individual samples using the prediction window; and because they are so uniform it’s very unlikely that there will be any samples that don’t fit perfectly into those windows. To get the prediction windows to line up nicely took a couple of attempts of trial and error but once you know your settings fit your samples- you’re good to go! <br>I used a prediction window of 100, with a sample rate of 50Hz. This was the best fit for my data and provided the best training results.</p><p>The cherry on top is that you can Airdrop the CSV files straight onto your Mac from <a href="http://sensorlog.berndthomas.net/">SensorLog</a>, saving yourself even more time. This is quite a novel way of speeding up the data collection and it <strong><em>almost </em></strong>made my data collection fun.</p><p>A final word of warning: if your actions require you to hold your arm straight out in front of you at a 90 degree angle to complete them, you may end up with one super strong (or painful) shoulder and one normal one. So take regular breaks!<br>Thanks for reading! Good luck with your apps! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df56366a5c1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Python with Node.js]]></title>
            <link>https://python.plainenglish.io/use-python-with-node-js-a5111ccddd04?source=rss-2318678bf990------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5111ccddd04</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dan Beech]]></dc:creator>
            <pubDate>Wed, 17 Feb 2021 13:21:05 GMT</pubDate>
            <atom:updated>2021-02-19T22:55:57.953Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes you find yourself in a situation where Python would be a better option than Node for getting a job done.</p><p>I’ve encountered this while creating desktop apps with Electron.js that interact with microcontrollers, and I’ve created web APIs to interact with existing Python scripts. Regardless of your scenario, it’s a useful tool to have in your arsenal, so here’s how I usually do it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vl78B9YaE0Tklwbw" /><figcaption>Photo by <a href="https://unsplash.com/@maxcodes?utm_source=medium&amp;utm_medium=referral">Maxwell Nelson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article we will create a simple program using Node.js and Python. The JS will call the Python, get the output of the script and return it to JS.</p><p>First let’s create a simple Python script that has a single argument, we will print out whatever value is provided with that argument, and then print out some generic messages afterwards:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c6e77767f917edaaa4a86032043547f/href">https://medium.com/media/1c6e77767f917edaaa4a86032043547f/href</a></iframe><p>As you can see we have the argument ‘<strong><em> — hello</em></strong>’ and we print the value of this argument on line 8. From line 10 onwards we’re just printing some generic strings out. We add <strong><em>flush=True</em></strong> to the prints, and a sleep to ensure that the output isn’t buffered. If you go ahead and remove the ‘<strong><em>flush=True</em></strong>’ from the first print, you will see that the output for the first and second prints are returned together. The same happens if you remove the sleep. I usually use a sleep of 0.05, but I’ve exaggerated it here.</p><p>Buffered output can be useful in some cases, but most of the time I’ve wanted my output separated so that I can use each separate print to trigger actions in my Node.js programs. There are other ways (inside Python, or in the spawn of the child process) to make the output unbuffered, but for this article we’re using the most obvious way of doing it.</p><p><strong>A note for Electron.js users:</strong> <em>Whenever I’ve used a Python child process inside an Electron app I’ve had to add a small sleep (0.05) after each print in the Python script or the output will be buffered, whether you tell it to be unbuffered or not. If this is the case you will often receive prints mixed together with others and it can mess with the functionality of your apps. So add your sleeps!</em></p><p>We can test that the script works by running it with: <br>python python.py --hello “Hello world!”</p><p>And we will get the output (with the delays we added):<br>Hello world!<br> I am talking from Python<br> I really like Python<br> But JS is cool too</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*88OWHcBrHDLTT7JB" /><figcaption>Photo by <a href="https://unsplash.com/@wocintechchat?utm_source=medium&amp;utm_medium=referral">Christina @ wocintechchat.com</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Great! The Python script is working, so now how do we call it from <em>inside </em>Node.js?</p><p>Create a file called <strong><em>main.js</em></strong>, and inside it add the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e9ead7d9d3ea360c29607ff2e9b3ec9/href">https://medium.com/media/4e9ead7d9d3ea360c29607ff2e9b3ec9/href</a></iframe><p>First we require ‘<strong><em>child_process</em></strong>’, this is part of Node.js that allows you to create, or to use another, more relevant word; <strong><em>spawn</em></strong>,<strong><em> </em></strong>a subprocess.</p><p>Before we spawn our child process, however, we want to push our arguments to it.</p><p>On line 3 we make a new array called <strong><em>args</em></strong>, and initialise it with one element; a string containing <strong>the name of the Python script</strong> <strong>we want to run</strong>, in this case that is ‘<strong><em>python.py</em></strong>’.</p><p>Next we push our script arguments to the array; so we push ‘ <strong><em>— hello</em></strong>’, and it’s value ‘<strong><em>Hello World</em></strong>’.</p><p>Line 6 is where we actually spawn the subprocess that runs the Python script. I’ve named it <strong><em>childProcess </em></strong>because it makes the rest of the code more readable and easy to understand. When we spawn the subprocess, we are giving it two values:</p><ul><li>You will remember that we executed the script on it’s own by using python python.py --hello “Hello world!”. <br>Here we are giving <strong><em>the cmd that is required to run the script in a terminal </em></strong>as the first argument; in this case ‘<strong><em>python</em></strong>’. This could change to ‘<strong><em>python3</em></strong>’ depending on your system.</li><li>Next we are providing the <strong><em>args</em></strong> array that we created.</li></ul><p>Now that the subprocess is spawned with it’s arguments, we need to handle its output so that it can be returned to Node.</p><p>On line 8 we set the encoding for <strong><em>childProcess.stdout</em></strong>. This will be where the output of the Python script is returned to.</p><p>Line 9 is where we handle that output. If we read the words of this line backwards you can almost make a readable sentence out of it: <strong><em>data — on — stdout — childProcess</em></strong>.</p><p>Let’s fill in some gaps and we have a sentence that says exactly what it does: The <strong>data </strong>received <strong>on </strong>the <strong>stdout </strong>of the <strong>childProcess</strong>.</p><p>I’ve interpolated the data into a string so that when we run the child process script we will see the difference; that it is now <strong>printing from inside Node</strong>, and not Python.</p><p>On lines 13–16 we do the same, but this code handles the errors that may occur in the Python script. We can then handle those differently in Node so our programs are responsive and provide accurate user feedback.</p><p>Lastly, line 18–20 handles what happens when the Python script has finished its execution. Here we just print that it has finished.</p><p>Let’s test out the program! We can now run our Node.js code with:</p><p>node main.js</p><p>And we will see a different output in the terminal:</p><p>Node output: Hello World</p><p>Node output: I am talking from Python</p><p>Node output: I really like Python</p><p>Node output: But JS is cool too</p><p>Your Python and Node code should now be connected. You can also use the stdin to send information from Node to Python but I haven’t used that too much. You can read more into child processes <a href="https://nodejs.org/api/child_process.html"><strong>here</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2a_CUmKw_1s4aNd0" /><figcaption>Photo by <a href="https://unsplash.com/@dulgier?utm_source=medium&amp;utm_medium=referral">Nastya Dulhiier</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4><strong><em>Final tips</em></strong></h4><p>You can add markers to your Python prints and check for them when you handle them in your Node code. A habit I picked up from a friend of mine was to add [-] to the front of your prints for messages that won’t trigger actions and should just print to the console, and [+] for actions that will trigger actions. This allows easy string checking and makes it easier to debug and read your code when you need to maintain it.</p><p>If you need to compile the python script to a .exe to bundle it with your software then you can do so easily with <a href="https://pypi.org/project/auto-py-to-exe/"><strong>auto-py-to-exe</strong></a>. Usually I do this when including Python child processes in Electron.js apps.</p><p>That’s a very basic implementation, but this functionality allows you to do very powerful things if you use it correctly. Try it out and let me know what you created! Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5111ccddd04" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/use-python-with-node-js-a5111ccddd04">How to use Python with Node.js</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Looping with a delay in Swift]]></title>
            <link>https://danbeech.medium.com/looping-with-a-delay-in-swift-7f55a5dba510?source=rss-2318678bf990------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f55a5dba510</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Dan Beech]]></dc:creator>
            <pubDate>Sat, 13 Feb 2021 00:25:39 GMT</pubDate>
            <atom:updated>2021-02-13T00:25:39.205Z</atom:updated>
            <content:encoded><![CDATA[<p>Coming from JavaScript and Python, creating a delay in Swift was odd. Here’s how to get your delays working.</p><p>Before I figured this out I was trying GCD (Grand Central Dispatch) as that’s what everybody suggests on most popular forums, but whenever I tried using GCD I couldn’t find a solution that worked for <em>this</em> scenario. <br>I’m sure a more experienced Swift programmer will tell me there’s a way, but I’m here to provide a working solution that fit my needs… And hopefully helps somebody else out too!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eGI8h7azWydr_Gn2" /><figcaption>Photo by <a href="https://unsplash.com/@fabiofistarol?utm_source=medium&amp;utm_medium=referral">Fabio Fistarol</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>My scenario was this:</p><ul><li>Make an API call to get some data</li><li>Provide reminder at set time intervals until stopped by the user or the data is not available</li></ul><p>I started with making an API call on every iteration and using GCD to execute that code using asyncAfter on a background queue. This worked fine until I remembered that currently I was the only person submitting API requests, and that if more users were doing the same I would <strong><em>quickly</em></strong> reach my rate limit allowance.<br>So a new solution was needed, and knowing that the data I was collecting wasn’t likely to change within 30–60 seconds; I had some wiggle room to loop and provide the same feedback to the user, while reducing my API calls by a factor of 3. Nobody would even know!</p><p>The scenario became:</p><ul><li>Make an API call</li><li>Provide reminders for multiple loops of the timer without any API call</li><li>Repeat until stopped by user or data isn’t available</li></ul><p>This is where GCD stopped working for me.<br>I tried a for loop with asyncAfter, but that won’t work because asyncAfter returns immediately meaning that all of the iterations are fired almost immediately and then return almost at the exact same time. Not what we need!<br>I tried different GCD methods. I even tried sleep() even though I knew it was going to block my UI updates.<br>Everything I tried either fired all iterations at the same time, or blocked the main thread and left the UI un-usable…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fv4f8v2y5wDNgFtx" /><figcaption>Photo by <a href="https://unsplash.com/@neonbrand?utm_source=medium&amp;utm_medium=referral">NeONBRAND</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Timer to the rescue!<br>I’ve explained my woes enough, so how do we fix it?.. <strong>Timer</strong>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0245fe9ccbbc124123fcfdc51819239e/href">https://medium.com/media/0245fe9ccbbc124123fcfdc51819239e/href</a></iframe><p>This Timer will loop every <strong><em>10 seconds</em></strong>, and will increment the variable <strong><em>count</em></strong> when it does.<br>If count equals 2, then we fire off an API call, and then <strong>invalidate the Timer</strong>. I’ve bolded invalidate the Timer because it’s important. It will keep looping until you invalidate it and you can end up with multiple Timers if you call the entry to the loop again in your code.</p><p>So how does this work on an endless (but interrupt-able) loop? We can do something like this:</p><ul><li>When the view appears, call the function that contains the API call.</li><li>If the API call is successful; call the function containing the Timer and do whatever else you need to do with the data.</li><li>Let the Timer loop until it meets it’s condition providing feedback on each loop as if there has been another API call, <strong>invalidate it</strong>, and then call the function with the API call again.</li></ul><p>After this I added the ability to pause the Haptic feedback with a long press gesture on the UI. To do that you can create a boolean, e.g. <strong><em>shouldStopAPICalls </em></strong>and toggle it on the gesture, wrap the block of code the Timer executes with an if-else, if <strong><em>shouldStopAPICalls</em></strong> is false then execute the loop, else- invalidate the Timer.<br>You’ll need to call the function with the API call to start up the loop again, but you will have a working loop (with delays if you added that part) that can be paused without blocking your main thread!</p><p>Thanks for reading, good luck with your apps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f55a5dba510" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get your standalone HealthKit Watch app through App Store Connect & Review]]></title>
            <link>https://danbeech.medium.com/get-your-standalone-healthkit-watch-app-through-app-store-connect-review-767278c211a3?source=rss-2318678bf990------2</link>
            <guid isPermaLink="false">https://medium.com/p/767278c211a3</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[app-store]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apple-watch]]></category>
            <dc:creator><![CDATA[Dan Beech]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 18:07:20 GMT</pubDate>
            <atom:updated>2021-02-17T16:21:09.813Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oUugbEDhC3eKtM-G" /><figcaption>Photo by <a href="https://unsplash.com/@karsten116?utm_source=medium&amp;utm_medium=referral">Karsten Winegeart</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently I published a standalone Watch app to the App Store. I designed the app, I coded the functionality, I added the relevant HealthKit permissions to the info.plist, and I followed the steps to send the build over to App Store Connect for review. Job done 👏 … Or so I thought. <br>Xcode kicked the app back to me with upload errors telling me that I hadn’t requested the permissions I had already requested 🤔<br>This one was a bit confusing so I thought I’d write my first Medium article to let everybody else know how I got past it and got my app published!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZWZPwap0U_fsPHlm" /><figcaption>Photo by <a href="https://unsplash.com/@bugsster?utm_source=medium&amp;utm_medium=referral">Taras Shypka</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I’m going to start from the beginning for those who haven’t started their implementation yet, but feel free to skip ahead to get what you need.</p><p>First of all, you need to <strong>initialise</strong> your HealthStore.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/514a6063214f1ad5f08c2b2124c15114/href">https://medium.com/media/514a6063214f1ad5f08c2b2124c15114/href</a></iframe><p>And then you will need to <strong>request authorisation</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d052fb5236cfdf8b879b35283e5b9ec0/href">https://medium.com/media/d052fb5236cfdf8b879b35283e5b9ec0/href</a></iframe><p>After the initial set up is done you will want to start your heart rate query and then process the values. In my process function I have saved the result of the processing to a variable called heartRateBPMValue as an int to use in the UI of my project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be26d01f6b4dc52caf0eab4f234d25fa/href">https://medium.com/media/be26d01f6b4dc52caf0eab4f234d25fa/href</a></iframe><p>Finally to display the heart rate to the user you can do something like the following code in SwiftUI.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be395b1f093a126cbdde5cc7049ab7dc/href">https://medium.com/media/be395b1f093a126cbdde5cc7049ab7dc/href</a></iframe><p>This code isn’t meant to be a full working project, it’s just some functions I’ve pasted from my own project that will help you on your way to setting up and using HealthKit and heart rate monitoring. The Apple documentation has much more information about other types of workouts and what you can monitor, so go and check those out to make sure you fully understand it.</p><p>Now that the code is done and your project is working, what permissions do you need to request? For the above example, using heart rate monitoring, you would need to add two entries to your WatchKit Extension info.plist:</p><ul><li><strong>NSHealthShareUsageDescription</strong></li><li><strong>NSHealthUpdateUsageDescription</strong></li></ul><p>You should provide a string for these values explaining why you need to access this information and let the user know what you will use the information for, e.g. “The app needs access to your Health data to track heart rate during workouts”. This is the message that will be displayed to users when the app first asks them for permission to access their data, so ensure that the users know what you are doing or Apple may reject your app and ask for a more descriptive message.</p><p>You should also head into your ‘App_Name WatchKit Extension.entitlements’ file and add HealthKit to the entitlements, and add any HealthKit Capabilities that you need.</p><p>So now that the code is done, the app works, it requests the permissions when you run it on a new device or simulator, and it tracks the heart rate properly. It’s time to send the app off to the App Store? <br>Nope, not yet! It will still be rejected at this stage, so let’s fix that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vy9XkiFK06JfBPHe" /><figcaption>Photo by <a href="https://unsplash.com/@rev3n?utm_source=medium&amp;utm_medium=referral">Michał Kubalczyk</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Create a new group at the same level in the folder structure as your Watchkit Extension. Name this the name of your app. Now we need to create a new info.plist inside this folder. Don’t delete your other files, we still need those to actually ask for permissions on the Watch app, this new file is to satisfy the App Store review process because standalone Watch apps are submitted as iOS apps and something in the review process means that the permissions on your WatchKit Extension won’t be enough to satisfy the Review process.</p><p>Remember those permissions we asked for earlier? You will need to add those to this info.plist too, <strong>as well as the original</strong>. You can copy and paste the same strings you used in the original file.<br>After you’ve added those permissions you should <strong>uncheck all the targets</strong> in the ‘Target Membership’ in the Utilities (right-hand side menu in Xcode).<br>Then click on the ‘.xcodeproj’ and select the target that says the name of your app in the targets list (not the WatchKit App, or WatchKit Extension). Go to the Build Settings and search for ‘info.plist’ for the entry named ‘<strong>Info.plist File</strong>’ you should enter the location of the file we added. If you put it in a folder with your app name as I mentioned earlier, you should enter ‘<strong>AppName/Info.plist</strong>’.</p><p>Now you can build your app and send it to App Store Connect and it <strong><em>should</em></strong> make it through the review process without flagging up those issues about permissions! 👏👏</p><p>If you are submitting the app for review make sure that your app description declares that your app integrates with the Apple Health application and why it does that, or they will reject it based on metadata, just to save you some extra time!</p><p>Thanks for reading, good luck with your apps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=767278c211a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>