Jekyll2019-03-17T21:25:10+01:00https://kairadiagne.com/feed.xmlKaira DiagneHere I write about everything I learn about iOS developmentCreating a QR code scanner for your iOS app in Swift2019-03-07T00:00:00+01:002019-03-07T00:00:00+01:00https://kairadiagne.com/2019/03/07/creating-a-qrscanner-in-swift<p>A QR code, short for Quick Response code, is a two dimensional barcode. Because it stores information in both the horizontal and vertical direction it can contain more information than a traditional barcode. It can for example store a url which deep links to a certain page in your app.</p>
<p>Because nowadays many people own a smartphone with a build in QR code reader and a fast internet connection they are really taking off. For example in China you can pay anywhere by using a QR code and this is rapidly expanding to other countries in Southeast Asia.</p>
<p>Since iOS 11 the native Camera app has built in support for scanning QR codes, eliminating the need for a separate app. However you can also built this functionality into your own app for example when you want it to be more discoverable or want to provide a custom user interface. This can be easily done with the help of the <code class="highlighter-rouge">AVFoundation</code> framework.</p>
<h3 id="setting-up-the-capture-session">Setting up the Capture Session</h3>
<p>Everything related to QR code scanning revolves around an <code class="highlighter-rouge">AVCaptureSession</code>.
A capture session manages access to an <code class="highlighter-rouge">AVCaptureDevice</code>, which represents a physical capture device in your phone and connects it to one or multiple <code class="highlighter-rouge">AVCaptureOutput</code>s. In our case we will add the default video camera that is located at the back of the phone as the input and as an output we attach an instance of <code class="highlighter-rouge">AVCaptureMetadataOutput</code> that will forward us the metadata from the video. Finally, we tell the output the type of metadata were interested in by setting the <code class="highlighter-rouge">metadataObjectTypes</code> to <code class="highlighter-rouge">[.qr]</code>.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">setupCaptureSession</span><span class="p">()</span> <span class="p">{</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">videoCaptureDevice</span> <span class="o">=</span> <span class="n">captureDeviceType</span><span class="o">.</span><span class="nf">default</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="o">.</span><span class="n">video</span><span class="p">),</span>
<span class="k">let</span> <span class="nv">captureDeviceInput</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="kt">AVCaptureDeviceInput</span><span class="p">(</span><span class="nv">device</span><span class="p">:</span> <span class="n">videoCaptureDevice</span><span class="p">),</span>
<span class="n">captureSession</span><span class="o">.</span><span class="nf">canAddInput</span><span class="p">(</span><span class="n">captureDeviceInput</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span>
<span class="c1">//The device is not available or can't be added to the session.</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="k">let</span> <span class="nv">metadataOutput</span> <span class="o">=</span> <span class="kt">AVCaptureMetadataOutput</span><span class="p">()</span>
<span class="k">guard</span> <span class="n">captureSession</span><span class="o">.</span><span class="nf">canAddOutput</span><span class="p">(</span><span class="n">metadataOutput</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span>
<span class="c1">// The output can't be added to the session.</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="n">captureSession</span><span class="o">.</span><span class="nf">addInput</span><span class="p">(</span><span class="n">captureDeviceInput</span><span class="p">)</span>
<span class="n">captureSession</span><span class="o">.</span><span class="nf">addOutput</span><span class="p">(</span><span class="n">metadataOutput</span><span class="p">)</span>
<span class="k">guard</span> <span class="n">metadataOutput</span><span class="o">.</span><span class="n">availableMetadataObjectTypes</span><span class="o">.</span><span class="nf">contains</span><span class="p">(</span><span class="o">.</span><span class="n">qr</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span>
<span class="c1">// QR code metadata output is not available on this device</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="n">metadataOutput</span><span class="o">.</span><span class="n">metadataObjectTypes</span> <span class="o">=</span> <span class="p">[</span><span class="o">.</span><span class="n">qr</span><span class="p">]</span>
<span class="n">metadataOutput</span><span class="o">.</span><span class="nf">setMetadataObjectsDelegate</span><span class="p">(</span><span class="k">self</span><span class="p">,</span> <span class="nv">queue</span><span class="p">:</span> <span class="n">processingQueue</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="setting-up-the-camera">Setting up the Camera</h3>
<p>To show the camera video preview in the scanner UI we need to add an instance of <code class="highlighter-rouge">AVCaptureVideoPreviewLayer</code> as a sublayer of our view, connect it to the capture session and tell the session to <code class="highlighter-rouge">startRunning</code>. Before we do that we need to make sure that we have permission to access the camera. To do this we add the <code class="highlighter-rouge">NSCameraUsageDescription</code> key to our app’s <code class="highlighter-rouge">Info.plist</code> with a description of why our app needs access to the camera, check the authorization status and respond to it.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">requestAuthorization</span><span class="p">()</span> <span class="p">{</span>
<span class="k">switch</span> <span class="kt">AVCaptureDevice</span><span class="o">.</span><span class="nf">authorizationStatus</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="kt">AVMediaType</span><span class="o">.</span><span class="n">video</span><span class="p">)</span> <span class="p">{</span>
<span class="k">case</span> <span class="o">.</span><span class="nv">authorized</span><span class="p">:</span>
<span class="c1">// The user has previously granted permission to access the camera.</span>
<span class="k">case</span> <span class="o">.</span><span class="nv">notDetermined</span><span class="p">:</span>
<span class="c1">// We have never requested access to the camera before.</span>
<span class="kt">AVCaptureDevice</span><span class="o">.</span><span class="nf">requestAccess</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="o">.</span><span class="n">video</span><span class="p">)</span> <span class="p">{</span> <span class="n">granted</span> <span class="k">in</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">async</span> <span class="p">{</span>
<span class="k">guard</span> <span class="n">granted</span> <span class="k">else</span> <span class="p">{</span>
<span class="c1">// The user denied the camera access request.</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="c1">// The user has granted permission to access the camera.</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">case</span> <span class="o">.</span><span class="n">denied</span><span class="p">,</span> <span class="o">.</span><span class="nv">restricted</span><span class="p">:</span>
<span class="c1">// The user either previously denied the access request or the</span>
<span class="c1">// camera is not available due to restrictions.</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>One more thing to understand about <code class="highlighter-rouge">AVCaptureSession</code> is that the <code class="highlighter-rouge">startRunning</code> method is blocking and the class itself is not thread safe. Thats why we create a serial <code class="highlighter-rouge">DispatchQueue</code> and make sure we only access the session on that queue. This way we serialize access to the session and prevent blocking the main thread.</p>
<h3 id="detecting-the-qr-code">Detecting the QR code</h3>
<p>At this point we have configured the capture session, connected it to a video preview layer and handled camera permissions from the user. The last thing we need to do is to conform to the <code class="highlighter-rouge">AVCaptureMetaDataOutputObjectsDelegate</code> by implementing the <code class="highlighter-rouge">captureOutput:didOutputMetadataObjects:fromConnection:</code>. This method will be called when a QR code is detected. Let’s say we expect the QR code to contain a url which we can use to deep link to a certain page in the app. To achieve this our code would look something like this:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">metadataOutput</span><span class="p">(</span><span class="n">_</span> <span class="nv">output</span><span class="p">:</span> <span class="kt">AVCaptureMetadataOutput</span><span class="p">,</span> <span class="n">didOutput</span> <span class="nv">metadataObjects</span><span class="p">:</span> <span class="p">[</span><span class="kt">AVMetadataObject</span><span class="p">],</span> <span class="n">from</span> <span class="nv">connection</span><span class="p">:</span> <span class="kt">AVCaptureConnection</span><span class="p">)</span> <span class="p">{</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">readableObject</span> <span class="o">=</span> <span class="n">metadataObjects</span><span class="o">.</span><span class="n">first</span> <span class="k">as?</span> <span class="kt">AVMetadataMachineReadableCodeObject</span><span class="p">,</span>
<span class="k">let</span> <span class="nv">stringValue</span> <span class="o">=</span> <span class="n">readableObject</span><span class="o">.</span><span class="n">stringValue</span><span class="p">,</span>
<span class="k">let</span> <span class="nv">url</span> <span class="o">=</span> <span class="kt">URL</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="n">stringValue</span><span class="p">)</span> <span class="k">else</span> <span class="k">do</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="c1">// Check if the url is valid deep link</span>
<span class="c1">// Deep deep link to a certain page into the app.</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="conclusion">Conclusion</h3>
<p>Thanks to the AVFoundation framework adding a QR code scanner to your app is very straightforward and does not require a lot of work. When you decide to do so remember to always handle the camera permissions and to dispatch all session code that interacts with the session on a serial dispatch queue to avoid threading issues and blocking the main thread.</p>
<p>Contact me on <a href="https://twitter.com/kairadiagne">Twitter @kairadiagne</a> if you have any questions, comments or feedback.</p>A QR code, short for Quick Response code, is a two dimensional barcode. Because it stores information in both the horizontal and vertical direction it can contain more information than a traditional barcode. It can for example store a url which deep links to a certain page in your app.What I learned from building an app extension2019-02-18T00:00:00+01:002019-02-18T00:00:00+01:00https://kairadiagne.com/2019/02/18/what-i-learned-from-building-an-app-extension<p>With App extensions iOS provides us with a powerful way to make the core functionality of our app available for use from system apps and apps from other developers. Instead of switching between apps they provide the user with an opportunity to use your app in the context of another app. With a share extension for example you could share images from the photos app straight to your app.</p>
<p>There are several types of extensions you could create for your app. Each having its own specific purpose and part of the system it is exposed from, called an <code class="highlighter-rouge">extension point</code>. Share and Action extensions for example are made available through the system share sheet.</p>
<h3 id="how-does-it-work">How does it work</h3>
<p>An app extension is an app of its own and runs independent from your app, also called the <code class="highlighter-rouge">containing app</code>. Since it runs in the context of another app, the <code class="highlighter-rouge">host app</code>, it has a different lifecycle compared to a normal app. It consists of several steps. Let’s look at a scenario where a user wants to share some images from the photos app:</p>
<ol>
<li>
<p>After the user has selected some images and has tapped the share button the photo’s app will instantiate an <code class="highlighter-rouge">UIActivityViewController</code> and present it to the user.</p>
</li>
<li>
<p>Now the user can pick from a list of share extensions that accept images as input. Once the user selects your app the photos app will create an <code class="highlighter-rouge">NSExtensionContext</code> and request the system to open the extension. An <code class="highlighter-rouge">NSExtensionContext</code> contains all the information that the extension needs to do its job and allows for communication between the host app and the extension and vice versa. The system will handle the request by instantiating the <code class="highlighter-rouge">principal view controller</code>, the first view controller in your extension, and provide it with the extension context.</p>
</li>
<li>
<p>At this point the extension will launch and display its UI in the context of the host app.</p>
</li>
<li>
<p>In this step the user will interact with your extension. After that they have the choice to either finish or dismiss the extension. When an extension is finished it needs to communicate this back to the host app by completing the request. Dismissal is communicated back to the host app by calling the cancel method with an appropriate error. Both communication is done through the extension context.</p>
</li>
<li>
<p>The host app handles the completion or cancellation of the request and tears down the extensions UI. After that the system will terminate the extension.</p>
</li>
</ol>
<h3 id="make-your-app-ready-for-an-extension">Make your app ready for an extension</h3>
<p>When writing an extension you probably want to reuse parts of your core business logic layer or some UI elements from your main app. You can do this by creating embedded frameworks, for example one for core business logic and another for common UI elements. Some API for example <code class="highlighter-rouge">UIApplication</code> is not available from an extension. By setting the <code class="highlighter-rouge">"Require Only App-Extension-Safe API"</code> to <code class="highlighter-rouge">YES</code> in the frameworks build settings the compiler will raise an error if you attempt to use unsupported API in your framework. Once you extracted the code that you want to reuse in a framework the process of adding a new extension is not so complicated. Added benefits are that it makes your code more modular, it lets you make use of the <code class="highlighter-rouge">internal</code> access level and allows you to work on certain parts of your app without compiling the entire app.</p>
<h3 id="app-groups">App Groups</h3>
<p>Just like how different apps don’t have access to each others data an extension by default does not have access to your main apps data. In order to share data you need to create an <code class="highlighter-rouge">App Group</code>. When both your app and extension are added to the same <code class="highlighter-rouge">App Group</code> they both get access to a shared directory where you can store data. Because your app and extension could be running at the same time it is important to safely coordinate writing and reading to shared files. If you use <code class="highlighter-rouge">Core Data</code> together with <code class="highlighter-rouge">sqlite</code> you will get this for free.</p>
<h3 id="defining-your-ui-in-storyboard-xib-or-code">Defining your UI in storyboard, XIB or Code</h3>
<p>No, I’m not going tell you what I think is the best way to create the user interface of your app. I believe that all of them have their pros and cons and that you should always pick the right tool for the job. In the default Xcode template the principal view controller is defined in a storyboard. If you want to go for a XIB or pure code you can do this by adding the <code class="highlighter-rouge">NSExtensionPrincipalClass</code> key to the extensions <code class="highlighter-rouge">info.plist</code> while removing the <code class="highlighter-rouge">NSExtensionMainStoryboard</code> key value pair. <code class="highlighter-rouge">NSExtensionPrincipalClass</code> should hold a string value with the name of the primary view controller. The next step is to mark that view controller as <code class="highlighter-rouge">@objc</code> to make it accessible to the Objective-c runtime:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">@objc(PrincipalClassName)</span>
<span class="kd">final</span> <span class="kd">class</span> <span class="kt">PrincipalClassName</span><span class="p">:</span> <span class="kt">UIViewController</span> <span class="p">{</span>
<span class="o">...</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="writing-unit-tests">Writing unit tests</h3>
<p>It is not supported to run unit tests inside an app extension. So how can we write unit test for our extension? The answer is easy. Since you most likely will create an embedded framework for the core business logic of the app you can easily test that code in isolation. Extension specific code that you want to test most likely revolves around the <code class="highlighter-rouge">NSExtensionContext</code>. By moving that code into helper objects you can test that part of your code as well.</p>
<h3 id="conclusion">Conclusion</h3>
<p>There are many different app extension you can create for your app. Because they provide users with a way to use your app in the context of another app they can be powerful and make your app stand out.</p>
<p>Contact me on <a href="https://twitter.com/kairadiagne">Twitter @kairadiagne</a> if you have any questions, comments or feedback.</p>With App extensions iOS provides us with a powerful way to make the core functionality of our app available for use from system apps and apps from other developers. Instead of switching between apps they provide the user with an opportunity to use your app in the context of another app. With a share extension for example you could share images from the photos app straight to your app.A better way to show progress with Progress2019-01-18T00:00:00+01:002019-01-18T00:00:00+01:00https://kairadiagne.com/2019/01/18/Better-progress-with-NSProgress<p>We all enjoy fast and responsive apps that feel good when you use them. Therefore as iOS developers we spend a lot of time on delivering the best possible experience to the user. This becomes extra important when your application has to perform some long running operation, for example downloading a large video. Depending on the size of the download, speed of the user’s internet connection, or hardware this could take a while, which can be frustrating for the user. We can improve such an experience by giving the user a sense of the current work that is happening and how far that work has progressed by using a progress indicator.</p>
<p>Lately I have discovered the <code class="highlighter-rouge">Progress</code> class from Foundation which allows us to do just that. This weeks blogpost will show you the basics of <code class="highlighter-rouge">Progress</code> by taking you through an example of how to show the progress of downloading an image to the user.</p>
<h3 id="what-is-progress">What is <code class="highlighter-rouge">Progress</code>?</h3>
<p><code class="highlighter-rouge">Progress</code> is a class that represents the completion of some work that an application is doing for example downloading or uploading a file to the server.
Many of the api’s that we use on a daily basis report their progress with <code class="highlighter-rouge">Progress</code> for example <code class="highlighter-rouge">NSURLSession</code> which we get to later in this blogpost. Of course you can also use <code class="highlighter-rouge">Progress</code> to report progress from the custom components in your application and if the work your application is doing consists of multiple sub steps you can even use composition to create a tree of progress objects.</p>
<h3 id="the-basics-of-progress">The basics of <code class="highlighter-rouge">Progress</code></h3>
<p><code class="highlighter-rouge">Progress</code> has three important properties that you should know about:</p>
<ul>
<li><code class="highlighter-rouge">totalUnitCount</code></li>
<li><code class="highlighter-rouge">completedUnitCount</code></li>
<li><code class="highlighter-rouge">fractionCompleted</code></li>
</ul>
<p>Let’s have a look at each one of them.</p>
<h4 id="totalunitcount">totalUnitCount</h4>
<p>The <code class="highlighter-rouge">totalUnitCount</code> represents how many units of work a <code class="highlighter-rouge">Progress</code> object should track, for example the amount of bytes of a download.</p>
<h4 id="completedunitcount">completedUnitCount</h4>
<p>The <code class="highlighter-rouge">completedUnitCount</code> reports how much work of the total amount of work that the progress tracks has completed.</p>
<h4 id="fractioncompleted">fractionCompleted</h4>
<p><code class="highlighter-rouge">fractionCompleted</code> tells you how much of the work has completed. It is a double value between 0 and 1 where 0 means that none of the work has completed and 1 means that all the work has completed.</p>
<h3 id="localization">Localization</h3>
<p><code class="highlighter-rouge">Progress</code> also has a property called <code class="highlighter-rouge">localizedDescription</code> and <code class="highlighter-rouge">localizedAdditionalDescription</code> which can be used to provide the user with more information on what is happening in your application. Through the <code class="highlighter-rouge">kind</code> property you can tell the progress object what its unit represents and to format the progress as such, for example if you set it to <code class="highlighter-rouge">ProgressKind.file</code> the <code class="highlighter-rouge">localizedAdditionalDescription</code> gives you a byte formatted string.</p>
<h3 id="an-example">An example</h3>
<p>In this example we are going to download an image from the internet and display the progress to the user. Download the <a href="https://github.com/kairadiagne/code-samples/tree/master/ProgressExample">sample project</a> if you want to follow along with this example.</p>
<p>The example project has one view controller which is very simple. It contains an <code class="highlighter-rouge">UIImageView</code> for showing the downloaded image, a <code class="highlighter-rouge">UIButton</code> to trigger a download, two <code class="highlighter-rouge">UILabels</code> that will contain text to explain the user what’s going. It also contains a <code class="highlighter-rouge">UIProgressView</code> which is a view that displays the progress in a progress bar.</p>
<p>Our view controller also has a reference to an <code class="highlighter-rouge">ImageDownloader</code> which has a method <code class="highlighter-rouge">downloadImage(with:completion:)</code> that downloads the image from the passed in <code class="highlighter-rouge">URL</code> This method returns a progress object which we will use to update our UI and has a completion handler with an optional image that gets called once the download has finished.</p>
<p>Inside the <code class="highlighter-rouge">downloadImage(with:completion)</code> method we create a <code class="highlighter-rouge">URLSessionDownloadTask</code> with the passed in url of the image. Since iOS 11 a task has a property called <code class="highlighter-rouge">progress</code> that contains the tasks progress object which we can use to update our UI.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">downloadImage</span><span class="p">(</span><span class="n">with</span> <span class="nv">url</span><span class="p">:</span> <span class="kt">URL</span><span class="p">,</span> <span class="nv">completion</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">((</span><span class="kt">UIImage</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="kt">Progress</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">task</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="nf">downloadTask</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
<span class="o">...</span>
<span class="k">do</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">imageData</span> <span class="o">=</span> <span class="k">try</span> <span class="kt">Data</span><span class="p">(</span><span class="nv">contentsOf</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">image</span> <span class="o">=</span> <span class="kt">UIImage</span><span class="p">(</span><span class="nv">data</span><span class="p">:</span> <span class="n">imageData</span><span class="p">)</span>
<span class="nf">completion</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
<span class="nf">completion</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">task</span><span class="o">.</span><span class="nf">resume</span><span class="p">()</span>
<span class="k">return</span> <span class="n">task</span><span class="o">.</span><span class="n">progress</span>
<span class="p">}</span>
</code></pre></div></div>
<p>When a download action is triggered we call the download function and store the returned progress object in a property called <code class="highlighter-rouge">downloadProgress</code> in our view controller. It has a property observer in which we attach the new progress object to the UI. To display the progress in our progress view we set <code class="highlighter-rouge">downloadProgress</code> to the <code class="highlighter-rouge">observedProgress</code> property of the view. We use KVO to update our progress label every time the <code class="highlighter-rouge">localizedAdditionalDescription</code> changes. KVO notifications of properties on <code class="highlighter-rouge">Progress</code> are sent from the thread on which the progress was updated and therefore we have to dispatch our update of the progress label onto the main queue to make sure our UI gets updated on the main thread.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="k">var</span> <span class="nv">downloadProgress</span><span class="p">:</span> <span class="kt">Progress</span><span class="p">?</span> <span class="p">{</span>
<span class="k">didSet</span> <span class="p">{</span>
<span class="n">observation</span><span class="p">?</span><span class="o">.</span><span class="nf">invalidate</span><span class="p">()</span>
<span class="n">progressView</span><span class="o">.</span><span class="n">observedProgress</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">progress</span> <span class="o">=</span> <span class="n">downloadProgress</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="n">progressView</span><span class="o">.</span><span class="n">observedProgress</span> <span class="o">=</span> <span class="n">progress</span>
<span class="n">progressDescriptionLabel</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">progress</span><span class="o">.</span><span class="n">localizedDescription</span>
<span class="n">observation</span> <span class="o">=</span> <span class="n">progress</span><span class="o">.</span><span class="nf">observe</span><span class="p">(\</span><span class="o">.</span><span class="n">localizedAdditionalDescription</span><span class="p">,</span> <span class="nv">options</span><span class="p">:</span> <span class="p">[</span><span class="o">.</span><span class="n">initial</span><span class="p">,</span> <span class="o">.</span><span class="k">new</span><span class="p">])</span> <span class="p">{</span> <span class="p">[</span><span class="k">weak</span> <span class="k">self</span><span class="p">]</span> <span class="n">_</span><span class="p">,</span> <span class="n">change</span> <span class="k">in</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">async</span> <span class="p">{</span>
<span class="k">self</span><span class="p">?</span><span class="o">.</span><span class="n">progressLabel</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">change</span><span class="o">.</span><span class="n">newValue</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="conclusion">Conclusion</h3>
<p>Next time when your application is performing some long running task consider using <code class="highlighter-rouge">Progress</code> to let the user know what is happening in the application. As you can see from the example some Cocoa api’s already support progress reporting and therefore just require a few steps to make it work. But there is much more you can do with <code class="highlighter-rouge">Progress</code>. You could for example create a tree of progress objects or use the progress object to cancel, pause or resume work. Take a look at it and have some fun!</p>
<p>Contact me on <a href="https://twitter.com/kairadiagne">Twitter @kairadiagne</a> if you have any questions, comments or feedback.</p>We all enjoy fast and responsive apps that feel good when you use them. Therefore as iOS developers we spend a lot of time on delivering the best possible experience to the user. This becomes extra important when your application has to perform some long running operation, for example downloading a large video. Depending on the size of the download, speed of the user’s internet connection, or hardware this could take a while, which can be frustrating for the user. We can improve such an experience by giving the user a sense of the current work that is happening and how far that work has progressed by using a progress indicator.Understanding the Core Data perform methods2019-01-06T00:00:00+01:002019-01-06T00:00:00+01:00https://kairadiagne.com/2019/01/06/understanding-the-core-data-perform-methods<p>Core Data is controversial to many developers. Some love it while others think it is too difficult or that it contains too many bugs. Personally I enjoy using Core Data. Out of the box it provides a lot of functionality needed for managing the model layer of most applications and because it is supported by Apple it saves us from adding another third part dependency to our projects.</p>
<p>However I do think Core Data has a high learning curve. This weeks blogpost is about one of the things of Core Data that took me a while to understand, the <code class="highlighter-rouge">perform(_:)</code> and <code class="highlighter-rouge">performAndWait(_:)</code> api. Let’s dive in.</p>
<h3 id="core-data-in-a-nutshell">Core Data in a nutshell</h3>
<p>In Core Data all model objects are subclasses of <code class="highlighter-rouge">NSManagedObject</code>. You typically access those objects from a <code class="highlighter-rouge">NSManagedObjectContext</code>, which is like a scratchpad that allows you to fetch, change, add and delete managed objects and save these changes to a persistent store. The persistent store is like a repository in which managed objects get stored. Usually the persistent store is a SQLite database.</p>
<h3 id="nsmanagedobjectcontext-concurrency-policies">NSManagedObjectContext Concurrency policies</h3>
<p>A managed object context is tied to a serial queue to ensure that all operations on the context are performed in order so that the data stays consistent. Therefore the most important thing to remember is that you should always access the context and its managed objects from its own queue. Which type of queue is used by the context depends on its <code class="highlighter-rouge">concurrencyType</code>.</p>
<p>A context with the <code class="highlighter-rouge">NSPrivateQueueConcurrencyType</code> has its own background queue. This type of context, usually called a background or private context, is great for executing work that can block the main queue for example importing a large JSON response into the persistent store.</p>
<p>A context with the <code class="highlighter-rouge">NSMainQueueConcurrencyType</code> does not have its own queue but instead is connected to your applications main queue. This type of context, <code class="highlighter-rouge">NSPersistentContainer</code> calls it a view context, is typically used for the user interface of your application.</p>
<p>That is all very straightforward right? But the queue used by the context is private so how can we make sure we only access the context from its own queue? This is where the <code class="highlighter-rouge">perform(_:)</code> and <code class="highlighter-rouge">performAndWait(_:)</code> methods comes in. Work that is wrapped inside these methods is executed on the contexts queue. On a background queue you always need to wrap your work into one of these methods. For view contexts this is a bit different, because they are typically used for the user interface most of the time you can access the context directly since the UI runs on the main queue. However if you want to access a main context or its managed objects on a background queue you do need to use the <code class="highlighter-rouge">perform(_:)</code> api to make sure the work is executed on the main queue.</p>
<h3 id="perform_"><code class="highlighter-rouge">perform(_:)</code></h3>
<p>The <code class="highlighter-rouge">perform(_:)</code> method is asynchronous which means it will return immediately and does not block the calling thread. The block submitted through this method will be executed on the queue’s context in FIFO order.</p>
<p>One of the advantages of using Core Data is that it provides you with change tracking, which is great for writing reactive user interfaces. The <code class="highlighter-rouge">perform(_:)</code> method treats the block you have submitted as a user event, which means that at the end of every perform block it will make sure that all changes are processed and relevant notifications are sent. <code class="highlighter-rouge">NSFetchedResultsController</code> for example automatically updates your tableview in response to those notifications.</p>
<p>Lastly, every block submitted through the <code class="highlighter-rouge">perform(_:)</code> method gets wrapped in a autorelease pool.</p>
<h3 id="performandwait_"><code class="highlighter-rouge">performAndWait(_:)</code></h3>
<p>The <code class="highlighter-rouge">performAndWait(_:)</code> method is synchronous which means it will return once the block submitted to the queue has been executed. Thus it will block the calling thread. A block submitted through the <code class="highlighter-rouge">performAndWait(_:)</code> is not treated as a user event, which means that if your using a <code class="highlighter-rouge">NSFetchedResultsController</code> you need to manually tell the context to process the changes by calling <code class="highlighter-rouge">processPendingChanges()</code> or save the context by calling <code class="highlighter-rouge">save()</code> in order for it to pick up the changes. It also does not wrap the block in an autorelease pool.</p>
<p>Calls to <code class="highlighter-rouge">performAndWait(_:)</code> are reentrant. This means that if you call <code class="highlighter-rouge">performAndWait(_:)</code> from within another perform block they will be executed inline.</p>
<h3 id="conclusion">Conclusion</h3>
<p>When interacting with a context or its managed objects always use a perform method to make sure that the work is executed on the right queue. An exception to this rule is when you want to access a view context and you are already on the main queue. In that case you don’t need to use one of the perform methods.</p>
<p>Contact me on <a href="https://twitter.com/kairadiagne">Twitter @kairadiagne</a> if you have any questions, comments or feedback.</p>Core Data is controversial to many developers. Some love it while others think it is too difficult or that it contains too many bugs. Personally I enjoy using Core Data. Out of the box it provides a lot of functionality needed for managing the model layer of most applications and because it is supported by Apple it saves us from adding another third part dependency to our projects.Memory management in Swift2017-05-31T00:00:00+02:002017-05-31T00:00:00+02:00https://kairadiagne.com/2017/05/31/Memory-management-in-Swift<p>Recently I was having a hard time tracking down a bug that was the cause of performance problems in the app I was working on. It took me a while but it turned out that these performance problems were caused by a memory leak. After this experience I thought it would be a good time to learn more about memory management in Swift which resulted in this blogpost. Lets start with the basics.</p>
<h3 id="what-is-memory-management">What is memory management?</h3>
<p>Memory refers to all the hardware involved in storing information on your device. Your iPhone has two main ways of storing data 1. the disk 2. Random Access Memory (RAM). When an app is run on your iPhone a file containing all executable instructions will be loaded into the RAM. At the same time the system will claim a chunk of the RAM, which is called the heap. This is the place where all instances of our classes will live while the app is running. When we talk about memory management we refer to the process of managing heap memory. This means managing the life cycles of objects on the heap and making sure that these objects are freed when they are no longer needed so the memory can be reused. In Objective-C aside from primitives like Int, CGRect etc. everything is an object and therefore will be allocated on the heap. In Swift reference types are allocated on the heap, but values types are not. Managing the heap memory is very important because objects can be large and our apps get only so much memory from the system. Running low on memory will cause an iOS App to run slower and eventually will make the app crash. Although nowadays it is getting more rare to see a RAM overload since our devices are getting more powerful its always important to be a good memory citizen.</p>
<h3 id="reference-counting">Reference counting</h3>
<p>In Swift memory management is handled by Automatic Reference Counting (ARC). Whenever you create a new instance of a class ARC allocates a chunk of memory to store information about the type of instance and values of stored properties of that instance. Every instance of a class also has a property called reference count, which keeps track of all the properties, constants, and variables that have a strong reference to itself. A strong reference is basically a pointer that increments the reference count of the instance of a class it is pointing to with one. Whenever the reference count of an object reaches zero that object will be deallocated. This way ARC keeps class instances in memory as long as they are needed and frees up memory when they are no longer needed.</p>
<p>ARC has been around since iOS 5, and before that time developers used a system called Manual Reference Counting (MRR). MRR is not that different from ARC, and actually shares the same reference counting system and memory conventions, except that with ARC the compiler adds all the memory management code for you at compile time. To have a better understanding of how ARC works it is good to know more about MRR and its conventions:</p>
<p>1.You own any object you create<br />
2.You can take ownership of an object using retain<br />
3.When you no longer need it you must relinquish ownership of an object you own <br />
4.You must not relinquish ownership of an object you do not own</p>
<p>Based on these conventions developers would decide when they needed to retain (increment reference count) or release (decrement reference count) an object by sending it the message retain or release. Then there was also autorelease with which you declare that you don’t want to own an object beyond the scope in which you sent autorelease. It then gets added to the autoreleasepool, which when it gets drained sends release to all the objects in the pool. This usually happens when the current runloop ends but it can be sooner. So with autorelease you basically say “I want this instance to be released some time in the future”. Autorelease is very handy when you are passing instances around in your code, for example when you have a method that creates and returns an instance of a class you can use autorelease to make the caller responsible for managing its memory.</p>
<h3 id="strong-reference-cycles">Strong reference cycles</h3>
<p>So Swift does most of the memory management for us, Awesome! Does that mean we as developers don’t have to think about memory management anymore? The answer is no, because with ARC we always have to be careful not to create any strong reference cycles. You create a strong reference cycle when two instances of a class both hold strong references to each other. Because in this situation the reference count of those objects will never get to zero they will hold each other in memory until the application terminates. This is called a memory leak. Strong reference cycles are not always easy to find but a common pitfall is working with closures. This is best illustrated with an example.</p>
<p><a href="https://github.com/kairadiagne/ShinyFeatures">Shiny features</a> is a basic app that lets you search on GitHub for repositories of projects that demonstrate new iOS and Swift features announced at this years WWDC. When you first run the app you are greeted with a simple tableview thats lists some new Swift and iOS features. Once you select one of those topics you will be taken to another screen which fetches and presents a list of GitHub repositories found on that topic. When you select a repository you navigate to the RepositoryDetailViewController which will load the GitHub page of that repository in a UIWebView.</p>
<p>In this example there are two classes which are important to have a look at:</p>
<h5 id="repositorytableviewmodel">RepositoryTableViewModel</h5>
<p>RepositoryViewModel has a loadRepositories function which fires of a network request to the GithubAPI to load repositories for the selected iOS 11 or Swift feature. It also contains a stored property called changeHandler which gets called everytime the repositories get updated.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="kd">class</span> <span class="kt">RepositoryTableViewModel</span> <span class="p">{</span>
<span class="kd">typealias</span> <span class="kt">ChangeHandler</span> <span class="o">=</span> <span class="p">(()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span>
<span class="k">var</span> <span class="nv">repositories</span><span class="p">:</span> <span class="p">[</span><span class="kt">GitHubRepo</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> <span class="p">{</span>
<span class="k">didSet</span> <span class="p">{</span>
<span class="k">self</span><span class="o">.</span><span class="nf">changeHandler</span><span class="p">?()</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">private</span> <span class="k">let</span> <span class="nv">apiService</span><span class="p">:</span> <span class="kt">GithubAPIService</span>
<span class="k">var</span> <span class="nv">changeHandler</span><span class="p">:</span> <span class="kt">ChangeHandler</span><span class="p">?</span>
<span class="nf">init</span><span class="p">(</span><span class="nv">apiService</span><span class="p">:</span> <span class="kt">GithubAPIService</span> <span class="o">=</span> <span class="kt">GithubAPIService</span><span class="p">())</span> <span class="p">{</span>
<span class="k">self</span><span class="o">.</span><span class="n">apiService</span> <span class="o">=</span> <span class="n">apiService</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">loadRepositories</span><span class="p">(</span><span class="nv">feature</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="p">{</span>
<span class="n">apiService</span><span class="o">.</span><span class="nf">loadRepositories</span><span class="p">(</span><span class="nv">searchTerm</span><span class="p">:</span> <span class="n">feature</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">async</span> <span class="p">{</span>
<span class="k">switch</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="k">let</span> <span class="nv">repositories</span><span class="p">):</span>
<span class="k">self</span><span class="o">.</span><span class="n">repositories</span> <span class="o">=</span> <span class="n">repositories</span>
<span class="k">case</span> <span class="o">.</span><span class="nv">error</span><span class="p">:</span>
<span class="c1">// TODO: - Handle Error</span>
<span class="k">break</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<h5 id="repositorytableviewcontroller">RepositoryTableViewController</h5>
<p>This is a simple UITableViewController. In viewDidLoad we bind the viewController to the viewModel by setting its changeHandler and in viewWillAppear we tell the view model to load data.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="kd">class</span> <span class="kt">RepositoryTableViewController</span><span class="p">:</span> <span class="kt">UITableViewController</span> <span class="p">{</span>
<span class="kd">private</span> <span class="k">let</span> <span class="nv">feature</span><span class="p">:</span> <span class="kt">String</span>
<span class="kd">private</span> <span class="k">let</span> <span class="nv">viewModel</span><span class="p">:</span> <span class="kt">RepositoryTableViewModel</span>
<span class="nf">init</span><span class="p">(</span><span class="nv">feature</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">viewModel</span><span class="p">:</span> <span class="kt">RepositoryTableViewModel</span><span class="p">)</span> <span class="p">{</span>
<span class="k">self</span><span class="o">.</span><span class="n">feature</span> <span class="o">=</span> <span class="n">feature</span>
<span class="k">self</span><span class="o">.</span><span class="n">viewModel</span> <span class="o">=</span> <span class="n">viewModel</span>
<span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">nibName</span><span class="p">:</span> <span class="kc">nil</span><span class="p">,</span> <span class="nv">bundle</span><span class="p">:</span> <span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
<span class="kd">required</span> <span class="nf">init</span><span class="p">?(</span><span class="n">coder</span> <span class="nv">aDecoder</span><span class="p">:</span> <span class="kt">NSCoder</span><span class="p">)</span> <span class="p">{</span>
<span class="nf">fatalError</span><span class="p">(</span><span class="s">"init(coder:) has not been implemented"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">override</span> <span class="kd">func</span> <span class="nf">viewDidLoad</span><span class="p">()</span> <span class="p">{</span>
<span class="k">super</span><span class="o">.</span><span class="nf">viewDidLoad</span><span class="p">()</span>
<span class="n">tableView</span><span class="o">.</span><span class="nf">register</span><span class="p">(</span><span class="kt">UITableViewCell</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="nv">forCellReuseIdentifier</span><span class="p">:</span> <span class="s">"RepositoryCell"</span><span class="p">)</span>
<span class="n">viewModel</span><span class="o">.</span><span class="n">changeHandler</span> <span class="o">=</span> <span class="p">{</span>
<span class="k">self</span><span class="o">.</span><span class="n">tableView</span><span class="o">.</span><span class="nf">reloadData</span><span class="p">()</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">override</span> <span class="kd">func</span> <span class="nf">viewWillAppear</span><span class="p">(</span><span class="n">_</span> <span class="nv">animated</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span> <span class="p">{</span>
<span class="k">super</span><span class="o">.</span><span class="nf">viewWillAppear</span><span class="p">(</span><span class="n">animated</span><span class="p">)</span>
<span class="n">viewModel</span><span class="o">.</span><span class="nf">loadRepositories</span><span class="p">(</span><span class="nv">feature</span><span class="p">:</span> <span class="n">feature</span><span class="p">)</span>
<span class="p">}</span>
<span class="o">...</span>
<span class="p">}</span>
</code></pre></div></div>
<p>To make it a bit easier to spot strong reference cycles I have added a breakpoint that makes a popping sound everytime a viewcontroller gets deallocated (Many thanks to <a href="https://twitter.com/0xced/status/900692839557992449">Cédric Luthi</a> for sharing this with the community). Now I want you to run the app, pick a feature e.g. ARKit and then select a repository so that you will end up on a detail page. When you are on the detail page tapp the back button to go back to the RepositoryTableViewController. Because the detailViewController gets popped of the stack it is not referenced anymore so it will get deallocated and you should hear the sound when that happens. So far so good. You would expect the same thing to happen when we again press the backbutton but somehow our viewcontroller is retained and we do not hear any sound. lets find out why?</p>
<p>Remember what I said about being careful with closures. It turns out that in this example there is a strong reference cycle between the viewModel and the viewcontroller. Our viewModel gets stored in a property of the viewcontroller and since all properties in Swift are strong by default it gets retained. Our changehandler gets retained by the viewModel and also retains our viewcontroller because it references self to reload the tableview. How can we solve this?</p>
<p>This can be solved by using a capture list. A capture list defines the rules to use when capturing one or more reference types within the closures body. In our case we can use this to create a weak or unowned reference to self. A weak or unowned reference is basically a reference that does not increment the reference count of the object it points to. You create a weak reference when the captured reference may become nil at some point in the future. You create an unowned reference when the closure and the instance will always refer to each other and have the same lifetime, meaning they will get deallocated at the same time. Unlike weak references unowned references are non optional and therefore a bit easier to handle but you have to be sure that at the time the closure gets called self still exists otherwise your app will crash. Now go ahead and make the following change in RepositoryTableViewController and press CMD+R to run the app.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">override</span> <span class="kd">func</span> <span class="nf">viewDidLoad</span><span class="p">()</span> <span class="p">{</span>
<span class="k">super</span><span class="o">.</span><span class="nf">viewDidLoad</span><span class="p">()</span>
<span class="o">...</span>
<span class="n">viewModel</span><span class="o">.</span><span class="n">changeHandler</span> <span class="o">=</span> <span class="p">{</span> <span class="p">[</span><span class="k">weak</span> <span class="k">self</span><span class="p">]</span> <span class="k">in</span>
<span class="k">self</span><span class="p">?</span><span class="o">.</span><span class="n">tableView</span><span class="o">.</span><span class="nf">reloadData</span><span class="p">()</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>There you go! By capturing self as weak in our changeHandler RepositoryTableViewController and its viewmodel will deallocate once the viewcontroller is popped of the stack, thus we have broken up the strong reference cycle.</p>
<p>Thanks for reading this article and if you have any questions, comments or feedback let me know either through a comment in the comments section below or on <a href="https://www.twitter.com/kairadiagne">Twitter @kairadiagne</a>.</p>Recently I was having a hard time tracking down a bug that was the cause of performance problems in the app I was working on. It took me a while but it turned out that these performance problems were caused by a memory leak. After this experience I thought it would be a good time to learn more about memory management in Swift which resulted in this blogpost. Lets start with the basics.Code signing and provisioning profiles2016-08-12T00:00:00+02:002016-08-12T00:00:00+02:00https://kairadiagne.com/2016/08/12/Code-siging-and-Provisioning-profiles<p>Before I got my first job as an iOS Developer I did not know much about code signing and provisioning profiles. As a member of Apple’s free developer program I mostly used the simulator and occasionally ran my app on my device. Whenever I would run into problems I would rely on Xcode to fix them for me. But after a while I quickly found out that it usually is better to solve the problem yourself instead of relying on Xcode. To gain a better understanding of what I was actually doing I did some research on code signing and provisioning profiles and I wrote it down as a reference to myself and everyone else who is interested in it.</p>
<h3 id="what-is-code-signing">What is code signing?</h3>
<p>Code signing is the process of digitally signing executables to confirm the software author and to guarantee that the code has not been altered or corrupted since it was signed. In iOS all code needs to be signed with a certificate that is issued by Apple. This makes Apple the only one who can decide if your app is allowed to run on a device or not. The only exception to this is when a device is jailbroken.</p>
<p>In short this is how it works:</p>
<ol>
<li>You register as a developer with the Apple WorldWide Developer Program.</li>
<li>Apple verifies your identity and then issues a certificate with which we can sign our apps and submit them for distribution to the App Store.</li>
<li>When your app is finished your app will be reviewed by Apple. During this process Apple checks if your app performs as expected and adheres to their guidelines.</li>
<li>When your app passes the review process Apple re-signs the app with their security certificate which means it can run on any iOS device and is ready to be published in the App Store.</li>
</ol>
<h3 id="what-is-a-provisioning-profile">What is a provisioning profile?</h3>
<p>As you can imagine Apple needed to come up with a way to manage all of the above. This is where provisioning profiles come into play. A provisioning profile is a collection of different components that the operating system needs to decide if an app should be allowed to run on a particular device or not. A provisioning profile allows us to test and debug our apps on our devices and distribute our app to beta testers or the app store. Provisioning profiles contain a lot of metadata and its four most important components are:</p>
<ol>
<li>The App ID</li>
<li>A signing certificate issued by Apple</li>
<li>List of devices (UDID) that the app is allowed to run on</li>
<li>Special entitlements the app is allowed to use like (e.g. push notifications or health kit)</li>
</ol>
<h4 id="1-app-id">1. App ID</h4>
<p>iOS identifies our applications through the App Bundle ID. It is very important that the App Bundle ID always matches the App ID in the provisioning profile. The App bundle ID has a couple of restrictions:</p>
<ul>
<li>Reverse DNS Format e.g. com.companyName.appName</li>
<li>Can only contain alpha-numeric characters, hyphens (-) or periods (.)</li>
<li>Special characters (like $, !, @, etc) are not allowed</li>
</ul>
<h4 id="2-certificates">2. Certificates</h4>
<p>Before I will talk about certificates, I will briefly touch on a subject called public-key cryptography which plays an important role in code signing. As a self-taught developer this was quite a new subject for me, but if you already know what this is then feel free to skip this part.</p>
<p>Public key cryptography, or asymmetric cryptography is a cryptography system that uses a pair of keys. That is a public key paired with a private key which is only known to the owner. With the public key you can authenticate that a message originated with a holder of the paired private key, or you can encrypt a message with the public key to ensure that the holder of the paired private key can decrypt it.</p>
<p>For example when you send a message over HTTP you can digitally sign this request to authenticate that the message was send by you, and has not been changed along the way. You do this by sending the message + the message encrypted by the private key. When the receiver receives the message he or she hashes the message with the public key and then compares it with the hash from the encrypted message. If they are the same then he or she knows for sure that the message comes from you and has not been changed by someone in the middle.</p>
<p>As I said earlier certificates play an important role in public key cryptography. They are issued by a certificate authority which is a trusted entity that issues digital certificates that verify a digital entity’s identity on the internet. They contain a lot of metadata like the owner’s name, the expiration date etc. iOS maintains a list of trusted CA root certificates to verify that a CA has issued and signed. Apple signs its developer certificates with a root certificate issued by the Apple WorldWide Developer Relations CA.</p>
<p>With a distribution certificate Apple uniquely identifies you as a developer or publisher of an app. The private key is used to sign the application. There are two types of signing certificates. First we have the developer certificate which allows for constant redeployment to our test device and debugging in Xcode. Production certificates are used for production builds (App Store) or QA builds that get distributed through e.g. Testflight. It is through this certificate that Apple identifies you as an App Store publisher or test distributor. Therefore it can only be used with a provisioning profile for production.</p>
<p>After the app is compiled it is signed with the private key of your distribution certificate. The private key must match the public key of the certificate in the provisioning profile. This tells Apple that you are the one who compiled the app, and not someone pretending to be you. Of course if the pair is invalid the app wont be installed and executed.</p>
<h3 id="3-udid">3. UDID</h3>
<p>UDID, short for Unique Device Identifier, uniquely identifies an app on your device. Don’t confuse this with UUID which stands for Universally Unique Identifier and identifies an app on a device.
A provisioning profile contains a list of UDID’s of the devices on which the app is allowed to run. Whenever an App is signed with Apple’s security certificate it can be published in the app store and installed on any device.</p>
<h3 id="4-entitlements">4. Entitlements</h3>
<p>Because iOS applications are sand-boxed they can not directly interact with system services.
Entitlements are permissions or capabilities for when an app needs to interact with system services like push notifications. They specify what to allow and what to deny your application. It is important that the entitlements in your app always match those in your provisioning profile.</p>
<p>I hope that you enjoyed this article and now understand enough about provisioning profiles to work with them and fix any issues that occur while using them. Because even though they can be very frustrating to work with, they are very important for keeping the iOS platform secure.</p>Before I got my first job as an iOS Developer I did not know much about code signing and provisioning profiles. As a member of Apple’s free developer program I mostly used the simulator and occasionally ran my app on my device. Whenever I would run into problems I would rely on Xcode to fix them for me. But after a while I quickly found out that it usually is better to solve the problem yourself instead of relying on Xcode. To gain a better understanding of what I was actually doing I did some research on code signing and provisioning profiles and I wrote it down as a reference to myself and everyone else who is interested in it.Learning about generics Swift2016-08-11T00:00:00+02:002016-08-11T00:00:00+02:00https://kairadiagne.com/2016/08/11/Generics-in-Swift<p>As a developer I really enjoy writing clear, readable and reusable code. Lately I have been learning more about Swift’s generics and how it can make my code more flexible and reusable. In this post I will explain to you the concept of generics and give you some examples of how I use this nice feature of Swift in my projects.</p>
<h3 id="what-are-generics">What are generics</h3>
<p>Generics allow us to create types and functions that can work with any type. Generic code is flexible and reusable and makes it easier for us to avoid code duplication. Generics are a very important feature of Swift. Even if you’re new to Swift and have never created a generic type or function, you have used generics. This is because a big part of the Swift standard library is written with the help of generics, for example Swift’s array. Because in Swift an array can hold any type, whenever we initialize a new array we always have to specify which type it will hold.</p>
<h3 id="generic-types">Generic types</h3>
<p>Generic types are custom classes, structs or enums that can work with any type, in the same way as Swift’s Array and Dictionary allow us to do that.</p>
<p>Example:</p>
<p>Let say we are developing a music app which displays a list of popular songs, popular artists and upcoming concerts which we fetch from a backend from a popular music site. These lists can be quite long so we get a paginated response from the backend. A request for a list looks something like this:</p>
<p>[“page”: 1,
“totalPages”: 20,
“totalItems”: 400,
“items”: [Aryay of dictionaries]
]</p>
<p>Okay so now we have to think about how we want to create our model objects. Besides creating objects for a song, an artist and a concert we also need an object that represents a list. This could be a list of songs, artist, concerts or if later in the development process we decide that we also want to display a list of albums we should be able to use the same List object. Thats why this object will be a generic type. This will look something like this:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">struct</span> <span class="kt">List</span><span class="o">&lt;</span><span class="kt">Item</span><span class="o">&gt;</span> <span class="p">{</span>
<span class="k">var</span> <span class="nv">page</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">maxPages</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">maxItems</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">Item</span><span class="p">]?</span>
<span class="p">}</span>
</code></pre></div></div>
<p>In this example Item is the generic parameter. Every time you initialize a generic object you need to specify which actual type to use in place of the generic parameter, just like you would do with an array or a dictionary. You can provide more than one generic parameter by writing multiple type parameter names within angle brackets, separated by commas. It is a best practice to give these parameters a descriptive name to tell readers of our code about the relationship between the generic parameter and the generic type. When this relationship does not exist you should name them using single letters such as T, U or V. Another best practice is to always use upper case names to indicate that they are a placeholder for a type and not a value.</p>
<p>Right we could pas any type of object inside of our list model and it works. Thats nice, however what if we want to enforce constraints on the types that can be used with our generic List object. In our music app we for example might want to cache the data on the disk with NSKeyedArchiver. For this we normally would use NSCoding to encode and decode our object but unfortunately NSCoding does not work with Swift structs. To solve this problem we create the following protocol which all our model objects that we want to save to disk need to conform to:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">Protocol</span> <span class="kt">DictionaryPresentable</span> <span class="p">{</span>
<span class="nf">init</span><span class="p">?(</span><span class="nv">dictionary</span><span class="p">:</span> <span class="kt">NSDictionary</span><span class="p">)</span>
<span class="kd">func</span> <span class="nf">dictionaryRepresentation</span><span class="p">()</span> <span class="err">→</span> <span class="kt">NSDictionary</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Now we can add a type constraint to the generic parameter of our List object. Type constraints specify that a generic parameter must inherit from a specific class, or conform to a particular protocol or protocol composition. Our List object now looks something like this:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">struct</span> <span class="kt">List</span><span class="o">&lt;</span><span class="kt">Item</span><span class="p">:</span> <span class="kt">DictionaryRepresentable</span><span class="o">&gt;</span><span class="p">:</span> <span class="kt">DictionaryRepresentable</span> <span class="p">{</span>
<span class="k">var</span> <span class="nv">page</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">maxPages</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">maxItems</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span>
<span class="k">var</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">Item</span><span class="p">]?</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="generic-functions">Generic functions</h3>
<p>A generic function is a function that can work with any type. Just as with generic types a generic function has a generic parameter or placeholder and the actual type to use in place of this parameter will be determined each time the function gets called.</p>
<h3 id="associated-types">Associated Types</h3>
<p>You can make protocols generic by declaring one or more associated types as part of the protocol’s definition. An associated type gives a placeholder name to a type that is used as part of the protocol.
Whenever the protocol is adopted you declare which actual type(s) to use for the associated type(s) in that protocol. In other words the protocol does not know the exact type so every class, struct, enum that adopts that protocol should fill in the details. So working with generics in protocols does not seem very different from working with generics in classes and structs. However there is one important difference. Instead of that the type gets specified when we instantiate a class or a struct with associated types it gets specified when we adopt a protocol, which hides this for the outside world. I do not have a lot of experience with associated types but I did use it in a project which was heavy on table views and collection views.For this I created a ‘dataprovider’ protocol which contains basic properties and functions that I want in my UITableViewDataSource and UICollectionViewDataSource objects and gave it a default implementation. This looked something like this:</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">protocol</span> <span class="kt">DataProvider</span><span class="p">:</span> <span class="kd">class</span> <span class="p">{</span>
<span class="kd">associatedtype</span> <span class="kt">Item</span><span class="p">:</span> <span class="kt">Mappable</span>
<span class="kd">associatedtype</span> <span class="kt">Cell</span>
<span class="k">var</span> <span class="nv">cellIdentifier</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
<span class="k">var</span> <span class="nv">itemCount</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
<span class="k">var</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">Item</span><span class="p">]</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
<span class="kd">func</span> <span class="nf">itemAtIndex</span><span class="p">(</span><span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Item</span><span class="p">?</span>
<span class="kd">func</span> <span class="nf">updateWithItems</span><span class="p">(</span><span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">Item</span><span class="p">])</span>
<span class="p">}</span>
<span class="kd">extension</span> <span class="kt">DataProvider</span> <span class="p">{</span>
<span class="k">var</span> <span class="nv">cellIdentifier</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span>
<span class="k">return</span> <span class="kt">Cell</span><span class="o">.</span><span class="nf">defaultIdentifier</span><span class="p">()</span>
<span class="p">}</span>
<span class="k">var</span> <span class="nv">itemCount</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">items</span><span class="o">.</span><span class="n">count</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">itemAtIndex</span><span class="p">(</span><span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Item</span><span class="p">?</span> <span class="p">{</span>
<span class="k">guard</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">index</span> <span class="o">&lt;=</span> <span class="n">itemCount</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">nil</span> <span class="p">}</span>
<span class="k">return</span> <span class="n">items</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">updateWithItems</span><span class="p">(</span><span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">Item</span><span class="p">])</span> <span class="p">{</span>
<span class="k">self</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>That’s it. I hope you learned enough about generics so you can start using this powerful feature of Swift in your own projects.</p>As a developer I really enjoy writing clear, readable and reusable code. Lately I have been learning more about Swift’s generics and how it can make my code more flexible and reusable. In this post I will explain to you the concept of generics and give you some examples of how I use this nice feature of Swift in my projects.Error Handling in Swift2016-04-12T00:00:00+02:002016-04-12T00:00:00+02:00https://kairadiagne.com/2016/04/12/Error-handling-in-Swift<p>Last year at WWDC Apple introduced a new syntax for handling errors. Lately I have noticed that sometimes I still get a bit confused about how this works so I figured it would be nice to learn more about this subject by writing a blog post.</p>
<h3 id="type-of-errors">Type of errors</h3>
<p>When an iOS or OSX application is running two different types of problems can arise, exceptions and errors. Exceptions are bugs in our program that warn us that an unexpected condition occurred and needs to be fixed e.g. when your program unexpectedly finds nil when unwrapping an optional value. Exceptions cause applications to crash so we want to avoid having these in our production code at all costs.</p>
<p>Errors are expected problems at the user-level like e.g. trying to load a file that doesn’t exist. Because they are expected during the normal execution of a program we should check for these kind of conditions and inform the user when they occur.</p>
<h3 id="how-it-was-done-in-the-past">How it was done in the past</h3>
<p>In the Objective-C days error handling was done through a mechanism called pass by reference. When we would call a method that could fail we would pass in an indirect reference to an <code class="highlighter-rouge">NSError</code> object as a parameter, and if the method would fail this reference would get populated with an <code class="highlighter-rouge">NSError</code> containing the error details. In the early days of Swift this was achieved by passing an optional instance of <code class="highlighter-rouge">NSError</code> to an <code class="highlighter-rouge">NSErrorPointer</code> parameter.</p>
<p>In those times it was easy to forget to check the error, or if you didn’t care about why a method failed you could just pass NULL or nil for the error argument and the error would be ignored. Since one of the core values of Swift is to make it easy to write safe code by default a new error handling syntax was introduced. This syntax makes it clear when we can expect an error and makes sure we handle those errors.</p>
<h3 id="do-try-and-catch">do, try and catch</h3>
<p>If we want a method to throw an error we need to create a list of all the possible errors that can occur when executing this method. We do this by creating an <code class="highlighter-rouge">enum</code> that represents our type of error. This enum must conform to the <code class="highlighter-rouge">ErrorType</code> protocol.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Step 1: Create an error</span>
<span class="kd">enum</span> <span class="kt">TitleError</span><span class="p">:</span> <span class="kt">ErrorType</span> <span class="p">{</span>
<span class="k">case</span> <span class="kt">Empty</span>
<span class="k">case</span> <span class="kt">Short</span>
<span class="k">case</span> <span class="kt">Long</span>
<span class="p">}</span>
</code></pre></div></div>
<p>The next step is to write the method that might fail. Here we can use the <code class="highlighter-rouge">guard</code> statement to check for errors. If the condition is not met we throw an error.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Step 2: Write a method that throws</span>
<span class="kd">func</span> <span class="nf">saveTitle</span><span class="p">(</span><span class="nv">title</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="k">throws</span> <span class="p">{</span>
<span class="c1">// Check if title is valid</span>
<span class="k">guard</span> <span class="n">title</span><span class="o">.</span><span class="n">characters</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="p">{</span> <span class="k">throw</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Empty</span> <span class="p">}</span>
<span class="k">guard</span> <span class="n">title</span><span class="o">.</span><span class="n">characters</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;=</span> <span class="mi">8</span> <span class="k">else</span> <span class="p">{</span> <span class="k">throw</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Short</span> <span class="p">}</span>
<span class="k">guard</span> <span class="n">title</span><span class="o">.</span><span class="n">characters</span><span class="o">.</span><span class="n">count</span> <span class="o">&lt;=</span> <span class="mi">40</span> <span class="k">else</span> <span class="p">{</span> <span class="k">throw</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Long</span> <span class="p">}</span>
<span class="c1">// Save the title</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Now that we created an error and a method that can fail it is time to call this method. When we call a method that can fail we have to precede it with the <code class="highlighter-rouge">try</code> keyword, otherwise our code won’t compile. The <code class="highlighter-rouge">try</code> keyword makes it instantly clear to the readers of our code that this method can fail.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">try</span> <span class="nf">saveTitle</span><span class="p">(</span><span class="n">someTitle</span><span class="p">)</span>
</code></pre></div></div>
<p>If you are certain that an error will never happen you can use <code class="highlighter-rouge">try!</code>. This communicates that you are aware there is a theoretical possibility of this call failing, but you are certain this is not going to happen in your case. If the method does fail your app will crash.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">try!</span> <span class="nf">saveTitle</span><span class="p">(</span><span class="n">someTitle</span><span class="p">)</span>
</code></pre></div></div>
<p>Ok nice, now we made sure that everyone who reads our code will know that this method can fail, but we are still not handling the error and therefore our code still won’t compile. For this we use the <code class="highlighter-rouge">do-catch</code> statement. We wrap the function that throws in the <code class="highlighter-rouge">do</code> block and then catch errors with <code class="highlighter-rouge">catch</code> blocks.</p>
<div class="language-swift highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">do</span> <span class="p">{</span>
<span class="k">try</span> <span class="nf">saveTitle</span><span class="p">(</span><span class="n">someTitle</span><span class="p">)</span>
<span class="c1">// Do some other stuff</span>
<span class="c1">// If saveTitle fails this code will not be executed</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Empty</span> <span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"Error: Save failed because the title is empty"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Short</span><span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"Error: Save failed because the title is too short"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="kt">TitleError</span><span class="o">.</span><span class="kt">Long</span> <span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"Error: Save failed because the title is too long"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"Error: Save failed because of an unknown reason"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Swift doesn’t know which specific error our method might throw so to make the statement exhaustive we provide a catch all block. This makes our code safer because now we made sure we will catch all possible errors. Now when we run our program and our method fails it will immediately skip to the <code class="highlighter-rouge">catch</code> blocks and handle the error.</p>
<p>Sometimes we are not interested in the details of the error but we just want to know if the call failed or succeeded. For this case we have the <code class="highlighter-rouge">try?</code> keyword. If we use try? the error is handled by returning an optional that doesn’t contain a value. Therefore there is no need to wrap the method that throws in a <code class="highlighter-rouge">do-catch</code> statement. This can be used in combination with optional binding.</p>
<p>So that’s how error handling is done in Swift 2. If you would like to see a more extensive example I advise you too watch <a href="https://developer.apple.com/videos/play/wwdc2015-106/?time=1700">this</a> WWDC talk about new features in Swift 2.0.</p>Last year at WWDC Apple introduced a new syntax for handling errors. Lately I have noticed that sometimes I still get a bit confused about how this works so I figured it would be nice to learn more about this subject by writing a blog post.My experience at MDevcon 20162016-04-01T00:00:00+02:002016-04-01T00:00:00+02:00https://kairadiagne.com/2016/04/01/MDevcon-2016<p>A while ago I stumbled upon an extremely handy website pomo.tv, which gives an overview of talks given at various iOS/MAC/Swift conferences throughout the world. I really enjoy watching these talks so when I heard about MDevcon, a conference in my hometown Amsterdam, I got very excited and signed myself up for both days of the conference. This post is a write up of some of my experiences in those two days.</p>
<blockquote>
<p><strong><em>When thinking about architecture you should not think about rules but more about principles. Not every project is the same so we can not use the same approach all the time.</em></strong></p>
</blockquote>
<p>The first day, tutorial day, I attended an interesting session by Jorge D. Ortiz-Fuentes about clean architecture of iOS Apps. The clean architecture approach involves several layers of abstractions and strong adherence to the single responsibility principle and dependency inversion principle which leads to a very decoupled system. Some advantages of this approach are:</p>
<ul>
<li>Highly testable code: Test the business rules without the UI, Web Server etc.</li>
<li>Independent of UI (e.g. easily switch out a UITableView for a UICollectionView)</li>
<li>Independent of service and database (e.g. easily move from parse to lets say Firebase)</li>
</ul>
<p>This was a interesting session which made me think a lot about a how I architecture my own apps. Since it’s quite an extensive subject to cover in just a few hours we only scratched the surface of it but I would really like to try this out in a future project.</p>
<blockquote>
<p><strong><em>You are not working for other designers you are working for the people who have no idea what it is you do, don’t care about what you do, but just want an app that works so they can go on with their lives</em></strong></p>
</blockquote>
<p>Conference day started with a talk from designer, developer and also member of the band airplane mode Joseph Cieplinski. His talk was about how he feels that a lot of designers design to impress other designers. His advise to not only designers but also developers was to take more time to learn from other people instead of competing with them, because in the end of the day its not about those people you compete with but about the group that you are building this product for, the user!</p>
<p>Later that day Marin Todorov, iOS Dev and author at raywenderlich.com, talked about how in the past year he has become a better developer by spending more time away from the computer. A lot times we are scarred that if we spend less time on our craft we will fall behind, which is not always true. Marin for example decided to spend more time away from the computer by picking up cooking and long distance running.
What he noticed is that by focusing on something completely else he gave his mind the chance to process the things he was working on. Therefore when he would come back to his code he would have a better understanding of how the problems he was facing could be solved. A lot of the things he talked about are not new concepts but things we tend to forget sometimes in our everyday lives. That’s why I think it is good to remind ourselves of this every once in while.</p>
<blockquote>
<p><strong><em>Being open source is the exact opposite of sitting in the corner of the office with your screen aimed towards the wall. In that spot very bad code is born!</em></strong></p>
</blockquote>
<p>Something else he talked about is about becoming more ‘open source’. With this he means that we always should look for feedback on our work and our projects. We might find out that some of the things we are very proud of can be done better in a different way, but if we would have not gotten that feedback we would have missed an opportunity to learn and get better. Marin found this out himself when he was writing an framework for fast JSON parsing. First he had not shared it with anyone and he was feeling very confident about his code. But when he proudly shared it with other developers he found out that his code wasn’t as great as he thought it was. But after all the feedback his project got so much better and eventually he was able to release a great framework. His advice was to look for feedback early and to not only look for feedback within your own community. For example try to talk to an android engineer and instead of debating which platform is better try to find out how they would approach their problems and get things done.</p>
<p>Other talks that I liked a lot where ‘Unit testing and mocking in swift’ by Ben Asher, ‘Asynchronous NSOperations at your command’ by Andrew MCKnight and ‘Hello Watch OS 2” By NatashaTheRobot. Overall it were to amazing days full of interesting talks which I learned a lot from. It was great meeting so many people with the same interests from all this different countries and it inspired me to get better. I hope to attend another conference soon.</p>A while ago I stumbled upon an extremely handy website pomo.tv, which gives an overview of talks given at various iOS/MAC/Swift conferences throughout the world. I really enjoy watching these talks so when I heard about MDevcon, a conference in my hometown Amsterdam, I got very excited and signed myself up for both days of the conference. This post is a write up of some of my experiences in those two days.Learning how to code - My Story2015-11-19T00:00:00+01:002015-11-19T00:00:00+01:00https://kairadiagne.com/2015/11/19/Learning-how-to-code<p>In the beginning of 2015 I promised myself that I was going to learn how to code. When I was in college I had played around a bit with HTML and CSS, so I decided continuing that would be a good way to start. After the first period where a lot of what I was doing didn’t make sense to me at all, things started to click and I started to develop a passion for coding.</p>
<h3 id="endless-possibilities">Endless possibilities</h3>
<p>Everyday it felt like I was being exposed to a whole new world. I became increasingly exited with the possibilities of how much further I could go, but at the same I started to feel a bit lost. I realised there is so much to learn and it’s impossible to know everything or learn everything at once. As a result I found myself asking google things like ‘Which language to learn first’ or ‘Where to start: Web or Mobile’.</p>
<h3 id="time-to-focus">Time to focus</h3>
<p>I realised that in order to become good at something you sometimes need to narrow your focus a bit. So I asked myself one simple question that sounded something like: from everything you have experimented within the last couple of months, what has intrigued you the most? The answer was developing apps for smartphones, because I just find it so cool that you can create something for a device that people have with them almost 24/7. Since I had been a avid user of the iPhone and iPad, the choice of starting with iOS development was easily made. This is when the real journey began.</p>
<h3 id="why-im-starting-this-blog">Why I’m starting this blog</h3>
<p>Now a little more than 6 months into this journey I’m starting this blog mainly to keep me motivated and to become a better developer by sharing what I learn. My goal is to not only become a better developer, but I also hope to inspire others to try out iOS Development and hopefully become as passionate about it as I am along the way.</p>In the beginning of 2015 I promised myself that I was going to learn how to code. When I was in college I had played around a bit with HTML and CSS, so I decided continuing that would be a good way to start. After the first period where a lot of what I was doing didn’t make sense to me at all, things started to click and I started to develop a passion for coding.