<?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 Marcos Timm on Medium]]></title>
        <description><![CDATA[Stories by Marcos Timm on Medium]]></description>
        <link>https://medium.com/@marcostimm?source=rss-941ad76aaf36------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Akxd0kXBIRie1sdfslZ_hw.png</url>
            <title>Stories by Marcos Timm on Medium</title>
            <link>https://medium.com/@marcostimm?source=rss-941ad76aaf36------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:53:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcostimm/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[Build an Exercise Tracking iOS App with Swift 3 and Realm]]></title>
            <link>https://medium.com/@marcostimm/build-an-exercise-tracking-ios-app-with-swift-3-and-realm-3ed8f0d8ddb4?source=rss-941ad76aaf36------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ed8f0d8ddb4</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[realm]]></category>
            <category><![CDATA[gps-tracking-system]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Marcos Timm]]></dc:creator>
            <pubDate>Sat, 17 Jun 2017 21:44:14 GMT</pubDate>
            <atom:updated>2017-06-17T21:49:55.413Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*X7F6yjPDxAaHNjt-vjH1CQ.jpeg" /></figure><p>There is a lot of app to do that. I know it. But recently I search for a specific tracking app to monitor Aquatic Activities that need some specific data and I could not find. So, what you do when you don’t find something? You just build your own app, right?</p><p>Let&#39;s get create a Exercise Tracking App for iOS with Swift 3.1 and using <a href="https://realm.io/products/realm-mobile-database/">Realm</a> to record ours workouts.</p><h3>Create a new project</h3><p>with template ‘Single View Application’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZsvR9ppQd5BLPCSbmjnovg.png" /><figcaption>Xcode 8.3.3 New project template screen</figcaption></figure><p>Before continue, let&#39;s make a <strong>couple of configurations</strong> in your project to accept receive data from <strong>User Location. </strong><em>(If you forgot to set this, your app will crash)</em></p><ul><li>Go to Info.plist file and set two keys of Privacy locations. You can change the value of this keys as you wish:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0c_Lfe9u2FZ9R6J8RQiFVw.png" /></figure><ul><li>Enable two Capabilities: Background Modes -&gt; Location updates and Healthkit</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PBp3DzJfYFRoQ4tOx5xemg.png" /><figcaption>Embed In →Navigation Controller (Xcode 8.3.3)</figcaption></figure><p>Then go to Main Storyboard, click on first View Controller and embed to a <strong>Navigation View Controller</strong>.</p><p>You don&#39;t need do use Navigation Controller if you want. But this is what I chose to this example. All right?</p><h3>Build a Basic UI</h3><p><strong>The first screen</strong> of this Tracking App will be a list of all workouts to keep you motivated for continue doing your exercise and keep even better and to do this let&#39;s use <strong>TableView </strong><em>(don&#39;t forgot to mark all constraints to zero of View)</em>. Add a <strong>Bar Button Item</strong> to <em>Navigation Item</em> of this view and change <em>System Item </em>to Add in Attributes inspector of element.</p><p>For the <strong>Tracking Screen</strong>, add one more <strong>View Controller</strong> to <em>Main Storyboard</em> and create a Segue to her from our Bar Button holding a Command key and dragging to this view. In this view we need to add some basic informations about current workout like:</p><ul><li><strong>Distance</strong></li><li><strong>Duration</strong></li><li><strong>Pace</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*aMQYiwUKlnaO_Xoyk03p_Q.png" /></figure><p>Fell free to choice the best layout as you prefer. You will need <strong>3x Label</strong> (distance, duration and pace), <strong>1x Button </strong><em>(Start/Stop workout)</em><strong> </strong>and a <strong>MKMapView</strong> for show the tracking line. After display all this element in a elegant layout create a <strong>IBOutlet for all</strong> and add a <strong>IBAction</strong> for the button.</p><h3>Let&#39;s go to the best part. The code:</h3><blockquote>Before continue, we need to <a href="https://realm.io/docs/swift/latest/#getting-started">install Realm</a> and the simple way to do that is using <a href="https://guides.cocoapods.org/using/getting-started.html">CocoaPods</a>. Look at the documentation and choice the best way for you. And If you just install a Pod, remember to close your Xcode project and <strong>open a .xcworkspace file</strong> <em>(instead .xcodeproj)</em></blockquote><p>For save your Workouts we need to create Realm data models using regular Swift classes with properties. I create a <strong>save()</strong> method to keep simple save the object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d25c7a0907ff2c6b120fd5026e270b3/href">https://medium.com/media/6d25c7a0907ff2c6b120fd5026e270b3/href</a></iframe><p>For each Workout we need to record all of locations that the GPS register and we do that using a <strong>To-Many Relationship</strong> creating a <em>List&lt;Location&gt;. </em>And <strong>Location</strong> will be another realm model with the properties of GPS locations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2deb00af9fb77c16aad39af299777ded/href">https://medium.com/media/2deb00af9fb77c16aad39af299777ded/href</a></iframe><p>Now go back to the tracking View and create a new <strong>UIViewController</strong> to make de magic. First we need to import some libraries, set variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b86ed7f20be9eeaa9bf9169ba3140617/href">https://medium.com/media/b86ed7f20be9eeaa9bf9169ba3140617/href</a></iframe><p>In this section of Class, we have some IBOutlet’s, in line fifteen the Workout Realm Object is instantiated, in line 22 we declare <strong>Core Location Manager</strong> with a little configuration, this object will be responsible for <strong>ask user if he want share location</strong> and start/stop tracking. Finally a lazy var for core locations and a Timer for update our exercise timer.</p><blockquote>For ask permission for user locations, you need to add two new key called ‘’Privacy — Location Always Usage Description” and “Privacy — Location When In Use Usage Description” in your Info.plist file with a describe text about why are you asking for get user location.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97c24473bb705a2d7e8fa4897f3b74d0/href">https://medium.com/media/97c24473bb705a2d7e8fa4897f3b74d0/href</a></iframe><p>In viewWillAppear CLLocationManager is instantiated and some configurations is defined, including request user authorization.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c2da8f5f01d35f2aff87a7211b4624b/href">https://medium.com/media/1c2da8f5f01d35f2aff87a7211b4624b/href</a></iframe><p>The code above start with a IBAction of our <strong>Start Button</strong>. The most important codes is a timer that initiate how our app will show de information each second and tell to <strong>locationManager</strong> to <strong>start update location</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e7b15ff8cf0d3da3b43d374e7ac7392/href">https://medium.com/media/4e7b15ff8cf0d3da3b43d374e7ac7392/href</a></iframe><p>Func <strong>saveWorkout</strong>() will save our data do Realm database. Simple like that.</p><p>When <strong>Core Location Manager</strong> is used, we need do inform what we want to do <strong>when location is updated</strong> (when user walk or swim or drive away…), and this is very easy with Delegate and <strong>didUpdateLocations</strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fe3bc9ff3a785ce7fc8bc2216cc8233/href">https://medium.com/media/9fe3bc9ff3a785ce7fc8bc2216cc8233/href</a></iframe><p>Note in this code we calculate de <strong>distance</strong>, <strong>pace</strong>, define the <strong>center of map</strong> with setRegion and finally add a <strong>MKPolyline</strong> to make a <strong>tracking line inside our map</strong>. This is very awesome!</p><p>To finish, lets implement one method of MKMapViewDelegate: rendererFor will asks the delegate to renderer object to use when drawing the specified overlay, so you can set the color and line width.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb7584e68108c79c081b2eb7a04ab58c/href">https://medium.com/media/eb7584e68108c79c081b2eb7a04ab58c/href</a></iframe><p>So far the app is already able to track the location, plot the route on a map and save the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*kb7cyCJ-XtIukAJvUhEFNA.png" /></figure><p>To test the app in simulator you will need to manually set the location like image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fM4ZDw74RmMwywG6NmV6Hw.png" /></figure><p>Good look! If you have any questions, please, download the complete code on Github and look at the working code:</p><p><a href="https://github.com/marcostimm/swift-realm-sport-tracking">marcostimm/swift-realm-sport-tracking</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ed8f0d8ddb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>