Mobile Apps and Web Solutions

watchOS 2 Complications Complications

So I decided against adding a glance to the app as promised last time, as it doesn’t really add much value over the app itself, and to be honest I found it quicker to go straight to the app rather than try to scroll through my glances.

Anyhoo, I’ve had a load of fun trying to get the complications working, and after more to-ing and fro-ing finally have it working.

This should have been easy, especially as the built in templates for the smaller complications already have the “progress ring” that matches almost exactly what I want.

Apple’s documentation is OK if a little dry, and there are few helpful examples that can be found with a quick search.

The one key point I found was that the XCode project settings where you set the data class to be used for the complications data isn’t quite correct.

What you need to do is set the class in Info.plist to be: $(PRODUCT_MODULE_NAME).ComplicationsDataSource

not:

ComplicationsDataSource

as the target\General\Complications Configuration would suggest.

So I think v2.0.0 is just about done! I’ll need to generate the App Store screen shots and text as usual, and then hopefully it’ll be released in the next couple of weeks.

I’ll probably write a summary post soon too to summarise all these ramblings :)

N.B. All code can be seen on GitHub

N.B. This blog post has now moved to https://writingontablets.com

Testing a watchOS 2 app directly on the watch

TL;DR

This is a fairly long story with a lot of false trails, so if you just want the best way of testing on the watch I’d skip to the end

The app never seems to start on the watch

In my last post I mentioned that although the app worked fine in the Watch simulator, it was “slow” on the watch - in other words it never seemed to load at all.

I first assumed the problem was that the app initialization was taking too long, so I tried a few things including:

  • Adding in-memory caching to the data model class to (maybe) improve subsequent reading of user settings post initialization
  • Changing the data model to be held in the App/Watch delegate class so it’s only initialized once per app
  • Moving the watch session connection code to run on a background thread so it didn’t block the UI thread loading the initial view controller

All of these changes made a difference in the load time on the simulator, but still no joy running on the watch :(

Pushing a TestFlight app to the Watch

I finally realised I must be missing something, so after doing a bit of research I found out that only signed versions of a Watch app will run on the actual device.

This made a lot of sense, so I followed the further advice of pushing an archived version of the code to the App Store, and then installing it on my phone (then watch) via a TestFlight install.

Now this actually worked, but it took me ages to actually get an acceptable archive working.

The problem was I was trying to reuse the code in both iOS and watchOS using a code library, and I just couldn’t get the archiving to work and sign the various bits of code correctly.

This is crazy complicated (for me at least) and to be honest whenever I’ve used a dynamic library other that through pods I’ve always had issues. Not sure if it’s me or it really is a complicated problem, but it seems the worst part of iOS development compared to most other languages and platforms.

In the end I gave up with the library approach on the watch, and just added the shared code files to the Watch Extension target directly which did the trick.

Solution: Add watch to provisoning profiles

Although the “TestFlight” solution worked, clearly it’s non-optimal as there’s a long iteration time to test any changes on the actual watch.

Some more research found this excellent post by Jared Sinclair on how to solve this problem by adding you watch to your provisoning profiles.

I won’t repeat Jared’s post here, and it’s sort of “obvious once you think about it” (which clearly I didn’t!), but I guess I did learn a lot along the various false trails - which is the purpose of the exercise :)

Next Steps

  • Add a glance view
  • Add watch complications
  • Ship it!

N.B. All code can be seen on GitHub

N.B. This blog post has now moved to https://writingontablets.com

Synching user settings on watchOS 2

In my last post I mentioned that watchOS 2 doesn’t have access to NSUserSettings, which isn’t entirely correct. The watch can have access to user settings, it’s just it can’t directly synchronise them when they are backed by iCloud.

This means if we change the user settings on the phone we need to push the changes to the watch ourselves.

This isn’t too tricky to do if you follow the instructions on the Apple Developer site about using WCSession to set up a sharing session between the phone app and your watch extension.

As I wanted to continue sharing code between the two apps, I’ve had to add a slightly hacky onWatch property to the settings class so it:

  • Only uses iCloud synchronisation on the phone
  • If a setting is changed on the phone, it sets up a WCSession and uses it to send a Dictionary containing the changes via transferUserInfo()
  • The watch will receive those changes in didReceiveUserInfo() which it can then use to update it’s local settings

There’s a small “code smell” of adding in this onWatch flag just so I can reuse some code in this way, so I may come back and revisit this code later.

According to the docs transferUserInfo() queues any transfers up to send to the watch when a connection is available.

The solution seems to work pretty well - in the watch simulator at least! On the watch itself the app does seem to run rather slow, but TBH that seems a problem with most watchOS apps I’ve used. More investigation needed!

Next steps: Start tidying up the UI in the existing app, and see if we can improve the performance on the actual watch.

N.B. All code can be seen on GitHub

N.B. This blog post has now moved to https://writingontablets.com

Updating code to watchOS 2

Updating my Count The Days Left app to watchOS 2 is clearly going to be more work than I thought :(

Upgrading to latest version of Swift

After loading the existing project into XCode 7, it prompted me to update the Swift code, and when I accepted did a pretty good job of fixing everything up.

As a learning experience I reverted the changes to fix them up myself. Changes made included:

  • Changing the way string length is calculated e.g. count(self.model.title) => self.model.title.characters.count
  • Using generic Dictionary<String, AnyObject> where previous used NSDictionary
  • Updating NSCalendarUnit values e.g. NSCalendarUnit.CalendarUnitDay to NSCalendarUnit.Day
  • touchesBegan() event handler now has a Set<UITouch> parameter instead of a Set<NSObject> (and a ? on the event)
  • Lots of places I’d used var could be replaced by let - too much C# in the day job I suspect :)

Two versions of the shared library required

The big architecture change in watchOS 2 is that the watch extension code runs on the watch not the phone.

This should make the app much quicker, but brings in a whole raft of problems when migrating the code.

First up was making use of the shared library on code used by both the iOS app and the extension. The version running on the watch needs to be compiled against the watchOS SDK (obviously).

Getting this working stumped me for a while until I found this transition guide on the Apple developer site. See the Sharing Code Between an iOS App and a watchOS App section on how to make a duplicate library using the same files but compiling against the correct SDK.

Can’t share NSUserSettings any more

After solving the above problems, everything built and deployed to the simulator/phone/watch OK, but the watch app didn’t actually work. However a bit more reading of the upgrading code documentation pointed out a fundamental problem.

Because the extension has moved over to the watch, it doesn’t have access to the NSUserSettings objects, even if we’ve set up the App Groups capability correctly.

This means I’ll have to roll my own sharing of data between the app and the extension, which is a bit of a pain, but should be fun to learn.

More on how I get on next time!

N.B. All code can be seen on GitHub

N.B. This blog post has now moved to https://writingontablets.com

WatchOS Development Phase 2

So I treated myself of an Apple Watch.

I’m enjoying it so far, but despite being heads down in getting the all-new v3.0 of Daily Optimiser out the door, and back contracting in London (earning some money!), I wanted to make some improvements to Count The Days Left now I can actually see it running on an actual watch.

There are several things in the original version that can be improved on or added to. In no particular order:

  • Upgrade to watchOS 2 and latest version of an ever evolving Swift
  • Design! A white background? What was I thinking :(
  • Adding a glance. It’s now pretty obvious this is the natural place for main access to the app (like the Today widget is the most useful part of the main iOS app)
  • Adding a complication so the days left can be seen on the main watch screen

I’m hoping to get all the above done quite quickly, and as before I’ll be blogging here about what I do as well as sharing the code on GitHub

If you missed it and you’re interested, you can see a summary of the previous iteration , which includes links to earlier posts.

N.B. This blog post has now moved to https://writingontablets.com