Under The Bridge Under The Bridge

Category: Programming
This iOS Goes To 11

Been enough of a while since WWDC ’17 for people to sort out what they find interesting about iOS 11 and all now, so let’s take a look shall we?

The canonical references up at the mothership are

A good TL;DR while you bookmark this for later is What’s new in iOS 11 for developers

iOS 11 By Examples has, surprise, examples of using new iOS 11 APIs:

  • Core ML: Image classification demo using Core ML framework
  • Vision: Face detection, landmarks, and object tracking
  • ARKit: Augmented reality experiences in your app or game
  • Core NFC: Reading of NFC tag payloads.
  • IdentityLookup: SMS and MMS filtering using IdentityLookup framework
  • DeviceCheck: Identifying devices that have used a promo, flagging fraudsters
  • Blogs/Newsletter: Other places that mentioned this list — probably mention more good stuff too!

Personally we’d already been planning to try out iPad-only travel soon, and iOS 11 + new iPad Pro looks like MASSIVE WIN on both fronts — seriously, did anybody at all predict the super duper ProMotion screen? — and the mutitasking stuff is a good bit better than we’d expected, and easy to use, see:

CoreNFC is one of those what took you so long things, but hey better late than never: CoreNFC tutorial

Now that Metal is even more metal to support 120 fps displays and all, they’re Introducing Metal 2

If you do anything with file names in iOS or macOS, make sure you read APFS Native Normalization

One thing worthy of noting as removing what we’d found the major annoyance with UIStackView and likely you too: Stack View Custom Spacing

Also top and layout guides are simplified to Safe Area Layout Guide

Some more little bites, to coin a phrase, at #309: UIFontMetrics and #310: Screen Edges in iOS 11 and #311: Round Corner Improvements

Now, to step back from the API level, some people are very excited about this Business Chat thing:

… I believe that Apple Pay Cash, and the ability to send low transaction amounts with no cost to the sender or the receiver (the business) will be one of the most transformative elements to the impact of Business Chat. This can bring rise to a number of new businesses and applications that may be similar to the “gig economy” of Fivver and other systems…

Business Chat can not only spell the end of the POS system and payment systems as we know it in retail sales, it may also spell the end of the shopping carts and payment systems in online sales. Some may argue this is bombastic…

Yes, maybe a touch. Doesn’t mean it’s wrong though; read the whole thing, and the Business Chat info at the mothership, and see what you think —or read this one that figures it’s not just shopping carts in the crosshairs: Apple Bank here we come

Last one we’ll call out here — some good thoughts on the new evolutions of the interface design language: Think Bigger: Design Changes in iOS 11

Need more? Check out your veritably canonical reference to everything new over at New stuff from WWDC 2017!

UPDATES:

Changes to location tracking in iOS 11

iOS 11, Privacy and Single Sign On

ARKit And Kaboodle

The other obviously transformational technology introduced at WWDC this year was Apple’s sudden leap from

One thing is clear: Apple needs to get moving soon.

to, a mere 5 days later — how’s that for “soon?” —

If Apple gets this right, they will own the hardware market for years to come.

and what we’re sure must be by far the quickest ever adoption of an Apple-only technology outside the computer industry,

Ikea’s plans for ARKit revealed, virtual shopping tool will launch in fall with iOS 11

so let’s start collecting links on that shall we?

Introducing ARKit

With ARKit, iPhone and iPad can analyze the scene presented by the camera view and find horizontal planes in the room. ARKit can detect horizontal planes like tables and floors, and can track and place objects on smaller feature points as well. ARKit also makes use of the camera sensor to estimate the total amount of light available in a scene and applies the correct amount of lighting to virtual objects…

… yeah ok that’s pretty cool.

There’s already signup open for ARKit Weekly to keep abreast of news here, and the same obviously extra-keen folk have also started

Made With ARKit: Hand-picked curation of the coolest stuff made with #ARKit

That’s some pretty darn nifty videos there for, like two weeks at this!

To grab some source code examples and interesting commentary, check out

Apple ARKit by Example

  1. Getting setup, draw a cube in virtual reality
  2. Plane Detection and Visualization
  3. Adding geometry and physics fun
  4. Physically Based Rendering

ARShooter – An Example Shooter Created Using iOS 11’s ARKit

ViewAR does first tests with ARKit

3 Things To Know About Apple’s ARKit

Apple’s new augmented reality platform may be its next game-changer

Pretty cool huh? It’s like the future’s so bright we have to wear shades … except the whole point of Apple AR (so far…) is that we don’t!

UPDATES:

ARBrush: “Quick demo of 3d drawing in ARKit using metal + SceneKit.”

ARKit Tutorial in Swift 4 for Xcode 9 using SceneKit

Watch a Tesla Model 3 come to life

ARTetris: “Augmented Reality Tetris made with ARKit and SceneKit”

Getting Started with ARKit: Waypoints

Core MLagueña

Checked out the WW17 Roundup yet? OK then, let’s start digging into this new stuff a little deeper. And we’ll start with the one with the most buzz around the web,

Introducing Core ML

Machine learning opens up opportunities for creating new and engaging experiences. Core ML is a new framework which you can use to easily integrate machine learning models into your app. See how Xcode and Core ML can help you make your app more intelligent with just a few lines of code.

Vision Framework: Building on Core ML

Vision is a new, powerful, and easy-to-use framework that provides solutions to computer vision challenges through a consistent interface. Understand how to use the Vision API to detect faces, compute facial landmarks, track objects, and more. Learn how to take things even further by providing custom machine learning models for Vision tasks using CoreML.

By “more intelligent” what do we mean exactly here? Why, check out

iOS 11: Machine Learning for everyone

The API is pretty simple. The only things you can do are:

  1. loading a trained model
  2. making predictions
  3. profit!!!

This may sound limited but in practice loading a model and making predictions is usually all you’d want to do in your app anyway…

Yep, probably. Some people are very excited about that approach:

Apple Introduces Core ML

When was the last time you opened up a PDF file and edited the design of the document directly?

You don’t.

PDF is not about making a document. PDF is about being able to easily view a document.

With Core ML, Apple has managed to achieve an equivalent of PDF for machine learning. With their .mlmodel format, the company is not venturing into the business of training models (at least not yet). Instead, they have rolled out a meticulously crafted red carpet for models that are already trained. It’s a carpet that deploys across their entire lineup of hardware.

As a business strategy, it’s shrewd. As a technical achievement, it’s stunning. It moves complex machine learning technology within reach of the average developer…

Well, speaking as that Average Developer here, yep this sure sounds like a great way to dip a toe into $CURRENT_BUZZWORD without, y’know, having to actually work at it. Great stuff!

Here’s some more reactions worth reading:

Here’s some models to try it out with, or you can convert your own built with XGBoost, Caffe, LibSVM, scikit-learn, and Keras :

  • Places205-GoogLeNet CoreML (Detects the scene of an image from 205 categories such as an airport terminal, bedroom, forest, coast, and more.)
  • ResNet50 CoreML (Detects the dominant objects present in an image from a set of 1000 categories such as trees, animals, food, vehicles, people, and more.)
  • Inception v3 CoreML (Detects the dominant objects present in an image from a set of 1000 categories such as trees, animals, food, vehicles, people, and more.)
  • VGG16 CoreML (Detects the dominant objects present in an image from a set of 1000 categories such as trees, animals, food, vehicles, people, and more.)

And some samples and tutorials:

Also note NSLinguisticTagger that’s part of the new ML family here too.

For further updates we miss, check out awesome-core-ml and Machine Learning for iOS !

UPDATES:

YOLO: Core ML versus MPSNNGraph

Why Core ML will not work for your app (most likely)

Blog: Getting Started with Vision

MLCamera – Vision & Core ML with AVCaptureSession Inceptionv3 model

Can Core ML in iOS Really Do Hot Dog Detection Without Server-side Processing?

Bringing Machine Learning to your iOS Apps 🤖📲

Creating A Simple Game With CoreML In Swift 4

Custom classifiers in iOS11 using CoreML and Vision

Using Vision Framework for Text Detection in iOS 11

WWDC 2017 Roundup

That was a particularly good WWDC this year wasn’t it? Lots of possibly transformational technologies introduced it’s going to be very interesting indeed to see how they play out!

We’ll explore those in more depth later on, but for now let’s sort out some resource links and initial reactions.

First up, you want to go download The Unofficial WWDC app for macOS:

4a5b0d11384fd46acdbe8f552a5b85a7

Isn’t that pretty? Definitely your WWDC video viewing app of choice, we say!

Next, address this problem

687474703a2f2f63646e2e6d656d6567656e657261746f722e6e65742f696e7374616e6365732f343030782f32313932383230372e6a7067.jpeg

with the WWDC-Downloader script over at Github.

(And while that’s all downloading for your next transcontinental plane flight or whatever, have your heart warmed with

WWDC17 Scholarship winner Kenny Batista shared his experiences at Apple’s big week for developers !)

If you want to prioritize a bit, here’s a WWDC 2017 Viewing Guide that looks solid.

There’s enough interesting new technologies that they deserve roundup posts each as there’s going to be a lot of figuring out there to keep track of, so for now we’ll just link a couple overviews hitting the highest points:

The 2017 Apple Design Award Winners

WWDC 2017 Initial Impressions from the Wenderlich team

Another take from the Big Nerd Ranch crew at WWDC 2017: Helping You Get Things Done

Also check out their article New HEVC & HEIF Media Formats: What You Need to Know and Begun, The Codec War Has for background on the fur that’s no doubt going to be flying all over about that soon.

Oh, and we’ll put An In Depth Look At the New App Store and New rules following WWDC 2017 here too — there’s some interesting changes there you want to be aware of. Most notably to us, that finally The new iOS App Store lets devs choose whether or not to reset ratings when updating!! Woo-woo-woohoo!

And as always, over at mjtsai.com you can find a near-canonical list of WWDC 2017 Links for deeper diving!

UPDATES:

HEIF: A First Nail in JPEG’s Coffin?

Swift Witness For Sourcery

Yep, think we’ve outdone ourselves with the obscure title references this time. If you knew it was from Malachi 3:5, our congratulations!

What we’re talking about today is what you might have noticed when it was named Insanity, but these days is called Sourcery:

What is Sourcery?

Sourcery scans your source code, applies your personal templates and generates Swift code for you, allowing you to use meta-programming techniques to save time and decrease potential mistakes.

Using it offers many benefits:

  • Write less repetitive code and make it easy to adhere to DRY principle.
  • It allows you to create better code, one that would be hard to maintain without it, e.g. performing automatic property level difference in tests
  • Limits the risk of introducing human error when refactoring.
  • Sourcery doesn’t use runtime tricks, in fact, it allows you to leverage compiler, even more, creating more safety.
  • Immediate feedback: Sourcery features built-in daemon support, enabling you to write your templates in real-time side-by-side with generated code.

Sourcery is so meta that it is used to code-generate its boilerplate code

We’re pretty leery of tools like this in general as they end up being yet another cesspool of technical debt, but that this one is based on SourceKitten is interesting enough to at least pay some attention to it. And it’s catching a bit of buzz:

How to Automate Swift Boilerplate Code with Sourcery

Switching between our Android projects written in Java (or more recently, Kotlin) and iOS projects in Swift makes me sorely miss annotation processing. Annotations in Java can be used to generate common code and things that Swift developers would have to hand write. Hand written code has to be bug free and consistently updated and maintained. Things like implementing equals and parsing JSON typically require boilerplate code, unique enough that you cannot abstract it away.

Although Swift does not have annotation processing, we can get pretty close with Sourcery. Let’s take a look at how it all works in this quick Sourcery tutorial…

AutoEquatable and AutoHashable by Sourcery

I’m really interested in Sourcery, so I installed it in my one of repositories. In my case, I generated AutoEquatable and AutoHashable at first because I felt these implementations contain really a lot of boilerplate codes…

If I add a new property or a new API, it prevents a human error! Though it looks trivial, Sourcery uses a safe Hashable as I mentioned in Safe Hashable in Swift. Isn’t it cool?

The Magic of Sourcery

Sourcery is a code generation tool for Swift. It is particularly well suited for auto-generating code that is very repetitious; code most developers refer to as “boilerplate”. Some examples that come to mind:

  • Equatable implementations
  • Hashable implementations
  • struct initializers
  • Lens implementations …

Sourcery — Meta-programing in Swift

Think of the potential : you can basically generate all the boilerplates of your architecture be it CLEAN or VIPER, you can generate all the protocol implementation or even the Realm data object, or even a view based on the variables that are being declared ( Sourcery do support inline code generate ). A declarative view ! The sky is the limit !

Meta Programming Swift with Sourcery

#292: Metaprogramming with Sourcery 🔮

#294: Annotations with Sourcery 🔮

#295: Building an API client with Sourcery Key/Value Annotations 🔮

If you’re looking for a good project to try this out with, a decent set of VIPER templates would be eagerly welcomed. Let us know if you come up with something!

All About Errors

Here’s one to bookmark for reference: The veritably _definitive_ guide to

Error Handling in Swift

Regardless of your skill, handling errors is a common task for any developer. As developers we have complete control of the code we write and features we include, but despite our best efforts, we don’t control everything. We don’t control the values our users enter or whether our applications have access to the resources they need. In this post we’re going to look at how we can handle these situations by looking at error handling in Swift…

Pretty certain you’ll find something new here no matter what your skill level:

Up to now we didn’t even realize catch could take a where clause, huh. That’s what a couple decades of C++ will do to you, reflexively cringe in triggered terror at the mere thought of “exception”. But we’ll get through it — that bit about propagating back asynchronously looks particularly interesting!

UPDATES:

Providing a unified Swift error API

Picking the right way of failing in Swift

Swiftier Swift, Swiftly

Everybody wants their Swift programs to run swiftier, right? In case you haven’t stumbled across it so far, The Canonical List Of Recommended Ways To Do That is found right in the Swift github repo:

Writing High-Performance Swift Code

So you should go read that at the first opportunity and keep an eye on edits too!

Follow that up with this excellently accessible talk:

Real World Swift Performance

Lots of things can make your application slow. In this talk we’re going to explore application performance from the bottom. Looking at the real world performance impact of Swift features (Protocols, Generics, Structs, and Classes) in the context of data parsing, mapping, and persistence, we will identify the key bottlenecks as well as the performance gains that Swift gives us…

Here’s a couple more illuminating posts on instruction level speediness:

Unowned or Weak? Lifetime and Performance

os_unfair_lock is the new synchronization hotness!

Mind you, it’s quite likely that for many of you the most compelling thing that springs to mind on reading the headline there is not swiftier execution, but swiftier compilation, amirite? Here’s some backgrounders and tips on that:

Profiling your Swift compilation times

Guarding Against Long Compiles

Speeding Up Slow Swift Build Times

Regarding Swift build time optimizations

After I read @nickoneill’s excellent post Speeding Up Slow Swift Build Times last week, it’s hard not to look at Swift code in a slightly different light. A single line of what could be considered clean code now raises a new question — should it be refactored to 9 lines to please the compiler? (see the nil coalescing operator example further down) What is more important? Concise code or compiler friendly code? Well, it depends on project size and developer frustration.

Before getting to some examples, let me first mention that going through log files manually is very time consuming. Someone came up with a terminal command to make it easier but I took it a step further and threw together an Xcode plugin

Also check out Swift build time optimizations — Part 2 for followup.

And you’re probably vaguely aware that XCTest does performance these days but haven’t adopted it wholeheartedly yet, right? Check out

measure(): How to optimize our slow code and adjust the baseline

XCTest makes performance testing extraordinarily easy: you give it a closure to run, and it will execute that code 10 times in a row. You’ll then get a report back of how long the call took on average, what the standard deviation was (how much variance there was between runs), and even how fast each of those 10 runs performed if you want the details…

Kinda handy to be able to get hard numbers for your refactoring efforts that easily, indeed.

Any other compilation or execution tips, tricks, and tradeoffs to share, Dear Readers?

UPDATES:

Improving Swift compile times

Swift one-line type-checking lasting 27s

How we cut our iOS app’s launch time in half (with this one cool trick)

A record-breaking story of boosting the launch time of Mail.Ru’s email app for iOS

Consistently Immutable

Here’s a good read for when you’re planning a data caching strategy for your Next Great Project:

Managing Consistency of Immutable Models

Immutable models have many advantages, but managing consistency can be difficult. At LinkedIn, we’ve open sourced two Swift libraries to help manage consistency and persistence of immutable models. Unlike Core Data, the API is non-blocking, exception-free, and scales well with large numbers of models and view controllers…

The natural reflex of course is Core Data, and these were their issues:

  • The first problem we ran into was stability…
  • The other issue we ran into was performance…
  • Another issue is there are no eviction strategies…
  • Another point was that migrations are necessary…
  • The final thing for us was scalability…
  • Even if we were to solve all of these problems, we didn’t like the programming model that Core Data provided. One of the things we didn’t like is that all the models were mutable, and they’re not thread-safe, so when you pass it to a different thread, you need to reread from the database…

We could nitpick over how serious these problems actually are if you know what you’re doing, but yeah that last one is kinda a deal. So what would a modern architecture look like, you ask?

What we really wanted to try was to use immutable models for all the data in our application. Immutable models have long been a cornerstone of functional programming languages, and with the introduction of Swift to iOS, I think that they’ve been pushing this idea of immutability. Swift has a bunch of “immutable by default” features, and personally, I’ve been learning a lot about immutability because of Swift. I’m going to list a few of the examples of why immutable models are beneficial:

We’ll assume you know those benefits already, but if not TL;DR they’re easier.

  • We wanted immutable, thread safe models for the reasons which I just went over.
  • We wanted consistency, so a change in one place would automatically be reflected elsewhere in the application.
  • We wanted non-blocking access on all reads and writes. There wouldn’t be any disk access on the main thread.
  • We wanted a simple eviction strategy. Looking at some of our peers, we saw that a lot of apps were using 200, 300, 400 megabytes of disk space, and ideally, for us, we wanted to limit it below 50 megabytes.
  • We want it to scale well. Again, as I said, we have hundreds of models and view controllers, and we wanted easy migrations. Actually, we wanted to write no migration code at all, ever.

To accomplish all these things we wrote RocketData.

RocketData is a caching and consistency solution for immutable models. It’s intended to replace Core Data or at least fulfill that role in an application. It’s written 100% in Swift, and we really like it…

Definitely read it all, but here’s the secret sauce:

The Consistency Manager, as I said, is truly the engine which drives everything. It’s an entirely separate open-source library that RocketData depends on. It’s written completely in Swift, and you can use it independently.

and here’s the executive summary:

Core Data Comparison

There is no tool which will solve every problem. For many use cases, we believe Rocket Data is a better option to Core Data because of the speed and stability guarantees as well as working with immutable instead of mutable models. However, Core Data is useful for other use cases, and it’s important to consider the differences before you make a decision for your app…

Always good to have options … and this looks like a pretty sweet one!

UPDATES:

Speaking of options: Core Data or Realm

#35 Structs Alternative: Using Swift Protocols to Enhance Safety of Core Data Access

Introducing Plank: Immutable model generation for iOS

Make Collections Great Again

Pretty good chance you use UICollectionView pretty widely in your apps, right? And pretty much exactly the same chance you’d like some extra performance and enhanced reusability, no doubt? Ch-ch-check out

Open Sourcing IGListKit

Today, we are excited to announce that we’re open sourcing one of Instagram’s core frameworks: IGListKit. This framework powers how we take data from the server and turn it into fast and flexible lists.

To do this, we combined a familiar data-driven UICollectionView architecture with a state-of-the-art diffing algorithm. With this setup, we created a tool that lets engineers with varying levels of experience work quickly and safely on Instagram. Outside of Instagram, IGListKit can help anyone who is building lists to skip tedious and error-prone setup, and utilize one of the fastest diffing algorithms available for iOS…

Better Architectures

IGListKit reduces the chance of having “massive view controllers” by dividing responsibilities into multiple layers: the view controller, list adapter, section controller, and the cell.

This design has a positive side effect of building your lists with independent components, meaning you end up with reusable section controllers and cells. The result is a one-way data flow, where each component has no knowledge of its parent…

Diffing

In Instagram, our data can change a lot. For example, new data arrives from the server each time you like a photo or you receive a direct message in real-time.

UICollectionView can handle all types of updates (deletes, reloads, moves, and inserts), but performing those updates without crashing can be a little tricky.

We built a lightning-fast diffing algorithm based on a paper published back in 1978 by Paul Heckel. This algorithm finds all of the possible updates that UICollectionView needs in linear time (that’s O(n))…

That’s a pretty serious level of win … especially if you’ve got some experience with that “little tricky” task of managing updates elegantly, in the same way that liquid nitrogen is a “little chilly”. So if you’ve got a big collection of data to manage, we wholeheartedly recommend you check that out!

ADDENDA:

And while you’re looking at libraries to make your UI nicer, check out this most excellent list:

39 Open Source Swift UI Libraries For iOS App Development

and of course there’s always

Awesome iOS: A curated list of awesome iOS libraries, including Objective-C and Swift Projects

UPDATES:

The Case for Deprecating UITableView

IGListKit Reference

IGListKit Tutorial: Better UICollectionViews

IGListKit — Migrating an UITableView to IGListCollectionView

iOS Keeping data fresh in Buffer for iOS with AsyncDisplayKit, IGListKit & Pusher

TextureIntroducing Texture, a new home for AsyncDisplayKit

The Perfect 10

Yep, it’s that time of year again, where we all panic about getting new iOS features into our apps before ship date. Know the feeling? Thought so, yep. We’ve got a better excuse than usual this year though; this last month we’ve been settling into the electric city of Bangkok to work at agoda.com. Which we’d thoroughly recommend based on our experience so far; check out all the great jobs if you’re looking for an awesome gig! Any-ways, if you’re looking to get up to speed on iOS 10, here’s some great resources to put the pedal down:

iOS 10 Day by Day

This series has been a great resource for several OS versions now, and this year sure doesn’t look like a letdown so far:

Day 1 :: Messages

One of the new features was the ability for 3rd-party developers to create their own message extensions that can be used from within the Messages application… To demonstrate this exciting new extension type, we’ll take a look at a project that allows two players to play a simplified version of the popular game, Battleships,

Day 2 :: Thread Sanitizer

Xcode has shipped with a new debugging tool called the Thread Sanitizer which can help identify [threading] issues before you even notice them. We’ll create a simple application which enables us to deposit and withdraw money in $100 denominations…

Day 3 :: Xcode Source Editor Extensions

We’ll take a look at building our own extension that replaces ASCII characters with their Emoji equivalents…

Day 4 :: UIViewPropertyAnimator

To explore this new class, we’ll go through a few examples where we animate an image across the screen…

Of course, the Mother of All Tutorial Sites aka raywenderlich.com has their (6th!) annual opus proceeding nicely:

iOS 10 by Tutorials: First 6 Chapters Now Available!

Also note the iOS 10 + watchOS 2 bundle, and check out the latest tutorials/screencasts.

And one particular to finish off with; our entire career, we’ve had requests for voice dictation apps that we’ve always begged off with “Dude, that’s hard. And expensive.” Well, LOOK HOW EASY IT IS NOW:

Building a Speech-to-Text App Using Speech Framework in iOS 10

Truly, we live in the age of marvels.

UPDATES:

A List of All Current iOS 10 UI Kits for Sketch

iOS 10 Sampler – A Set Of Examples For Many Of The New Features Added With The iOS 10 SDK

Getting Ready for ATS Enforcement in 2017

How to generate haptic feedback with UIFeedbackGenerator

Tutorial: Using iOS 10’s UIViewPropertyAnimator Class

Content app extensions in iOS 10

Enhance Your Apps With External Displays

Creating iMessage Apps Your Users Will Love

Improving our iOS Share Extension Installation Guide