As one of the early adopters of Apple TV and tvOS, Gilt Groupe was recently selected to present their “Gilt on TV” app at the Apple Keynote event in September.
This presentation covers Gilt's discoveries during the process of building a tvOS app from scratch in Swift.
It was presented at iOSoho on October 12, 2015 in New York City.
3. tvOS vs. iOS: Similarities
• Most of tvOS is a direct subset of iOS
• Familiarity with UIKit will serve you well with tvOS
• But, some of the things you may expect won’t be there
• No web views
• Certain frameworks like MultipeerConnectivity aren’t
available
• Several UIControls (pickers, steppers, sliders, etc.)
• Can use Interface Builder (or not), Swift or Objective-C
tvOS tvOS vs. iOS
4. tvOS vs. iOS: Differences
• User interaction is indirect touch
• Unlike an iPhone or iPad, the user doesn’t directly
manipulate the UI — uses indirect touch like a trackpad
• Directionality is reversed from iOS
• Relies on the Focus Engine for determining UI state
• Only on tvOS — adds extensions to UIViewController,
UIView, UITableView, UICollectionView, etc.
tvOS tvOS vs. iOS
5. Focus Engine Philosophy
• The Focus Engine is intended to be a black box
• You provide inputs that determine the initially focused view
• You define where the user can go
• Provide some visual indication of focusability
• Provide an animation or visual state change upon
becoming focused (often handled by the tvOS SDK)
• Once your view controller is onscreen and the initial view has
become focused, the user should be in control of all state
changes
• It is considered rude to forcibly change focus on the user
tvOS The Focus Engine
6. What is “Focus”?
• A focusable view is one that the user can select with the touch
remote
• Being focusable implies that the user can perform an action on
the view
• Focusable views are often “pressable,” like UIButtons
• A focused view renders itself to stand out from the others
onscreen, usually with one or more of the following attributes:
• A larger size
• A drop shadow
• A highlight color or border
tvOS Focus
7. How does the Focus Engine work?
• When the view hierarchy changes, the Focus Engine creates a
map of the visible focusable views
• If the user navigates in a given direction, the Focus Engine
looks for a focusable view in that direction, taking into account
each candidate view’s focus frame size & relative position
• A candidate for receiving next focus is selected
• If the candidate is a table or collection cell, the relevant
delegate is given an opportunity to reject focus and another
candidate may be selected
tvOS The Focus Engine
10. How does a view become “Focusable”?
• By default, the following views are focusable:
• UIControls
• Table cells and collection cells
• Can be overridden by UICollectionViewDelegate
• UIViews that are not otherwise focusable can be made so by
overriding canBecomeFocused()
• The focused property of a UIView indicates whether the
view is currently in focus
tvOS Focus
11. How does a view become “Focused”?
• When a view controller is displayed, the focus engine asks for
the preferredFocusedView from the relevant views and
view controllers to determine which view gets initial focus
• From there, the user controls focus using the remote
• When the user swipes in a given direction, the Focus Engine
selects an appropriate focusable view to get next focus
• Developers can use the UIFocusUpdateContext to
coordinate animations when views go into and out of focus
tvOS Focus
12. tvOS The Focus Engine
Gilt on TV 1.0 — Men’s Store (scrolled)
18. tvOS The Focus Engine
Navigating to “Buy Now” — A Near Miss
19. The Dilemma
tvOS Focus Guides
How could you solve it?
You can’t assume the Focus Engine “sees” focusable views that
aren’t in the direct path of the swipe direction
tvOS solution
Allow a UIView to report to a focus frame that’s larger than the
view’s native frame
1. Call parentView.addLayoutGuide(UIFocusGuide)
2. Set the focus guide’s preferredFocusedView
3. Add Auto Layout constraints using the focus guide as you
would any UIView
23. Handling scrolling content
• You will not be able to present scrolling content in the same
way you do in iOS
• User can only scroll by navigating to something focusable
within a scroll view
• The Focus Engine only pays attention to focusable non-hidden
views with non-zero alphas that have at least one pixel within
the screen bounds
• Focusable views and cells that are offscreen won’t be visible
to the Focus Engine, so the user can’t navigate to them
tvOS The Focus Engine
26. Other challenges for tvOS developers
• Apparently a lot of people rely on web views for mission-critical
screens within their apps — they will be forced to rethink
• Restrictions on App Store binaries:
• Applications need to be submitted as Bitcode binaries
• This includes embedded frameworks, which makes many
third-party closed-source frameworks unusable in tvOS
• On-device testing — Very few people have development
devices, and simulator testing may not reveal all problems
tvOS tvOS Development
27. Fun things about tvOS development
• Some of the iOS pain points don’t exist on tvOS
• Apple TVs will often be used to stream video — unlike with
mobile devices, tvOS developers can assume the presence
of low-latency, always-on, high-bandwidth networks
• Don’t need to think about screen rotation or size class
changes
• Only need to develop for a single screen resolution — for
now
tvOS tvOS Development
29. Swift — Pain Points
• The language is still changing, so major releases of Xcode
often require a bit of code rewriting
• Usually minimal, and the language is improving as a result
• Developer tooling still needs work
• Debugger sometimes refuses to cough up variable values
• Compiler errors can be vague or misleading
• Compiler stability is sometimes an issue
• Heavy use of Swift frameworks sometimes doesn’t play
nicely with “whole module optimization”
• Binaries may not be compatible across compiler versions
tvOS Swift
30. Swift — Advantages
• Swift may not be mature, but it is ready for prime-time
• Swift absolutely fulfills the promise of more efficient
development
• An entire class of errors that used to require runtime
debugging is now caught by the compiler
• Building a stable app takes much less work than Objective-C
• I probably spent about ¼ the time debugging as I would
have had to do in ObjC
• Coding in Swift is an absolute pleasure
tvOS Swift
31. Swift — The Verdict
tvOS Swift
Do it!
You know you’ll “go Swift” eventually…
…and the longer you wait, the more you’ll be kicking yourself
for not having done it sooner once you finally do!
32. Evan Maloney Distinguished Engineer, Gilt Groupe
A parting gift for you
https://github.com/emaloney/CleanroomLogger
CleanroomLogger — A pure Swift logging engine
Simple, lightweight & performant
Code goodies!