The iOS device family opens up a whole realm of possibilities for developers: Multitouch interfaces, always-on Internet access, video, and a whole range of built-in sensors can be used to create everything from games to serious productivity applications. Believe it or not, as a new developer, you have an advantage. You are starting fresh, free from any preconceived notions of what is possible in a mobile application. Your next big idea may well become the next big thing on Apple’s App Store.
This hour prepares you for your first development project. You’re about to embark on the road to becoming an iOS developer, but you need to do a bit of prep work before you start coding.
If you’re reading this book, you probably already have an iOS device, and that means you already understand how to interact with its interface. Crisp graphics, amazing responsiveness, multitouch, and hundreds of thousands of apps—this just begins to scratch the surface. As a developer, however, you need to get accustomed to dealing with a platform that, to borrow a phrase from Apple, forces you to “think different.”
The iOS platform family currently consists of the iPhone, iPad, iPod touch, and Apple TV; but at present, the Apple TV is not open for development. As you work on the tutorials in this book, you’ll notice in many screenshots that I focus on iPhone-centric projects. This isn’t because I’m lacking iPad love; it’s because it is difficult to capture iPad interfaces in a screenshot because they are so large. The good news is that if you want to develop a project on the iPad, you develop it on the iPad. If you want to develop it for the iPhone, you develop it for the iPhone. In almost all cases, the coding process is identical. In the few cases where it isn’t, I make sure that you understand what is different between the devices (and why). You’ll also find that each tutorial is available in an iPad and iPhone version on this book’s website (http://teachyourselfios.com), so you can follow along with a working application on whatever device you choose.
Note
Like Apple’s developer tools and documentation, I do not differentiate between the iPhone and iPod touch in the lessons. For all intents and purposes, developing for these devices is identical, although some capabilities aren’t available in earlier versions of the iPod touch (but the same can be said for earlier versions of the iPhone and iPad, as well).
The iOS devices offer a variety of different resolutions, but iOS provides a simple way of thinking about them. The traditional iPhone screen (before the iPhone 5), for example, is 320×480 points. Notice that I said points, not pixels. Before the release of the iPhone 4’s Retina display, the iPhone was 320×480 pixels. Now, the actual resolution of an iOS device is abstracted behind a scaling factor. This means that although you may be working with the numbers like 320×480 for positioning elements, your device may have more pixels than that. The iPhone 4 and 4s, for example, have a scaling factor of 2, which means that they are really (320×2) × (480×2) or 640×960 pixel resolution devices. The iPhone 5 changed things a bit by using a longer 320×568 point (640×1136 pixel) screen (see Figure 1.1). Although that might seem like quite a bit of screen real estate, remember that all these pixels are displayed on a screen that is roughly 4 inches diagonal.
The iPad family follows a similar pattern. The iPad 2 and first-generation iPad mini use screens with 768×1024 points and a scaling factor of 1. Retina display iPads still have a 768×1024-point screen, but with a scaling factor of 2 and a resolution of 1536×2048 pixels.
We take a closer look at how scaling factors work when we position objects on the screen throughout this book. The important thing to know is that when you’re building your applications, iOS automatically takes the scaling factor into play to display your apps and their interfaces at the highest possible resolution (with rarely any additional work on your part).
If you have a 27" cinema display on your desk, these handheld resolutions may seem limiting. Keep in mind, however, that desktop computers only recently exceeded this size, and many websites are still designed for 800×600. In addition, an iOS device’s display is dedicated to the currently running application. You have one window to work in. You can change the content within that window, but the desktop and multiwindow application metaphors are gone.
The screen limits aren’t a bad thing. As you’ll learn, the iOS development tools give you plenty of opportunities to create applications with just as much depth as your desktop software—albeit with a more structured and efficient interface design.
The graphics that you display on your screen can include complex animated 2D and 3D displays thanks to the OpenGL ES implementation available on all iOS devices. OpenGL is an industry standard for defining and manipulating graphic images that is widely used when creating games. Each year’s device revisions improve these capabilities with more advanced 3D chipsets and rendering abilities, but even the original iPhone has very respectable imaging abilities.
As with the HD displays on our desktops and laptops, we’ve grown accustomed to processors that can work faster than we can click. The iOS devices use a range of processors, from a ~400MHz ARM in the early iPhones to the multicore A6, A6X, and A7 in the currently selling iPhone and iPad devices. The “A” chips are a “system on a chip” that provide central processing unit (CPU), graphics processing unit (GPU), and other capabilities to the device, and this series is the first Apple-designed CPU series to be used in quite a while.
Because Apple has gone to great lengths to keep the iOS devices responsive, your device’s capability to multitask is limited. Starting in iOS 4, Apple created a limited set of multitasking application programming interfaces (APIs) for very specific situations. In iOS 7, the restrictions on these APIs have been loosened, but your application can never assume that it will remain running if iOS deems it to be a resource hog. iOS preserves the user experience above all else.
Another constraint that you need to be mindful of is the available memory. In the original iPhone, 128MB of RAM is available for the entire system, including your application. There is no virtual memory (slower storage space used as RAM), so you must carefully manage the objects that your application creates. In the latest models of the iPhone and iPad, Apple has graciously provided 1GB. This is great for us, but keep in mind that there are no RAM upgrades for earlier models.
The iPhone and iPad can always be connected to the Internet via a cellular provider (such as AT&T, Verizon, and Sprint in the United States). This wide-area access is supplemented with built-in WiFi and Bluetooth. WiFi can provide desktop-like browsing speeds within the range of a wireless hot spot. Bluetooth, in contrast, can be used to connect a variety of peripheral devices to your device, including a keyboard.
As a developer, you can make use of the Internet connectivity to update the content in your application, display web pages, and create multiplayer games. The only drawback is that applications that rely heavily on 3G or LTE data usage may have a smaller audience due to the costs associated with data plans, and the popularity of WiFi-only models. Design your apps for WiFi, but support conservative use of cellular data if you can.
iOS devices shine when it comes to input and feedback mechanisms and your ability to work with them. You can read the input values from the capacitive multitouch (up to 11 fingers on the iPad) screen, sense motion and tilt via the accelerometer and gyroscope (iPhone 4, iPad 2, and later), determine where you are using the global positioning system (GPS) (cellular-enabled devices required), see which way you’re facing with the digital compass (iPhone 4, iPad 2, and later), and understand how a device is being used with the proximity and light sensors. iOS can provide so much data to your application about how and where it is being used that the device itself truly becomes a controller of sorts—much like (but surpassing) devices such as the Nintendo Wii and PlayStation Move.
iOS devices also support capturing pictures and video (iPhone, iPad 2, and later) directly into your applications, opening a realm of possibilities for interacting with the real world. Already applications are available that identify objects you’ve taken pictures of and that find references to them online (such as the Amazon Mobile app) or perform real-time translation of printed text (Word Lens).
Finally, for each action your user takes when interacting with your application, you can provide feedback. This, obviously, can be visible feedback on the screen, or it can be high-quality audio and force feedback via vibration (iPhone only). As a developer, you can leverage all these capabilities (as you’ll learn in this book).
That wraps up our quick tour of the iOS platform. Never before has a single device defined and provided so many capabilities for a developer. As long as you think through the resource limitations and plan accordingly, a wealth of development opportunities await you.
Being an iOS developer requires more than just sitting down and writing a program. You need a modern Intel Macintosh desktop or laptop running OS X Lion, Mountain Lion, or Mavericks and at least 6GB of free space on your hard drive. The more screen space you have on your development system, the easier it is to create an effective workspace. You can even take Xcode into full-screen mode, removing all distractions. That said, I’ve worked perfectly happily on a 13" MacBook Air, so an ultra-HD multimonitor setup certainly isn’t necessary.
So assuming you already have a Mac, what else do you need? The good news is that there isn’t much more, and it won’t cost you a cent to write your first application.
Despite somewhat confusing messages on the Apple website, there really is no fee associated with joining the Apple Developer Program, downloading the iOS SDK (software development kit), writing iOS applications, and running them on Apple’s iOS Simulator.
Limitations do apply, however, to what you can do for free. If you want to have early access to beta versions of the iOS and SDK, you must be a paid member. If you want to load the applications you write on a physical device or distribute them via the App Store, you also need to pay the membership fee. Most applications in this book work just fine on the Simulator provided with the free tools, so the decision on how to proceed is up to you.
Perhaps you aren’t yet sure whether the paid program is right for you. Don’t worry; you can upgrade at any time. I recommend that you start with the free program and upgrade after you’ve written a few sample applications and run them in the Simulator.
Note
Obviously, things such as motion sensor input and GPS readings can’t be accurately presented in the Simulator, but these are special cases and aren’t needed until later in this book.
If you choose to pay, the paid Developer Program offers two levels: a standard program ($99) for those who will be creating applications that they want to distribute from the App Store, and an enterprise program ($299) for large (500+ employee) companies that want to develop and distribute applications in-house but not through the App Store. Most likely, the standard program is what you want.
The standard program is available for both companies and individuals. If you want to publish to the App Store with a business name, you are given the option of choosing a standard “individual” or “company” program during the registration.
Big or small, free or paid, your venture into iOS development begins on Apple’s website. To start, visit the Apple Developer Registration Center (https://developer.apple.com/programs/register/), shown in Figure 1.2. If you have an existing Apple ID from using iTunes, iCloud, or other Apple services, you can use that ID for your developer account. If not, or if you want a new ID to use solely for development, you can create a new Apple ID from the registration site as well.
Assuming you have an ID, click the Sign In button, and then log in using your credentials.
The registration process then walks you through the process of agreeing to Apple’s Developer terms, and collecting information about you or your company, development interests, and experience, as shown in Figure 1.3.
After you have registered, you can decide to join a paid program or to continue using the free resources. If you choose to join a paid program, point your browser to the iOS Developer enrollment page (http://developer.apple.com/programs/ios/) and click Enroll Now, as shown in Figure 1.4.
Read the introductory text, and then click Continue to begin the enrollment process. When prompted, choose to sign in with your existing Apple ID.
The registration tool then asks you to choose between the individual and company options, as shown in Figure 1.5. Make your choice, and then follow the prompts to complete your membership purchase.
Unlike the free developer membership, the paid Developer Program does not take effect immediately. When the App Store first launched, it took months for new developers to join and be approved into the program. Today, however, it might take a few hours or a day. Just be patient.
Thanks to the Mac App Store, downloading Xcode (the iOS development suite) is as easy as point and click. Open the App Store from your Dock, search for Xcode, and download it for free, as shown in Figure 1.6. Sit back while your Mac downloads the large (~3GB) application.
Tip
If you have the free developer membership and log in to the iOS Dev Center, you’ll likely see just a single installer for Xcode and the iOS SDK (the current-release version of the development tools). If you’ve become a paid program member, you may see additional links for different versions of the SDK (7.1, 8.0, and so on). The examples in this book are based on the 7.0+ series of iOS SDKs, so be sure to choose that option if presented.
If you’re interested in the cutting-edge beta releases of Xcode, you can download preview versions directly from the iOS Dev Center (http://developer.apple.com/ios).
When the download completes, you have either a disk image (if you downloaded a preview release from the iOS Developer site) or an application (if you downloaded from the Mac App Store). Open the disk image, if necessary, and copy the application to your Mac’s Applications folder. You can have multiple copies of Xcode installed, so feel free to install both the beta from the dev center and the current stable release from the Mac App Store.
Xcode is interesting in that it is both an application that provides a development environment and a wrapper around a collection of standalone tools (such as the iOS Simulator). These additional tools are launched from within Xcode. Xcode itself is started like any other application, by using the Finder or Launchpad (see Figure 1.7).
Once the Xcode application itself is installed, you might still need to install some additional software before you can use it. Launch Xcode from the Finder or Launchpad, and then wait for it to start. If it’s your first time using the application, you’ll likely be presented with a license agreement, and then Xcode will install updates and additional components to support iOS development (see Figure 1.8). If prompted, click Install to continue.
When the installation completes, you can exit out of Xcode by choosing Quit from the Xcode menu. Your development environment is now ready to go. Although we do not get into real development for a few more hours, we will be configuring a few options in Xcode in the next section, so make sure you’ve properly installed the software before proceeding.
Even after you’ve obtained an Apple developer membership, joined a paid Developer Program, and downloaded and installed the iOS development tools, you still cannot run any applications that you write on your actual device. Why? Because you haven’t created a development provisioning profile.
Caution: For Paid Developers Only
Only paid developer accounts can complete the following steps. If you have a free developer account, don’t fret; you can use the iOS Simulator to test your apps until you’re ready for a paid membership. Skip ahead to “Running Your First iOS App,” later in this hour.
In many development guides, this step isn’t covered until after development begins. In my mind, once you’ve written an application, you’re going to want to run it on a real device immediately. Why? Because it’s just cool to see your own code running on your own iPhone or iPad.
Like it or not, Apple’s current approach to iOS development is to make absolutely certain that the development process is controlled—and that groups can’t just distribute software to anyone they want. The result is a rather confusing process that ties together information about you, any development team members, and your application into a provisioning profile.
Tip
If you don’t see your iPhone or iPad appear after a few seconds, make sure that your device is unlocked and connected with a properly licensed cable. Beginning in iOS 7, your device will also prompt you as to whether it should trust the connection to your computer. Be sure to allow the trusted connection; otherwise, you won’t be able to test applications on your device.
A development provisioning profile identifies the developer who may install an application, an ID for the application being developed, and the “unique device identifiers” for each device that will run the application. This is only for the development process. When you are ready to distribute an application via the App Store or to a group of testers (or friends) via ad hoc means, you must create a separate “distribution” profile. Because you’re just starting out, this isn’t something you need right away.
In the past, creating a provisioning profile for the sole purpose of development was a frustrating and time-consuming activity that took place in an area of the iOS developer site called the Provisioning Portal. Apple has dramatically streamlined the process in recent versions of Xcode, making provisioning as simple as connecting your device and clicking a button.
To install the development profile, first make sure that your device is connected to your computer, and then launch Xcode. When Xcode first launches, close any “welcome” windows that appear, and then immediately choose Window, Organizer from the menu bar. Click the Devices icon on the Organizer toolbar. You should see your iOS device listed in the leftmost column of the Organizer under the Devices section, as shown in Figure 1.9.
Click the Devices icon to select it, and then click the Use for Development button (see Figure 1.9). Xcode will prompt you to add an Apple ID that is enrolled in a Developer Program. Click Add, and then provide the Apple ID login associated with your paid developer membership, as shown in Figure 1.10. You can update or change the Apple ID associated with your development using the Accounts section of the Xcode Preferences. (Choose Xcode, Preferences from the menu bar.)
In the background, Xcode is adding a unique identity to the iOS developer portal that identifies you and will be used to digitally sign any applications you generate; this is called the signing identity. It also registers your device with Apple so that it can run the software you create (and beta releases of iOS). If this is the first time you’ve been through the process, you are asked whether a development certificate should be generated. Click Submit Request to continue.
Xcode continues to communicate with Apple to create a development profile that will be named iOS Team Provisioning Profile and to create a unique App ID. This ID identifies a shared portion of the iOS device keychain to which your application will have access.
Note
In case you’re wondering, Apple’s Mac Developer Program creates and maintains similar profiles for deploying to the Mac App Store. The biggest difference is that unlike with iOS apps, you are free to run your Mac apps on any machine you want. In the Mac development program, your certificates will be used for deploying to the App Store and for testing Apple-hosted services like Game Center and iCloud integration. In iOS development, you need the appropriate certificates installed to run your software anywhere outside of the iOS Simulator.
The keychain is a secure information store on iOS devices that you can use to save passwords and other critical information. Most apps don’t share a keychain space (and therefore cannot share protected information). If you use the same App ID for multiple applications, however, those applications can share keychain data.
For the purposes of this book, there’s no reason the tutorial apps can’t share a single App ID, so letting Xcode generate an ID for us is just fine. Xcode will, in fact, create a “wildcard” App ID that will be applied to any application you create using the team provisioning profile.
Finally, Apple’s servers use all of this information, along with the unique identifier of your connected iOS device, to provide Xcode with a completed provisioning profile. Xcode then transparently uploads the profile to your device the first time Xcode attempts to run an application on it. Assuming everything went according to plan, you should see the little dot to the right of your iOS device icon in the Organizer turn green.
To confirm that your signing identity and provisioning profile are installed, choose Xcode, Preferences, and then click the Accounts icon on the toolbar of the window that appears. Click to highlight your Apple ID in the list of accounts, and then click View Details button in the lower-right corner. You should see both a signing identity and provisioning profile listed, as shown in Figure 1.11. Congratulations! You’re ready to run an application on your device.
Tip
After you have a single development machine configured, you can easily configure other workstations by exporting your signing identities and provisioning profiles. To do this, open the Xcode preferences (Xcode, Preferences) and click the Accounts icon on the toolbar. Click the gear icon at the bottom of the Accounts list, and choose Export Accounts. This option (and the corresponding Import Accounts selection) export (and subsequently import) all your developer profiles/certificates in a single package.
It seems wrong to go through a whole hour about getting ready for iOS development without any payoff, right? For a real-world test of your efforts, let’s actually try to run an application on your iOS device. If you haven’t downloaded the project files to your computer, now is a good time to visit http://teachyourselfiOS.com and download the archives.
Now let’s run that app:
1. Within the Hour 1 Projects folder, choose between iPhone and iPad versions of the project, and then open the Welcome folder. Double-click Welcome.xcodeproj to open a simple application in Xcode. When the project opens, your display should look like Figure 1.12.
Tip
If you feel adventurous, a tutorial is available on http://teachyourselfiOS.com that will teach you about the basics of Xcode source control, including how you can connect to a live repository of the latest versions of all the book’s sample projects.
2. Make sure that your iOS device is plugged into your computer.
3. Using the right side of the pop-up menu in the upper-left corner of the Xcode window (called the Scheme menu), choose your iOS device, as shown in Figure 1.13. This tells Xcode that when the project is built it should be installed on your device, not run in the Simulator. If you don’t have a paid developer membership, choose one of the Simulator options, depending on which version of the project you’re using.
You’ll usually only see options for running an application on either a variation of the iPhone Simulator or the iPad Simulator. In Figure 1.13, you see all the available Simulator options. This occurs when you are using an iPhone-targeted app (because they run on both iPhones and iPads) or when an app is a universal application (more on that later in the book). In this case, the screenshot was captured using the iPhone version of the project so that you could see all the options.
4. Click the Run button in the upper-left corner of the Xcode toolbar. After a few seconds, the application should be installed and launched on your device. Because I can’t actually show you my device running the app, Figure 1.14 demonstrates what it looks like in the iPhone Simulator.
5. Click Stop on the Xcode toolbar to exit the application. You can now quit Xcode. We’re done with it for the day.
When you clicked Run, the Welcome application was installed and started on your iOS device. It remains there until you remove it manually. Just touch and hold the Welcome icon until it starts wiggling, and then delete the application as you would any other. Applications installed with your development certificate stop working when the certificate expires (120 days after issuance).
Over the course of the next few hours, you are introduced to the technologies that you’ll be using to create iOS applications. The goal is to get you up to speed on the tools and technology, and then you can start actively developing. This means you’re still a few hours away from writing your first app, but when you start coding, you’ll have the necessary background skills and knowledge to successfully create a wide variety of applications.
In this hour, you downloaded and worked with the Xcode application. This, coupled with the iOS Simulator, will be your home for the duration of the book. These two applications are so critical and feature packed that we spend a couple more hours (Hour 2, “Introduction to Xcode and the iOS Simulator,” and Hour 5, “Exploring Interface Builder”) covering their capabilities and use.
It’s worth mentioning that almost every iPhone, iPad, iPod, and Macintosh application you run, whether created by a single developer at home or by a huge company, is built using the Apple developer tools. This means that you have everything you need to create software as powerful as any you’ve ever run.
Objective-C is the language that you’ll be using to write your applications. It provides the structure for our applications and controls the logic and decision making that goes on when an application is running.
If you’ve never worked with a programming language before, don’t worry. Hour 3, “Discovering Objective-C: The Language of Apple Platforms,” covers everything you need to get started. Developing for iOS in Objective-C is a unique programming experience, even if you’ve used other programming languages in the past. The language is unobtrusive and structured in a way that makes it easy to follow. After your first few projects, Objective-C will fade into the background, letting you concentrate on the specifics of your application.
Objective-C defines the structure for iOS applications, and Cocoa Touch defines the functional building blocks, called classes, that can make iOS devices perform certain actions. Cocoa Touch isn’t a “thing,” per se, but a collection of interface elements, data storage elements, and other handy tools that you can access from your applications.
As you’ll learn in Hour 4, “Inside Cocoa Touch,” you can access literally hundreds of different Cocoa Touch classes and do thousands of things with them. This book covers quite a few of the most useful classes and gives you the pointers necessary to explore even more on your own.
The iOS platform and Macintosh use a development approach called Model-View-Controller (MVC) to structure applications. Understanding why MVC is used and the benefits it provides will help you make good decisions in structuring your most complex applications. Despite the complicated-sounding name, MVC is really just a way to keep your application projects arranged so that you can easily update and extend them in the future. You learn more about MVC in Hour 6, “Model-View-Controller Application Design.”
Xcode is the cornerstone of your iOS development experience. You will design, code, and test your apps in Xcode. You’ll provision your devices and even submit apps to the App Store, all through Xcode. Noticing my emphasis yet? Xcode. Xcode. Xcode. Although we’ll be spending time going through the Xcode features now, take a moment to watch Apple’s introductory videos to get a sense for what you’ll be seeing. To do this, open Xcode, and then choose Xcode Help from the Help menu.
The more you familiarize yourself with the tools, the quicker you’ll be able to use them to build production-ready applications.
This hour introduced you to the iOS platform, its capabilities, and its limitations. You learned about the different iOS devices’ graphic features, RAM sizes, and the various sensors that you can use in your applications to create uniquely “aware” experiences. We also discussed the Apple iOS developer tools, how to download and install them, and the differences between the various paid Developer Programs. To prepare you for actual on-phone development, you explored the process of creating and installing a development provisioning profile in Xcode and even installed an application on your device.
The hour wrapped up with a quick discussion about the development technologies that make up the first part of this book and form the basis for all the iOS development you’ll be doing.
Q. I thought that iOS devices ranged from a minimum of 16GB of RAM in the low-end iPad and iPhone to 128GB on the high-end models. Don’t they?
A. The “memory” capabilities of devices that are advertised to the public are the storage sizes available for applications, songs, and so forth. It is separate from the RAM that can be used for executing programs. If Apple implements virtual memory in a future version of iOS, it is possible that the larger storage could be used for increasing available RAM.
Q. What platform should I target for development?
A. That depends on your goals. If you want to reach the largest audience, consider a universal application that works on the iPhone, iPad, and iPod touch. We explore this in a few projects later in this book. If you want to make use of the most-capable hardware, you can certainly target the unique capabilities of a specific device, but by doing so you might be limiting the size of your customer base.
Q. Why isn’t the iOS platform open?
A. Great question. Apple has long sought to control the user experience so that it remains “positive” regardless of how users have set up their device, be it a Mac, an iPad, or an iPhone. By ensuring that applications can be tied to a developer and by enforcing an approval process, Apple attempts to limit the potential for a harmful application to cause damage to data or otherwise negatively impact the user. With nearly a million active applications on the App Store, I think Apple must be doing something right.
1. Establish an Apple developer membership and download and install the developer tools. This is an important activity that you should complete before starting the next hour’s lesson (if you didn’t do so while following along in this hour).
2. Review the resources available in the iOS Dev Center. Apple has published several introductory videos and tutorials that supplement what you’ll learn in this book.