Developing for the Apple Watch in Swift
Total Page:16
File Type:pdf, Size:1020Kb
Developing for The Apple Watch in Swift Hampus Nilsson Computas @hjnilsson What’s Up? • Brief about the Watch • Experiment with Swift • Get started with our App • WatchConnectivity etc. Doing iOS Development • Been working with iOS since late 2009 • Computas mainly make apps for government entities • I made the Student ID app • In consulting: Xamarin / web apps are more appropriate right now Quick Brief of Apple Watch • Apple Watch is still new, everyone might not have one • The Interface might be alien • I’ll do a quick brief of the UI so we’re all on the same page • If you already have a Watch, now is an excellent time to play with it! Watch face & Home screen Notifications • Notifications are accessed from the Watch Face • Swipe down • Essentially the same as on the Phone • Can open the app by tapping Force Touch • Or 3D Touch • A “through-the-screen” tap • Push firmer than normally, a secondary type of tap • Used to display options & menus on the watch Glances • Accessed from Watch Face by swiping up • Short previews of apps • Can be updated from phone • Won’t implement this today Text input • Yes it’s possible • Done either using Siri, or by suggestion-based input Using Apps (Trivia Crack) • You launch an app from the home screen • Apps run on device, and do not need the phone to function. • App interaction should be quick • Unless it’s a workout app, it’s dismissed automatically when you “turn off” the watch App Opportunity! • New market (especially in Norway) • Since watchOS 2, there are plenty of opportunities for innovation • “Relatively” easy platform to work with (especially coming from iOS) • A very limited platform = easy to make apps Where to start? • Get a Mac • … also an iPhone… and a Watch • iOS development is not cheap. :) • Then get XCode from the App Store, no other programs necessary We need an idea • Something that’s not included on the Watch • That is useful on it’s own • And is not connected to a product (this is usually the challenge) = Let’s make a Calculator The Watch does not ship with one! ( Outrageous! ) “This should only take an hour” - Every developer, ever We need to learn Swift • We’ll have a short live intro! • Imperative language with functional concepts • Modern • Especially compared to Objective-C Swift Lightning Course • Step 1: Basic flow • Step 2: Types & Functions • Step 3: Working with NS* / UI* Playground! Let’s get going BREAK Back on Track • We learned some Swift • Basic types, loops, classes & optionals • Now it is time to dive into WatchOS Step 0 • Set up the Project • I’ve done this for you here: git clone [email protected]:hjnilsson/ watch-calculator.git • This is tied to com.hjnilsson.watch-calc, less that ideal for you if you try it on the Phone etc. Step 1: Building the Interface • UI is built in Interface Builder, just as far iOS • Set of basic controls are: Labels, Images, Buttons, Pickers, Tables • Uses a stack layout (rows & columns, similar to HTML flexbox) • No dynamic interface - everything must be present always, can only show/hide elements Step 1: Building the Interface • For our app we need: • A set of buttons to input the calculation • A label to display the ongoing calculation & result Step 2: Connecting the Interface • Time to write some code • Clicking the buttons should add digits to the label • A clear button is very useful as well Step 3: Calculate! • We need to actually figure out what 2+2 resolves to • Easy on iOS (there we can cheat), for watchOS we need to do the hard work of parsing. • We’ll make a very dumb parser (simple Left-to-Right) Step 4: More Actions • Finally, let’s add some more actions via Force Touch • Clear can be a Force Touch action • Then we have space for a [ . ] button • Sum can also be one Bonus step: History • We’ll store completed sums • Do so in the WatchExtension class (global singleton; equivalent of AppDelegate on iOS, or Program in C#) • Display them as a new page next to the calculator interface So that’s it! You could almost submit this to the App Store Just need to finish it… Finishing an App • Making the “Core Interaction” is the easy part • Stuff that takes time: • Making all the ancillary functions • Fixing bugs • Arriving to something you are proud of • Addendum: Apps don’t make money, making apps makes money Debugging • Atrocious experience on the Watch • Rarely works like the simulator • Laggy & buggy • Will take time if you have an advanced app DEBUGGING More resources • WWDC Videos: https://developer.apple.com/videos/ wwdc2015/ • WatchKit in Depth 1 & 2 • WatchKit Tips & Tricks • Introducing Watch Connectivity • NSHipster: nshipster.com • Core Intuition podcast (& Slack), coreint.org Questions? If you want the working code it can be found here: git clone [email protected]:hjnilsson/ watch-calculator.git working Hampus Nilsson Computas @hjnilsson / hjnilsson.com.