4
Delegation and Core Location

In this chapter, we will introduce delegation, a recurring design pattern of Cocoa Touch development and the Core Location framework, which provides the location-finding features of iOS. In addition, we will also take a quick look at how to use the debugger that Xcode provides to find and fix problems in your code.

To learn about delegation, you’re going to write an application called Whereami that uses delegation over and over again. This application will display an interactive map and allow the user to tag the current location with a pin and a title. This exercise spans two chapters. At the end of this chapter, the application won’t look like much, but the final product – and the clearer understanding of delegation – will be worth it.

From the File menu, select New and then New Project.... On the next window, select Application from the iOS section and create a Window-based Application. Name the project Whereami and select iPhone as the device family.

Projects, targets, and frameworks

Let’s look more closely at the what this new project actually is. A project is a file that contains a list of references to other files (source code, resources, frameworks, and libraries) as well as a number of settings that lay out the rules for items within the project. Projects end in .xcodeproj, as in Whereami.xcodeproj.

A project always has at least one target. A target uses the files in the project to build a particular product. When you build and run, you build and run the target, not the project.

The product the target builds is typically an application, although it can be a compiled library or a unit test bundle. When you create a new project and choose a template, Xcode automatically creates a target for you. When you created the Whereami project, you selected an iOS application, so Xcode created an iOS application target and named it Whereami.

In the project navigator, select the Whereami project (the item at the very top). Notice that the Whereami project and the Whereami target are listed in the editor area. Select the Whereami target to see the details and settings that define this target. (We won’t discuss all of these now, but we’ll come back to different ones as we need them.) From the choices at the top of the editor area, select Build Phases (Figure 4.1). The target’s build phases are a series of steps, and these steps lead, in this case, to an iOS application.

Figure 4.1  Build phases of the Whereami target

Build phases of the Whereami target

The essential build phases for creating an iOS application are Compile Sources, Link Binary With Libraries, and Copy Bundle Resources. We’ll look at these phases in detail at the end of the chapter. For now, let’s focus on Link Binary With Libraries and frameworks.

A framework is a collection of related classes that you can add to a target. Cocoa Touch is a collection of frameworks. One of the benefits of Cocoa Touch being organized into frameworks is that you only have to add the frameworks that a target needs.

To see what frameworks are already linked to your target, click the disclosure button next to Link Binary With Libraries. Right now, there are three. The UIKit framework contains classes that make up the iOS user interface, the Foundation framework includes classes like NSString and NSArray, and Core Graphics enables the graphics library that we will dig into starting in Chapter 6.

Whereami also needs the Core Location framework, which includes the classes related to finding a device’s location. To add this framework to your target, click the plus (+) button in the bottom left corner of the Link Binary With Libraries section. A sheet that displays the available frameworks will appear (Figure 4.2). Select CoreLocation.framework from this list and click Add.

Figure 4.2  Adding the Core Location framework

Adding the Core Location framework

CoreLocation.framework will now appear in the Link Binary With Libraries phase and in the project navigator. In the project navigator, you can move the framework to the Frameworks group to keep your project tidy, but you don’t have to.

Make sure you remember how to add a framework to a project – you will have to do it fairly frequently!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.50