Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 1: Welcome to the Jungle

5

You should also be familiar with iOS itself, as a user. Just as you would with any platform for which you wanted to write an application, get to know the nuances and quirks of the iPhone, iPad, or iPod touch. Take the time to get familiar with the iOS interface and with the way Apple’s iPhone and/or iPad applications look and feel.

NEW TO OBJECTIVE-C?

If you have not programmed in Objective-C before, here are a few resources to help you get started:

Check out Learn Objective-C on the Mac, an excellent and approachable introduction to Objective-C by Mac programming experts Mark Dalrymple and Scott Knaster (Apress, 2009):

http://www.apress.com/book/view/9781430218159

See Apple’s introduction to the language, Learning Objective-C: A Primer:

http://developer.apple.com/library/ios/#referencelibrary/

GettingStarted/Learning_Objective-C_A_Primer

Take a look at The Objective-C Programming Language, a very detailed and extensive description of the language and a great reference guide:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Ob jectiveC

That last one is also available as a free download from iBooks on your iPhone, iPod touch, or iPad. It’s perfect for reading on the go! Apple has released several developer titles in this format, and we hope that more are on the way. Search for “Apple developer publications” in iBooks to find them.

What’s Different About Coding for iOS?

If you have never programmed in Cocoa or its predecessors NeXTSTEP or OpenStep, you may find Cocoa Touch—the application framework you’ll be using to write iOS applications—a little alien. It has some fundamental differences from other common application frameworks, such as those used when building .NET or Java applications. Don’t worry too much if you feel a little lost at first. Just keep plugging away at the exercises, and it will all start to fall into place after a while.

If you have written programs using Cocoa or NeXTSTEP, a lot in the iOS SDK will be familiar to you. A great many classes are unchanged from the versions that are used to develop for Mac OS X. Even those that are different tend to follow the same basic principles and similar design patterns. However, several differences exist between Cocoa and Cocoa Touch.

Regardless of your background, you need to keep in mind some key differences between iOS development and desktop application development. These differences are discussed in the following sections.

www.it-ebooks.info

6CHAPTER 1: Welcome to the Jungle

Only One Active Application

On iOS, only one application can be active and displayed on the screen at any given time. Since iOS 4, applications have been able to run in the background after the user presses the home button, but even that is limited to a narrow set of situations, and you must code for it specifically.

When your application isn’t active or running in the background, it doesn’t receive any attention from the CPU whatsoever, which will wreak havoc with open network connections and the like. iOS 5 makes great strides forward in allowing background processing, but making your apps play nicely in this situation will require some effort on your part.

Only One Window

Desktop and laptop operating systems allow many running programs to coexist, each with the ability to create and control multiple windows. However, iOS gives your application just one “window” to work with. All of your application’s interaction with the user takes place inside this one window, and its size is fixed at the size of the screen.

Limited Access

Programs on a computer pretty much have access to everything the user who launched them does. However, iOS seriously restricts what your application can access.

You can read and write files only from the part of iOS’s file system that was created for your application. This area is called your application’s sandbox. Your sandbox is where your application will store documents, preferences, and every other kind of data it may need to retain.

Your application is also constrained in some other ways. You will not be able to access low-number network ports on iOS, for example, or do anything else that would typically require root or administrative access on a desktop computer.

Limited Response Time

Because of the way it is used, iOS needs to be snappy, and it expects the same of your application. When your program is launched, you need to get your application open, preferences and data loaded, and the main view shown on the screen as fast as possible—in no more than a few seconds.

At any time when your program is running, it may have the rug pulled out from under it. If the user presses the home button, iOS goes home, and you must quickly save everything and quit. If you take longer than five seconds to save and give up control, your application process will be killed, regardless of whether you are finished saving.

www.it-ebooks.info

CHAPTER 1: Welcome to the Jungle

7

Note that in iOS 5, this situation is ameliorated somewhat by the existence of new API that allows your app to ask for additional time to work when it’s about to go dark.

Limited Screen Size

The iPhone’s screen is really nice. When introduced, it was the highest resolution screen available on a consumer device, by far.

But the iPhone display just isn’t all that big, and as a result, you have a lot less room to work with than on modern computers. The screen is just 640 × 960 on the latest retina display devices (iPhone 4 and fourth-generation iPod touch) and 320 × 480 pixels on older devices. And that 640 × 960 retina display is crammed into the same old form factor, so you can’t count on fitting more controls or anything like that; they will all just be higher resolution than before.

The iPad increases the available space a bit by offering a 1024 × 768 display, but even today, that’s not so terribly large. To give an interesting contrast, at the time of this writing, Apple’s least expensive iMac supports 1920 × 1080 pixels, and its least expensive notebook computer, the MacBook, supports 1280 × 800 pixels. On the other end of the spectrum, Apple’s largest current monitor, the 27-inch LED Cinema Display, offers a whopping 2560 × 1440 pixels.

Limited System Resources

Any old-time programmers who are reading this are likely laughing at the idea of a machine with at least 256MB of RAM and 8GB of storage being in any way resourceconstrained, but it is true. Developing for iOS is not, perhaps, in exactly the same league as trying to write a complex spreadsheet application on a machine with 48KB of memory. But given the graphical nature of iOS and all it is capable of doing, running out of memory is very easy.

The iOS devices available right now have either 256MB or 512MB of physical RAM, though that will likely increase over time. Some of that memory is used for the screen buffer and by other system processes. Usually, no more than half of that memory is left for your application to use, and the amount can be considerably less, especially now that apps can run in the background.

Although that may sound like it leaves a pretty decent amount of memory for such a small computer, there is another factor to consider when it comes to memory on iOS. Modern computer operating systems like Mac OS X will take chunks of memory that aren’t being used and write them out to disk in something called a swap file. The swap file allows applications to keep running, even when they have requested more memory than is actually available on the computer. iOS, however, will not write volatile memory, such as application data, out to a swap file. As a result, the amount of memory available to your application is constrained by the amount of unused physical memory in the iOS device.

www.it-ebooks.info

8CHAPTER 1: Welcome to the Jungle

Cocoa Touch has built-in mechanisms for letting your application know that memory is getting low. When that happens, your application must free up unneeded memory or risk being forced to quit.

No Garbage Collection, but…

We mentioned earlier that Cocoa Touch uses Objective-C, but one of the key new features of that language is not available with iOS: Cocoa Touch does not support garbage collection. The need to do manual memory management when programming for iOS has been a bit of a stumbling block for many programmers new to the platform, especially those coming from languages that offer garbage collection.

With the version of Objective-C supported by iOS 5, however, this particular stumbling block is basically gone. iOS 5 introduces a feature called Automatic Reference Counting (ARC), which gets rid of the need to manually manage memory for Objective-C objects. We’ll talk about ARC in Chapter 3.

Some New Stuff

Since we’ve mentioned that Cocoa Touch is missing some features that Cocoa has, it seems only fair to mention that the iOS SDK contains some functionality that is not currently present in Cocoa or, at least, is not available on every Mac:

The iOS SDK provides a way for your application to determine the iOS device’s current geographic coordinates using Core Location.

Most iOS devices have built-in cameras and photo libraries, and the SDK provides mechanisms that allow your application to access both.

iOS devices have a built-in accelerometer (and, in the latest iPhone and iPod touch, a gyroscope) that lets you detect how your device is being held and moved.

A Different Approach

Two things iOS devices don’t have are a physical keyboard and a mouse, which means you have a fundamentally different way of interacting with the user than you do when programming for a general-purpose computer. Fortunately, most of that interaction is handled for you. For example, if you add a text field to your application, iOS knows to bring up a keyboard when the user clicks in that field, without you needing to write any extra code.

NOTE: Current devices do allow you to connect an external keyboard via Bluetooth, which gives

you a nice keyboard experience and saves some screen real estate, but this is still a fairly rare

usage. Connecting a mouse is still not an option.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]