Join Simon Allardice for an in-depth discussion in this video Creating a simple iOS app, part of iOS 8 App Development with Swift 1 Essential Training.
- I get it, we want to dive right in and just start making apps, but right at the beginning of iOS development is where many programmers hit their first sticking point and often sooner than they expect because most of the time when we begin a new language or technology we can make a very simple application. A simple program to get a feel for how this new technology works and how everything hangs together. But with iOS development, when you create that first simple app you usually end up with more questions than when you started. So, here is why.
Into Xcode 6 it gives us the welcome screen and I need to make a new project. For right now, just think project equals application. Whenever you want to make an app, you're going to make a new project in Xcode. It's just a way of keeping all your relevant files together. And I can start from the welcome screen or if that's closed I can use the Xcode File menu and say New Project. They both do the same thing. And we'll get the new project window. We have a lot of choices here because Xcode just isn't for iOS development, it's also used for developing programs for the Mac desktop, and command line tools, and creating shared libraries you can share across multiple applications.
So, what we're looking at here is the two main sections, one for OS X and one for iOS. Not surprisingly we're living in iOS in this course and we'll live in the iOS application section. With that selected it shows us a collection of project templates, useful starting points for a few common types of iOS applications. We will come back to explore these templates but for now the one we're going to use is the Single View Application. So, with that selected click next and we're asked to provide some name details for this project, this app.
Now, this screen and all the information it demands can seem like overkill when you're brand new to Xcode and you just want to try a few things out, but bare in mind Xcode doesn't know if we're making a simple demo or if this project will be a number one hit in the app store installed on ten million devices. So, it wants you to begin by naming everything well. And the most important name is first the product name, the name for your application. So, here don't typically use spaces. I could call it Demo. I could call it Example. I'll just call this FirstDemo.
Then onto Organization Name. If you're a solo developer, this could be your name. If it's a company, your company name. If you leave this blank, and you can, it will use the current username of whoever's logged on. Now, this is not technically all that important but whatever you put in here is used in comments when you make a new file in this project. On to the next one and as for an Organization Identifier, and you do need something here because if we did end up putting this project, this application, up on the iOS app store the product name, FirstDemo at the top, well, that might be unique but I can't guarantee it's unique.
There's well over a million applications in the app store now. But by adding the name of my Organization, even if that's just my own domain name I can make sure it's unique. Now, by convention, this is usually your domain name in reverse. So, I could say com.lynda or I could say com.simonallardice. Whatever I'm gonna do with this application. Now, you don't actually have to have a domain here and Xcode doesn't check that you own anything. So, if you don't have one just put something generic for now like com.yourcompany. And notice that what it's doing is combining the Product Name from the top and the Organization Identifier to create what's called a bundle identifier here.
This is a unique name for the final compiled and packaged application. Down to Language we have a choice of the older Objective-C and the newer Swift and everything we do in this course will be in Swift. And on to Devices, mostly we'll use iPhone here because it's the most common and it fits easier on the reduced screen size I'm currently recording with. There is an option for an iPad only app and one for Universal. And Universal means one iOS app that can substantially change it's appearance based on the device it's running on.
Now, that would add a little complexity to our first simple demo project. So, we won't do Universal yet, choose iPhone. And likewise the core data option would add complexity we don't need, so keep that unchecked and on to next. Now, finally we're asked where to save this new project and all its files, and you can put this stuff anywhere you want. There is no magic location for storing Xcode projects. I'm going to just save it onto my desktop. You can put it in your documents folder or anywhere else. At the bottom of this panel is an option for source control, for managing changes to our project files.
Now, Xcode understands both Git and Subversion, but even though I am a huge fan of source control, this is a discussion for another day. And this course is not about source control, so I'll be leaving that unchecked. I click Create and we have our project. We will dive deep into Xcode shortly but for now we see the project navigator, all the different files of the this project over here on the left. I can single click these and they will open up for editing in the middle section. If I select a file that represents code I get a code editor, and we have a few code files here at Delegate.swift, ViewController.swift, with some possibly threatening looking code, if you've never done iOS before.
Now, if I select a file that represents a user interface such as this Main.storyboard file here, I get Xcode's user interface editor, also known as the Interface Builder. And I see a blank canvas area representing our first iPhone screen with the battery staters at the top. Like most other programming IDEs, Xcode lets us write code, test code, design user interfaces all in the same application. And like many IDEs, it let's me drag and drop UI elements onto the screen: buttons, labels, text fields, etc.
And in Xcode the place to find those is called the Object Library. That's over here in this right-hand section. There's a lower section here and you want the third icon from the left. This is called the Object Library. Now, if you lose this section you can get to it from the View menu. It's View, Utilities, Show Object Library. Now, I want a simple label, I can scroll through this section and I can either find that or I could filter using the search box at the bottom. So, I see label here. I'm just going to click and drag that over onto this user interface screen and put it somewhere in the top-left area, then I'll double click it and type the traditional, albeit cliche phrase, Hello World.
Looks a little small so I want to adjust the size of the font. So, with this label selected so I see the grab handles around it, I'll go over to the right side again and I want in the top section the fourth icon from the left. This is called the Attribute Inspector and it let's us change properties of whatever's currently selected. So, here I'll just use the button to increase the size of this font. Let's make it, say thirty six. We're not seeing this all here on our screen so I'll grab the lower right handle and just re-size that.
Okay, as simple as this is I now want to test this application. So, up to the toolbar in Xcode I could just go ahead and click this Play button to run our application but there is an option here that says how we're gonna run it. The iOS simulator, which is what we're going to use, can act as multiple different devices. So, by clicking here I actually see many multiple options under iOS simulator: the iPad 2, the iPad Air, the 4S, the 6, Resizable ones. We'll get to most of this.
For now, I'm going to pick a middle of the pack, iPhone 5, and go ahead and click the Run button. Xcode compiles and launches our application. It's going to automatically open up the iOS simulator, install our program onto it, and run it, and we get our screen with our label. Not a very impressive application but this is running and this is working. I see Xcode in the background. I'm just gonna go ahead and press the Stop button on the toolbar up here just to stop the iOS simulator and come back into Xcode. Okay, this is very simplistic.
We haven't added any behavior, even the simplest bit of code, but you're probably already starting to form a lot of questions. Why did we pick the single view application Xcode project instead of the other ones? Why the different code files over here and what do they represent? Where would we write some swift to change this label? What exactly is a storyboard? And if you clicked around a bit, you'd probably have even more questions. What is a ViewController exactly? What is a first responder? And perhaps even more fundamental, as far as I know you can't buy a square iPhone or a square iPad so why does this default user interface provided by Xcode look square? In short, what's immediately important here and what's not.
And we're going to get to all of this. We first need to take our perspective up a level about the best way to approach everything.
Author Simon Allardice also covers the dos and don'ts of iOS 8 interface development, techniques for connecting UI elements to code, and tips for making flexible layouts that display correctly on different screen sizes. The last chapter shows you how to add app icons and launch screens and prep your app for submission to the App Store.
- Installing Xcode and the iOS SDK
- Joining the iOS Developer Program
- Using MVC in iOS
- Creating basic interaction
- Using first responders
- Exploring delegation
- Connecting UI elements to code
- Working with foreground and background events
- Creating and customizing table views
- Exploring storyboards
- Understanding the differences in iPad development
- Altering views and constraints, with size classes
- Adding application icons and launch images