The iOS file system in-depth (and how to be the best using critical thinking)

Have you ever wondered how all those people out there figured out how to manipulate the iOS file system in their apps? For some strange reason, Apple has never provided well-organized documentation on the subject. Here’s how I feel: “Ask and simple question and get an obtuse and overly complex answer.” There are many articles and tutorials out there, including my own, showing you examples of Objective-C or Swift code for manipulating the iOS file system, and most of the code looks basically the same. Nonetheless, this code is deceivingly complex, often underestimated, and rarely well-explained or well-understood.

Where did everybody find this boilerplate code? From simple observation, I’ve found that in many cases, developers use a copy and paste methodology, i.e., look up a few keywords in a web search engine, find the code needed on sites like StackOverflow or some blog, copy it, paste it into an Xcode project, and beat on it until it works. I don’t want you to feel this way after reading my tutorials.

I hope you’ll find it edifying and interesting to read about how I figured out how to understand and navigate the iOS file system using the “most of the code looks basically the same” boilerplates. But I bet you’ll find it even more intriguing to find that I’ve discovered an much better alternative to the boilerplate code.

Continue reading “The iOS file system in-depth (and how to be the best using critical thinking)”

Controlling chaos: Why you should care about adding error checking to your iOS apps

NOTE: The second installment of this article, “Controlling chaos: Error Handling in Swift 4 with do, try, catch, defer, throw, throws, Error, and NSError,”, has just been released.

In this tutorial, the first in a series of tutorials, we’re going to discuss the arduous topic of looking for unexpected values, events, and conditions that arise during program execution, using a technique I like to call “error checking.” Today, I’ll concentrate on the reasons why you should check for errors. I’ll mention a number of techniques I use but leave detailed discussion of those techniques and sample code to subsequent articles. The purpose of this tutorial is to convince you to make use of error checking in your apps. You ignore errors at your own dire peril. This is sink or swim. If you put out a crappy app, no one’s going to use it because you’ll get a bad reputation at Internet speed, and employers/customers will be more than happy to leave you behind forever for other app developers who aren’t too lazy to write quality code.

Continue reading “Controlling chaos: Why you should care about adding error checking to your iOS apps”

Xcode tidbits: Open as Hex, Open As Source Code, (Git) line endings, and text encoding

With this article, I’m starting a series about all the goodies — useful tools — that can be found in Xcode. Some of these tidbits are tools everyone knows about while others are barely documented to undocumented. For example, how many of you know that you can view, inspect, and debug all your Auto Layout constraints live during app execution using the “Debug View Hierarchy” Xcode feature? I discussed that feature in detail in this article, “Troubleshooting Auto Layout using Xcode’s Debug View Hierarchy.” Today, we’ll discover two editors that ship with Xcode, the “Open As > Hex” and “Open As > Source Code” editors, both only available by right-clicking on files in the “Project Navigator” to reveal a contextual menu.

Continue reading “Xcode tidbits: Open as Hex, Open As Source Code, (Git) line endings, and text encoding”

Swift 3 segues, unwind segues, storyboards, and view/navigation controllers

[Download Xcode 8.2.1 project with full Swift 3 source from GitHub.]

Today’s tutorial covers transitions — segues — from one source storyboard scene to another destination scene, and unwind segues leading back from destination to source… I created a project to help you follow along with this tutorial, written in Swift 3, against the iOS 10 SDK, and using the Xcode 8.2.1 IDE. Please download the project. The app produced by the project is shown in action in the following video. Please watch before continuing on:

Segues don’t exist in a vacuum. I’ve introduced a UINavigationController into the mix. Of course, you’ll see a few UIViewControllers. I’ve also used a UITableView and managed its complexity by breaking it into logical pieces by using Swift “extensions.” As you proceed, you’ll have to grasp concepts like Auto Layout and managing a table view’s data source.

Continue reading “Swift 3 segues, unwind segues, storyboards, and view/navigation controllers”

Oh, my – App Transport Security has blocked a cleartext HTTP (https://) resource load since it is insecure

App Transport Security (ATS) is enabled by default for apps linked against the iOS 9.0 or OS X v10.11 SDKs or later, as indicated by the default Boolean value of NO for the NSAllowsArbitraryLoads key. This key is at the root level of the NSAppTransportSecurity dictionary.

With ATS enabled, HTTP connections must use HTTPS (RFC 2818). Attempts to connect using insecure HTTP fail. ATS employs the Transport Layer Security (TLS) protocol version 1.2 (RFC 5246). For background on secure Internet connections, read HTTPS Server Trust Evaluation.

Apple, Information Property List Key Reference

With the advent of iOS 9, Apple decided that developers should avoid accessing insecure, unencrypted clear text HTTP (https://) resources on the Internet. Today I’ll show you how to access HTTP sites/services in your apps. I’ll explain the special hoops that Apple wants you to jump through just to use HTTP — and help you keep your app from being rejected.

For Apple to assume that anything HTTP is dangerous is a bit overboard as there are legitimate reasons to access a resource via clear text, like downloading an image (clear binary). Grabbing an image won’t reveal information about users’ private lives. A web/REST service that consumes someone’s name and Social Security number is a different story — that info must be encrypted.

Fortunately, Apple has made some accommodations in allowing continued use of HTTP as long as you provide “justification” when submitting your apps.

Continue reading “Oh, my – App Transport Security has blocked a cleartext HTTP (https://) resource load since it is insecure”

Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 3

[Download the full Swift Xcode project from GitHub.]
[Download the full Objective-C Xcode project from GitHub.]


UPDATE: I’ve updated this article for Swift 4, learned a few new tricks, and taken advantage of the Swift 4 compiler’s “intelligence.” Please check out the new version as it’s more comprehensive and detailed, and my source code has been highlighted and commented to better help you understand the sometimes confusing concept of parallelism. There’s a brand new Xcode companion project, too.


Today, I’m going to start answering some of the concurrency questions I asked you to ponder in yesterday’s post entitled “Concurrency in iOS — Grand Central Dispatch (GCD) with Swift 3.” Specifically, I’m going to write some code in Swift 3 and Objective-C showing you the difference between serial and concurrent queues. But before coding we’ll talk about concurrency in general, the terminology used in discussing concurrency (threads, process, and tasks), the differences between the terms “concurrent” and “parallel,” the differences between serial and concurrent queues, the differences between synchronous and asynchronous methods/functions, and finally we’ll wrap up with some more definitions you need to know about.

Continue reading “Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 3”

Concurrency in iOS – Grand Central Dispatch (GCD) with Swift 3

[Download the full Swift Xcode project from GitHub.]
[Download the full Objective-C Xcode project from GitHub.]

I use concurrency in most of my iOS apps, generally to keep the user interface responsive. For those of you new to iOS and/or new to computer science, “Concurrency is the notion of multiple things happening at the same time.” (We can discuss the old “What’s the difference between concurrency and parallelism?” question later.) I’m constantly being asked questions about how to implement concurrency in iOS and, most recently, I’ve received many questions about how to implement it in Swift 3. Here’s a video showing this post’s concurrent Swift code running — and notice that a UIProgressView is updating while images are downloading in the background; notice also that I’m continually pressing a button that increments an instance variable during background processing:

(Make sure you stay with me to complete this discussion of iOS concurrency in the next post. I’ll write some code in Swift 3 and Objective-C showing you the difference between serial and concurrent queues. Before coding we’ll talk about concurrency in general, the terminology used in discussing concurrency (threads, process, and tasks), the differences between the terms “concurrent” and “parallel,” the differences between serial and concurrent queues, the differences between synchronous and asynchronous methods/functions, and finally we’ll wrap up with some more definitions you need to know about.)

The questions about implementing GCD in Swift 3 have come from beginners to intermediate- to even advanced-level developers. For years, the most widely used iOS construct for concurrency — “starting tasks asynchronously” — has been Grand Central Dispatch (GCD). Most iOS old-timers like me have gotten used to GCD’s Objective-C language version of the “dispatch_async” function:

Continue reading “Concurrency in iOS – Grand Central Dispatch (GCD) with Swift 3”

Xcode secrets: save time with context-sensitive help and documentation

How many times have you been looking at Swift or Objective-C code in Xcode and can’t remember what a framework method, argument, constant, etc. means? Did you know that help — full documentation — is just a keystroke away? Did you also know that you can add the same type of pop-up, context-sensitive help to your own code? Here’s how. Let’s say you’re looking at the following NSString class method and can’t remember exactly what the call does, what parameters it takes, and what is its return value:

Highlight the method name, parameter, even the enum, and then press the following key combination on your keyboard:

[command] + [control] + [shift] + [?]

      or, using key symbols:

⌘ ⌃ ⇧ ?

Here’s what you’ll see: a context-sensitive help/documentation popup. Note that I added the red lines to highlight content. The red highlighting is not what Xcode provides (click to enlarge):

Figure 1: Xcode framework context sensitive help and documentation.

So you immediately get information about the method, parameter, enum, even constant’s:

  • Declaration (formal language signature);
  • Description (textual explanation of the entity’s purpose);
  • Parameters (a full list of names and definitions);
  • Returns (the value returned by a method/function, if applicable);
  • Availability (what version of iOS that the entity became available in — and sometimes in what version it was deprecated);
  • Declared In (the framework which contains the entity); and,
  • More (generally, the formal definition of the entity with explanations and other links).

Check out what I get if I click on the “More Type Method Reference” link as shown in Figure 1 (click to enlarge):

Continue reading “Xcode secrets: save time with context-sensitive help and documentation”

Using Swift extensions to manage complexity, improve readability, and increase extensibility (UICollectionView, protocols, and delegates)

[Download the full Xcode project from GitHub.]

Today, I’m going to show you how to leverage the Swift “extension” language feature to manage software complexity, improve code readability, and increase extensibility. We’ll also talk about delegates, data sources, and protocols as they are concepts essential to this tutorial. According to Apple’s “The Swift Programming Language (Swift 3.0.1):”

Continue reading “Using Swift extensions to manage complexity, improve readability, and increase extensibility (UICollectionView, protocols, and delegates)”

UPDATE: The UICollectionView is much more than a grid or matrix

Get started by [downloading the full Xcode project from GitHub.]

Let’s talk about the UICollectionView, a rich, configurable, and powerful iOS user interface component. I will write code in Swift 3.0 to create a UICollectionView to which I can add, select/highlight, deselect/unhighlight, and remove UICollectionViewCell’s. I’ll show you that I can select, deselect, add, and remove one cell at a time, or multiple cells at a time. And most importantly, I’ll demonstrate the importance of the relationship between a UICollectionView and its data source, and the importance of keeping a UICollectionView and its data source synchronized. You can download my entire Xcode 8.2.1 project. Feel free to reuse my code as long as you follow the terms of the license agreement. Today, we’re going to build a basic but completely functional instance of the UICollectionView in Swift 3.0 as illustrated by the following video:

Continue reading “UPDATE: The UICollectionView is much more than a grid or matrix”