Chapter
Setting Up the Development Environment
It is more fun to see apps run than to read the source code, and you cannot get hands-on programming experience by just reading books. Lets get the development environment up and running first so you can use itand learn Swift programming for iOS along the way.
Xcode and the iOS SDK
ANDROID ANALOGY
The Android Developer Tools (ADT plug-in for Eclipse or Android Studio.
Xcode is the complete toolset for building iOS apps. It is an integrated development environment (IDE) that helps you build, test, debug, and package your iOS apps. It is free but you must have an Intel-based Mac running Mac OS X Mavericks or later. You will use the latest Xcode, version 6, throughout this book.
Installing from the Mac App Store
Xcode is distributed in the Mac App Store shows Xcode in the Mac App Store app.
. Xcode in Mac App Store
All you need to do is install the latest Xcode from the Mac App Store. After completing the installation, go ahead and launch Xcode from the Applications folder. Keep it in the Mac OS Dock so that you can launch it at any time.
The first time you launch Xcode, it immediately prompts you to install the required components (see ). Click Install to complete the Xcode installation.
. Install the required components
After the required components are installed, you should see the screenshot in . Your iOS IDE, Xcode, is ready!
. Welcome to Xcode
Create an iOS Project Using the Template
ANDROID ANALOGY
New Android Application Project template in ADT.
Youve got the right tool; now, wouldnt you like to see some real actionlike creating an iOS app and seeing it run? Id like that, too! You want to do this to ensure your IDE is working properly as well.
I actually created my very first Android app using the ADT New Android Application Project template when I had no idea how to create Android mobile apps. All I wanted was to see something running in no time. Yep, ADT did it for me nicely. I was very happy with myself when I felt Id created an Android app without knowing anything! Hey, theres nothing wrong with making yourself happy, right?
Xcode offers the same thing. The objective of this section is to show you how to create an iOS app as quickly as possible. Hold any programming questions so you can finish the project as fast as you can. For now, complete the following steps:
- Launch Xcode if you havent launched it yet.
- Select Create a new Xcode project from the Welcome to Xcode screen (see shows the prompt that asks you to choose a template for your project:
- In the left panel of , select iOS Application.
- In the right panel of , you may choose any of the templates. Just for fun, choose Game.
- Click the Next button.
- depicts the basic project info that requires you to fill in the following:
- Product Name: This is the app name. Name your project LessonOne .
- Organization Name: Optional; for example, Your organization or any name you choose.
- Organization Identifier: Together with the product name, the organization identifier should uniquely identify your app. Reverse domain name is recommended (e.g., com.yourdomain.xxx ).
- Language, Game Technology, and Device: You dont need to change these settings.
- Click the Next button when done.
- Select a folder in which to save your LessonOne project.
That is it! You just created an iOS project, the LessonOne project as shown in .
The LessonOne project can be seen on the left-hand side panel as shown in ; this is the Project Navigator in the navigation area. Just as when you use ADT project creation templates, the Xcode project template creates the project folder, the application source code, and all the resources for building the LessonOne apps.
Build the Project
ANDROID ANALOGY
The Eclipse ADT Build action keyboard shortcut on the Mac is the same as the build command in Xcode: Command+B (z+B). In Windows the Eclipse build shortcut is Control+B.
To build and compile the Xcode project, use the Build action, which is located in the Xcode menubar Product Build (or z+B). You will get used to using the z+B keyboard shortcut a lot because Xcode doesnt automatically build your code, unlike Eclipse ADT, which builds it automatically by default.
Launch the App
The LessonOne project should have no errors. You can launch the app and see it run on an iOS emulator. The emulator is a very important piece of any IDE. Unlike ADT, there is no need to mess with something like AVD Manager to create an emulator. All the emulators are right there in Xcode and you can launch the LessonOne project onto the selected device, including the iOS emulator, with a click on the triangle button in the upper left corner as shown in .
Alternatively, you may use an Xcode keyboard shortcut key for the Run action, Command+R (z+R), to launch the app. You should see your LessonOne app.
. LessonOne app in Emulator
Play with the app, and then select other emulators from the device emulator selector (see the pointer in ). A mouse-click event on an emulator is equivalent to a touch event, and three-finger movement on the trackpad is equivalent to touch-drag on a physical iOS screen. If you dont have a particular device yet, definitely play with the emulator to get familiar with the emulated iOS devices.
Tip To change to landscape or portrait orientation, press z+left arrow or right arrow to rotate the emulator.
The iOS emulators are way better than AVDvery robust and responsive, and they behave just like real devices. For learning Swift programming for iOS, the emulator actually is better, as iOS developers use emulators much more frequently than Android developers. In this book, you are not required to run apps on a physical iOS device; for that you would need to be a registered iOS developer and an iOS device. You can save the $99 iOS developer membership fee until you are ready to submit your first app to the App Store or if your app requires certain features not available in the emulator (e.g., the camera or certain sensors). For now, if your app is launched and running on an iOS emulator, your mission is completed!
Next page