Creating a Counter App
Try reloading this page, or reviewing your browser settings
In this video segment, you will learn more about the purpose and features of a Swift project.
- Apple Watch
About this video
- Ravin Sardal
- First online
- 06 April 2019
- Online ISBN
- Copyright information
- © Ravin Sardal 2019
In this video, I wanted to give an overview on what files are available to develop your Apple Watch project in Xcode and what each of them mean. So I’m going to start by creating a new Xcode project so that we can see all the files from when the project is first created and I’ll just call this Files Overview. And I’ll save it with the file system. Great. OK.
So you can see that up here, File Overview. So this is for the iOS app, these files over here. But in this series, we’re going to be dealing with WatchKit. So the WatchKit apps actually have two folders for them. One of the folders consists of the actual app interface files and then the other folder consists of the WatchKit extension files which is where the Watch is actually having its code execute.
So let’s start with the interface files. So if we go in File Overview, WatchKit app, we can see that there is an interface dot storyboard file. So in general, in Objective C or Swift programming, you’ll have these storyboard files. And these storyboard files allow you to create and design your applications interface. So when we click on the storyboard file, we’ll actually see an overview of what our Apple Watch app screen would look like when it was first launched.
And if we actually open up the Objects pane, we can actually add different objects to the screen and display different views. So I can add a switch or a label. You can even add sliders. There are a variety of different options. And we’ll talk about how you use these options in a further video.
But I just wanted to show you how– or sorry– what is– what is created and available at this screen. So the Assets file is where all the assets lie. So that’s where like you would put in any pictures that you want to add to your application. Let’s say your application displays a bunch of pictures of the screen or you want your button to look a certain way, you can add them to the Assets folder.
Right now there is an app icon assets so it gives you the option to add pictures for all sorts of sizes for the app icon. You can see that how the app icon for the Apple Watch will look at the app store or how it looks in the short look or the home screen or the Notification Center and it goes on and on.
The info p-list file in the WatchKit app folder is the final file that comes with the basic project structure. And this file is really for all of your local settings. It lets you choose see bundle name, whatever dictionary version you’re using, your development language. So this is a file we won’t really be using in our development series, but it’s just good to know what’s in the file and what it does.
OK. So now over to the WatchKit extension folder. So the first file you’ll see is the interface controller, but I’m actually going to start with the extension delegate. So what the extension delegate does is this is how– this is the file that handles all of the functions when your app first launches or when it comes from a background task. So if we actually look in our file, we’ll see a couple of functions that are already here from the start. And these are functions that I haven’t written, but these are functions that Apple has provided for us.
So let’s look at this first function, application did finish launching. So when the user decides to press the app icon and the app opens up, once the application– once the system finishes initializing the application, we, the developer, can write some more code that initializes our app fully to our own liking. Let’s say you want to add some settings or say some variables or say some values after the app launches, this is what you can do. This is a first function that the developer gets to use.
Again, this is when your application becomes active. So if your app was in the background and the user decides to wake it, this is the function that will be called right away. This is when the application is about to resign. So what Apple says here is when a phone call pops up or an SMS message pops up, and so the system interrupts the screen in some way, this is a function that is called. So let’s say your application is doing something and a phone call up pops up, you can choose to save some states or see some variables so that the user doesn’t lose that data.
And then this big function over here is for background task handling. And if you just go through, on your own time, you can see what each case does and how you can handle it. OK. So that’s the extension delegate file. And if you’ve worked on iOS apps before, the iOS framework provides a similar file called the app delegate.
OK. Now let’s go back to interface controller. So the interface controller is actually a parallel file with the interface storyboard. And why I’m saying parallel is because we have one interface over here. And when you go back to the storyboard file and that interface over here is controlled by this file over here. So this file is actually linked up to this interface over here. And if you actually go to the right-hand bar and click on the identity inspector, you can choose which class to give up to. And classes are just another word for files in our case.
So you can see that the interface controller is selected over here and then when you click on interface controller dot Swift, we can see the code that is controlling that view over there. And we can open up our system editor so you can see both things on each side. So now let’s go through this file.
So the first thing that you see in this file is the Awake function. So the Awake function is when this interface, this view over here, this specific view is about to– about to start up. So this isn’t when it’s been shown, but when the system knows that it’s about to show up. So you can configure any interface objects here. So like let’s say you want to download some data and get it ready to present to the user. Like if you’re doing a banking application on the watch, you can download the account balance and get it ready. And what the Will Activate function does is that it’s called right before your interface is shown to your users.
So let’s say you want to prepare some views like, OK, I want to change a couple of buttons on how they look or like switch states or labeled texts, we can do that in our Will Activate function. And then parallel to what Will Activate is our Did Deactivate function. So deactivate is when the view is about to disappear.
And when it’s disappeared, the Did Deactivate function gets called and the developer can choose to, let’s say, save some states or some variables to an external server or whatever they’d like to do, but that function is also available. And there are more functions that are available in our View Lifecycle, as it’s called by Apple, that we can use, but these are the basic functions.
OK. And again, in our WatchKit Extension folder, we have an assets file. And right now in our assets, they provide a folder for complications. We’re not going to be dealing with complications just yet, but just know that they’re provided for different screen sizes. As of now, the Apple Watch has four different screen sizes, but they may increase in the future so we have to provide different asset sizes for each one.
And again, the Extension folder has an info p-list that lets you set particular app settings, but we won’t be needing to modify this at all in our tutorial. And finally, the products is what is generated by your Xcode application. So once the Xcode builds the app, it appears in this products. So hopefully, in this tutorial, you’ve learned what to expect when developing a new Apple Watch application and how to navigate to each files. Thank you.