Amro Mousa

iOS developer, BMW fan, and amateur photographer.

Read this first

Improving Swift Debug Build Times

It’s well known that enabling Whole Module Optimization greatly reduces Swift build times, but did you know it’s possible to do this for debug builds without breaking the debugger? I didn’t.

Normally, one sets SWIFT_OPTIMIZATION_LEVEL to -Onone for development builds. Otherwise, the debugger will behave in ways that are expected but not helpful.

I wanted the build-time gains that come from enabling Whole Module Optimization without breaking the debugger. After some trial/error, I tried setting SWIFT_WHOLE_MODULE_OPTIMIZATION to YES and, to my surprise, it worked. And the debugger still worked in my testing.

This reduced debug build times by approximately 30% on a project I contribute to.

You might be run into an interesting compiler segmentation fault after enabling SWIFT_WHOLE_MODULE_OPTIMIZATION. If you do, check the build log for a stack trace that mentions SIL optimization

Continue reading →

Implementing Rich Notifications in iOS 10

I’ve spent a bit of time with notification extensions in iOS 10 and they’re pretty great. If you’re not familiar with notification extensions, check out Apple’s Introduction to Notifications talk before reading this post. Anywho, notification extensions improve your app’s notification experience giving you a chance to mutate notification payloads, download and display media, and show custom interfaces in response to notifications. I thought I’d share a bit about how they work and a few things I ran into while adding notification extensions to an existing app.

Before we dive in, I should mention that iOS 10 brings other fancy notification enhancements like aggregation, a way to handle notification actions in an extension, and the ability to opt into system notification banners for your application while it is open. I’ve chosen to focus on processing and displaying notifications in this

Continue reading →

Fugly Side Project ???

For the last few months, I’ve spent many of my evening hours working on a side project. Usually that ends with my solving whatever interesting problems there are and moving on to something else. But this time it stuck. I built a Fugly Side Project called Dog Ear that tells you when your servers go down.

I’ve used uptime monitoring services in the past and they all seemed to send false positive alerts too often. At least often enough to bother me. So the implicit goal of Dog Ear is to do less of that. Hopefully much less of that.

Today Dog Ear is light on features. For instance, it doesn’t support multi-region monitoring, though that’s something I’d like to add. And although the web app is responsive, there’s no iPhone or Android app. Alerts are sent via SMS and email thanks to a couple of reliable providers and I have also farmed out credit card processing to Stripe. It’s easier for me

Continue reading →

Communicating with WCSession

WatchKit on watchOS 2 brings major improvements to how Apple Watch apps communicate with their host app. Gone is openParentApplication(_:reply:), which has been replaced by the more capable WCSession.


WCSession allows WatchKit apps and their counterpart application to communicate, bidirectionally, in three ways: an updatable Application Context, live messages, and background transfers. The two later communication methods can either send a dictionary of values or raw data.

 Application Context

The Application Context is a dictionary that’s sent to the counterpart and persisted. The dictionary can then be read when the counterpart, whether that’s the WatchKit app or the host app, wakes up.

Updating the Application Context is simple. Just call updateApplicationContext(_:). This replaces the prior context value visible to the counterpart application. Note that updateApplicationC

Continue reading →

Watch Extension Lifecycle

One useful addition brought by watchOS 2.0 is the WKExtensionDelegate protocol, which exposes lifecycle events and more. This protocol is similar to UIApplicationDelegate.

WKExtensionDelegate has three methods that let you respond to lifecycle events. The lifecycle methods will seem familiar: applicationDidFinishLaunching(), applicationDidBecomeActive(), and applicationWillResignActive().

There are also several methods you can implement to handle notifications.

And finally, you can implement handleUserActivity() to facilitate Handoff.

Continue reading →

Building  Watch Apps

I’ve been tinkering with WatchKit in my spare time and thought I’d share a bit of what I’ve learned. Of course, all of my tinkering was done in the simulator as I’ve not actually played with an  Watch.

 Watch Apps Have Up To Three Components

Watch apps contain an application, an optional glance, and optional custom notifications.

Glances are sort of like Today widgets on iOS. The idea is to show the most important bit of information your app provides. For example, CNN might display breaking news. The Watch may create your glance controller some time before the user sees it so it’s important to make sure the information displayed is up to date in willActivate(). Glances can be enabled or disabled by the user, don’t support scrolling, and allow for limited interactivity. Tapping a glance opens your Watch application.

Notifications are baked in with a default look but you can choose to

Continue reading →

More on Lazy Initialization in Swift

@ColinEberhardt wrote a great post titled Swift Initialization and the Pain of Optionals. In the post, Colin mentions not being able to pass parameters to initializers when assigning to a variable marked lazy. The workaround he found was to use a closure. Colin gave two examples:

lazy private var animator = UIDynamicAnimator(referenceView: self.view)

which did not compile and:

lazy private var animator: UIDynamicAnimator = {
    return UIDynamicAnimator(referenceView: self.view)

which did compile. @schukin noticed that Colin also added an explicit type to the variable declaration in the second example. Doing the same with Colin’s first example fixes the compilation error:

lazy private var animator: UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)

So it turns out that you can reference variables regardless of whether or not you use a closure. The important

Continue reading →

GCD: Using Dispatch Groups for Fun and Profit

I’m republishing this article here so I can reference it later.

I recently needed a way to perform an unknown number of asynchronous http requests and wait until they were all done before proceeding. dispatch_groups are a neat feature of Grand Central Dispatch (GCD) that made this easy to do.

There are a couple of ways to use dispatch_groups but the basic idea is the same: create a dispatch_group, give it some tasks, and wait for it to finish those tasks.

First, let’s create a group:

dispatch_group_t group = dispatch_group_create();

There are two ways to add tasks to our dispatch_group. You can either call dispatch_group_async with the group, a dispatch_queue and block to run or manually call dispatch_group_enter.

Calling dispatch_group_async looks something like this:

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Do stuff on

Continue reading →

Jarek’s Anecdote

I’ve just started reading Ed Catmull’s book, Creativity, Inc. Early on Ed explains that he came up with both the z-buffer and texture mapping techniques that had a profound impact on my childhood (Threewave CTF in Quake 1, anybody?). This reminded me of an anecdote the instructor of my second graphics class once shared.

Jarek Rossignac told us about his giving a talk during his tenure as a researcher at IBM. Jarek’s talk was about his research of subdivision of curves. After the talk, a gentleman struck up a conversation with him. That gentleman ended up being Pierre Bézier, the man who popularized the Bézier Curve.

Continue reading →