Fixing Watch to iPhone Communication

A quick perusal of the Apple Developer Forums shows more than a few developers have experienced strange bugs when calling openParentApplication:reply: in a WKInterfaceController. Most often, it seems, when making multiple requests in quick succession.

In an app I worked on recently we were hitting all sorts of strange behaviour. Sometimes openParentApplication:reply: would mysteriously be called twice. Every once in a while the reply block would fail to ever be called — despite being certain the delegate was functioning correctly and completing work in a background task.

Frustrating, to say the least.

However, I’ve dsicovered a solution that has made communication with the iPhone rock-solid. Essentially you have to begin — and end, after two seconds — an empty background task right at the beginning of the delegate method. It should be the absolute first thing you do. Afterward, kick off a background task for the real work.

Here’s an example:

The theory is the bogus background task prevents the OS from killing your app immediately. I honestly couldn’t tell you for sure.

All I know is it’s made openParentApplication:reply: far more reliable.

Tired of never shipping your side projects?

Check out 5 free lessons on mistakes developers make.

MAKE YOUR MONDAYS A TINY BIT MORE AWESOME

I share my best articles with the Monday Mailer, my weekly newsletter for developers about shipping side projects, being more productive, and doing your best work. Sign up below to get the next one in your inbox.