A story about unstoppable animations in SwiftUI

In a project I’m working on in SwiftUI I had a need for a refresh button. I wanted it to be a button with feedback to the user about what is going on and I also wanted to limit the users from hammering the button.

I decided that adding an animation where the image on the button would start rotating when it was pressed and stop rotating when the request was done would be a good enough feedback to the users. The button presses would be throttled in combine to refresh only once every 10 seconds.

To start an animation on a button press is no problem in SwiftUI, all you need to do is to use an updating property, in my case I decided to go with a @Published property in my model.

In my RefreshButton I observe the model object and the UI will update accordingly. Or so I thought… I quickly discovered that no combination of changing my isRotating to false in different places or using withAnimation or trying to set my animation to nil would stop the animation.

It appears that once you add a .repeatForever() modifier to your animation, it will – as the name suggests – repeat forever.

What I had to to do instead to fix this behavior was to introduce a second @Published property into my ObservableObject.

So instead of observing the isRotating to trigger the animation on and off I had to introduce a second state to show the rotating image or not, when it’s false I will instead show a non-rotating version of the same image.

I did also experiment with an animation that repeats a number of times and switching that number to 0 when my isRotating is set to false, and while this stops the animation, it won’t stop until it has actually finished animating which in my situation felt like it took too long.

Ideally I would like a way in the future to be able to toggle the animations on and off in a simpler way, perhaps being able to make a .conditionalAnimation() modifier that takes a bool and either adds the animation or not.

A beginners guide to WWDC

The keynote

For me, and many developers in the Apple ecosystem, the WWDC keynote is an occasion as important as any big holiday, birthday or family tradition.

As the event is live in the evening in Swedish local time, I give my family notice well in advance that this time is reserved for me. In the early years I would sit with my computer reading live blogs from the event and waiting for the stream to be made available and in later years in front of the TV watching the live stream on my Apple TV.
Only half jokingly I make it clear to my kids that while it might look like I’m present in the room, I’m actually in California watching the keynote live.

As the WWDC sessions are made available I try to find time to watch as many as I can, this is something that usually happens for me in the evenings, when I have a day off and I’m alone at home (which is usually not that often with three kids) or at work when I’m researching a specific subject.

I rarely get time to watch a majority of all the videos posted while they’re “fresh” but will eventually learn of the parts that are important to the tasks that I’m working on.

Last year was different as I finally got to experience my first WWDC on site in California. I entered the ticket lottery and was lucky enough to get a spot!

Preparing for WWDC

I had heard from others and read online that it’s important to book flight and hotels as soon as possible as they sell out quickly. Apple has some partner deals for nearby hotels but I ended up selecting one of my own which I felt was better priced.

I set out with a goal that commuting to the conference shouldn’t take more than 20 minutes and ended up with a hotel about 3.5km away from the San José Convention Center and payed around €900 for 8 nights, arriving on Sunday evening and leaving on Saturday afternoon.

I was planning on travelling light so I booked a cheap flight where only one small bag was included in the price and with one transfer there and back for about €500. This plan sort of failed as I decided to buy a HomePod and some sneakers and clothes and eventually had to buy another bag and pay for extra luggage at the airport. Next time I won’t cheap out.

Next thing on my todo was trying to decide what I would absolutely need to bring with me and what could be sacrificed.

My essentials list:

  • 32 L backpack (I used this as carry-on and at the conference for carrying around my computer, chargers and other miscellaneous stuff that I needed)
  • 15″ Macbook Pro (A lighter Mac would have been nice to travel with, but I only have one ??‍♂️)
  • Old iPhone 6S for testing the latest iOS 12 beta (Didn’t want to risk being without my phone if it got bricked)
  • Sennheiser noise canceling headphones (Great to have on the plane for sleeping and watching movies)
  • EU to US travel adapter
  • Powerbank with 10 000mAh and dual usb ports (This was a lifesaver, wouldn’t have been able to charge all my things without it)
  • Lightning cables and Apple Watch charger
  • Jacket for cold evenings (This turned out to be unnecessary as we of course did get the official WWDC-jackets and I also bought a jacket from the merchandise store)
  • Comfortable shoes

What didn’t make the cut:

  • Water bottle (They hand out free drinks anyway so I’m glad I didn’t bring it)
  • iPad Pro 12″ (Too big and heavy for travelling this time but I really missed it on the plane, in flight entertainment is garbage compared to an iPad)
  • Power strip (I switched this for a power bank and didn’t regret it at all)
  • Pretty much half of the clothes I needed because I figured I would buy some anyway (which I also did and more at this mall in SJ)

Making the most out of the conference

The Keynote

Having traveled all the way from Sweden to San José and being my first time at WWDC I wanted to make sure I had great seats at the Keynote. After some input from colleagues who had been there previous years I decided that leaving my hotel at 3AM was probably a good idea. I took an Uber to the conference and arrived around 3:30AM.

A small group of people waiting in line for the WWDC keynote
One of the first in line for the key note

It was a good call, maybe 20 people ahead of me in line and not too chilly at night.

Larger group of people waiting in front of apple staff
First in line inside the convention center

Standing in line took some time, after reviewing the time stamps on my photo I would say it was around 3 hours of queueing outside and then another 2,5 hours inside before being let into the hall where the keynote was.

Many people hurrying to the front of the keynote stage
The flood gates have opened

So was it worth it? If you ask me, yes, I got great seats at the center of the stage really close to the action. Wouldn’t have felt all too good being seated at the back of the hall after such a long travel.

Will I stand in line for that long again? Not 100% sure yet, time will have to tell.

The Labs

Before going I talked with my colleagues to see if anyone had issues to bring up in the engineering labs, I only had one myself concerning problems when using IBDesignables from a separate framework in Interface Builder causing them not to render properly and sometimes resulting in very weird content sizes.

I didn’t get any other issues to bring with me so I decided to make the most out of my child and obligation free time at the conference and go to as many sessions as possible as I don’t often have the opportunity at home to just sit down and binge watch the sessions.

I know many others will tell you to really make the most of the labs since that’s the only chance you have to really get to interact with the engineers who built all these cool new features. Some of the people I hung out with at the conference spent their days trying out all the new stuff and running back on forth to the labs to ask about their problems.

For me though I was really enjoying all the sessions and happy that I was able to really focus on watching as many as possible and didn’t really feel like I was missing out on anything by not going to so many labs.

The sessions

There are three sessions running in parallell so the first day was spent trying to figure out which sessions would be most interesting to watch live and which would be better watched later on.

I had a general idea to watch mostly sessions related to what I’m working on but to also go a bit outside my comfort zone and catch some design sessions and also tech that sounds interesting like ML and AR but aren’t related to what I’m working on right now.

The plan turned out quite well, I think I missed 3 sessions in total, one because I was at a lab session that took longer than expected and another one because the line for the merchandise was a lot longer than expected. And the last one was the last session before the Bash on Thursday and since I had to commute a bit to leave my bag at the hotell and freshen up I decided to skip it.

During WWDC and on the travel back I watched around 35-40 sessions in total which felt like a great success, I took a lot of notes during the sessions to also help me remember later what was said and I have been using those notes during the months after to find the right slides to go back and read up on again or to share something with colleagues.

This post isn’t really about the actual content of the 2018 WWDC, there were many great sessions to watch but if I had to pick three sessions to recommend to someone else, this is the list I would go with:

  1. Embracing Algorithms
    https://developer.apple.com/videos/play/wwdc2018/223/
  2. Adding Delight to your iOS Apps
    https://developer.apple.com/videos/play/wwdc2018/233/
  3. Designing Fluid Interfaces
    https://developer.apple.com/videos/play/wwdc2018/803/

The Venue

Getting to and from the conference was fairly straight forward, the buses and the light rail stop right outside and there were pick up spots for Uber around the corner.

Every morning there was some kind of breakfast, and with some kind I mean that it was not the typical hotel experience and what I would consider a healthy breakfast but there were usually soft drinks, coffee, tea, fruit and usually a lot of sweet stuff, like donuts.

Lots of chocolate glazed donuts for breakfast
Healthy Friday breakfast

Lunch, drinks and snacks are included every day, there were a couple of different options every day, as far as I could tell it was either a vegetarian, a meat or a fish dish of some sort. The good ones run out quickly so if you are picky don’t linger around after the sessions.

As a Swede I kept asking for when the coffee coffee flavoured tea would be served and to much disappointment it was only served during breakfast and late in the afternoon, and it wasn’t really coffee. There are plenty of Starbucks around that served something similar to coffee at least.

As for dinner there are plenty of restaurants nearby the convention center, the San Pedro Square Market that is nearby has decent food and drinks for example and a good place to hang out with a larger group of people.

The bash

Thursday evening after the sessions is the annual WWDC Bash. The Bash is a celebration party the last evening of the conference with lots of food, drinks and games and ending with a concert. I absolutely recommend going to the Bash as it’s a fun and casual event where you can have a good time and meet new people.

The venue is in a park and it’s big enough to not feel crowded. Last year Panic! At the Disco was playing which was a lot of fun!

Panic! At the Disco playing live at the Bash Concert
Panic! At the Disco @ The 2018 Bash

Hindsight

Going to WWDC is an expensive trip for most developers and companies. Hotel, plane tickets, WWDC ticket, living expenses in total for me came to around €3500-€4000 and an extra €1000 for shopping. Working as a consultant or freelancer you have to add loss of income to that list as well and if you’re working on a product then maybe it’s rather a loss of effort.

If you do decide to go, it can be worth finding a hotel near the light rail instead of the buses. I found the buses were quite unreliable so I ended up taking an Uber most of the time anyway (took me long enough as well to discover the ride sharing feature which reduced the cost of a rid to less than half and only about twice as expensive as a bus ticket, €3-5 per trip).
Some of my friends who took the light rail had better experiences though and the tickets were the same price as the buses, around €2 per trip.

I don’t know if I would have benefitted more from going to more labs or not, some of the people that I hung out with during the conference, and who had been there before, went to only a few sessions and instead put most of their time into trying out new things and going to the labs with the problems they discovered.
The strong argument for this is that you can of course only talk to the engineers and get help in the labs while at WWDC but you can watch the sessions anytime online. I don’t feel like I made a bad choice of mostly going to sessions but it’s something to have in mind.

When I booked my plane tickets I wanted to arrive on Friday in San Francisco for a weekend vacation before the conference and found a plane that arrived around noon. What I didn’t expect was that it would be another 2-3 hours from landing to my hotel because of some delays and also the time it takes to pass border control. It was however a great decision because I had a great day in SF.

Doing the opposite for my trip back was not such a great choice, I had opted for a flight back around four in the afternoon but I completely miscalculated that decision since checkout at the hotel was before noon and I had two large bags and backpack to carry around and I also didn’t want to risk being delayed by queues at the airport.

This year I learnt that there is a direct flight to the Oakland Airport which will save me some around 3-8 hours of travel time on the way there which is definitely a plus for me even though it costs a little bit more.

So my plan to have a last day of sightseeing didn’t fall through and instead I took an Uber to the airport around 10 in the morning and had a really long day there as the flight was delayed for one hour and didn’t leave until around half past five in the afternoon and then an additional almost two hours in Paris for my transfer so I didn’t arrive safe at home until after around 25 hours of travel instead of around 18-20 as I had anticipated from when I booked the tickets.

Conclusion

For me WWDC was absolutely worth paying for and I even decided to enter the lottery again this year and got a ticket!

I can’t say for sure that WWDC is worth the cost for everyone, if you’re only there to watch the sessions but have the possibility to do it when you’re at home then the price is even more questionable.

Hope this guide can be of help to someone preparing for their first WWDC experience!

 

Hiding sensitive information when your app goes into the background

Following a discussion from yesterday during an after work about the snapshot shown when the application goes into the background not being the same as when you just bring the app switcher up and then goes straight back to the app again I decided to put together a simple demo.

Below is a representation of the app life cycle

App Life Cycle, apple.com

Most apps present a custom snapshot for your app only when the app is sent to the background which might be okay in most cases. Although let’s say that you are browsing your account statement on the subway and a stranger stands behind you, perhaps you react fast and double-press the home button to bring the app switcher up, only to realize your information is still visible.

So here’s to show how to fix this problem.

In your app delegate, add two UIViews which will be shown when you enter the different states:

 

These UIViews will be added and removed to the view hierarchy when switching between app states by adding the following code:

 

And that’s it!

Run your app and demo by either bringing up the app switcher, control center or notification center to see the yellow label for inactive state or send your app to the background and bring the app switcher up to see the red label for background state.

Become more productive with the story board preview in Xcode 8

Here’s a bit of useful advice for debugging what’s going on with different screen sizes when you’re laying out your interface in Interface Builder.

First, select your Main.storyboard in the project navigator.

In the top bar of Xcode you have 6 buttons to the far right. The first one from the left is likely blue and looks like an alignment button. The second one from the right, the Assistant Editor, is the one you want when doing this, it looks like a Venn-diagram or two circles that intersect. Press it.

Now you have a split view with your Main.storyboard on the left side and most likely your ViewController to the right. In the navigation bar of the right split screen window, to the far left, you have a button that looks like four connected boxes. Press it, and from the menu that shows up, find your Main.storyboard file and select it. You will now have two windows showing your storyboard.

 

Now press the button with the squares again, this time it will show contextually different content. Navigate down to

“Preview (1)” and select the only option available in that menu.

The view on the right side will now change to show only the iPhone 7 or iPhone 7 Plus view. You also get a bar on the bottom of the screen with a Plus-sign in it. If you press it you can now add additional Devices that will show up on the screen at the same time.

So go ahead and add for example iPhone 7 Plus, iPhone 7 and iPhone SE, even multiple rotations if you need to, to be able to see in real time as you change your constraints on the left side, how they reflect on different screen sizes ?

Getting started with Xcode 8 Extensions


When Apple release Xcode 8 in 2016 they also released their take on Xcode plugins, called Extensions.

Getting started with extensions isn’t that hard, but they’ve hidden them away in the Settings app.

To start, download any Xcode extension from the App Store or GitHub, you can for example find a list here of some popular extensions: https://theswiftdev.com/2016/08/17/xcode-8-extensions/

Once you have installed your extension you may find that it doesn’t show up in Xcode in the bottom of the Editor menu.

What you need to do is:

  • Open the Settings app and click on the Extensions icon.
  • Xcode Source Editor should appear in the list on the left side of the window. Select it.
  • Check the box next to each extension that you want to enable.
  • Restart Xcode if the new extensions doesn’t appear in the Editor menu.

You’re done!