• No results found

Creating Separate Xcode Projects for iOS and OS X

In document Learning iCloud Data Management.pdf (Page 70-79)

The first step is to create a project and run it. For projects that will run on both iOS and OS X, it is common (but not required) to create a single Xcode project with two targets. For a project such as this first project, though, you will be letting iCloud do its work behind the scenes, so you do not need common code that will run on both operating systems. The code will be comparable, but you do not need to create a single project with two targets. Because this structure is a little simpler than the multitarget structure, these first apps will have two separate Xcode projects: one for iOS and one for OS X. (You’ll see how to build a single project with both an iOS and OS X app in Chapter 11, “Using Xcode Workspaces for Shared Development.”)

Note that this is one of the places where recent features of Xcode are demonstrated.

Previously, Xcode templates that used two targets used scenarios such as one target for OS X and another for Spotlight support. Now, the two targets, at least for iCloud proj-ects, can be iOS and OS X. That is a long-awaited goal that is now achieved.

In both the iOS and OS X Xcode projects, you will produce a (minimally) work-ing version of the app uswork-ing the graphical user interface of Xcode. The only typwork-ing involved in the creation of each project is typing in the name of the app (and, if you haven’t done so before, the name of your company). There is no code to type in for these projects, but rest assured there will be plenty to type in throughout the rest of the book.

ptg12441863 Creating the iOS Xcode Project

As a general rule, the templates built into Xcode are the most up-to-date code you can find. If you are working from a previously downloaded sample, check to see if a new version has been created on developer.apple.com and also check the date of the most recent version. With the addition of the refactor commands in the Edit menu, it seems that the sample code can be updated more easily than before. If you’re work-ing from your own older code, seriously consider at least refactorwork-ing it to use modern Objective-C syntax and ARC.

Bearing this in mind, the best way to start your new iOS app is to create a new iOS project using the Single View Application, as shown in Figure 3.3.

Figure 3.3 Using the Single View Application template

ptg12441863 Introducing the Built-In Data Apps 43

Figure 3.4 Selecting options

Select the options, as shown in Figure 3.4.

ptg12441863

Figure 3.5 Choosing Universal for the Devices setting

When requested, save the project. When the workspace window opens, as shown in Figure 3.5, choose Universal for Devices. The other settings are fine to leave as-is.

Choose the iPhone simulator for your scheme (you can use any iPhone or iPad), and then click the Run button at the upper left of the workspace window to run your new project on the simulator. You should see no error messages, and, in fact, nothing except the blank simulator. Any other result indicates that there’s something wrong either with your Xcode installation or with some configuration within it. The Apple Developer Forums can be helpful here if you run into problems.

Now that you’ve verified that the template runs for you, the first steps you add to this project will let you enter some text into a text view. To make that happen, open the iPhone storyboard, and then drag a text view from the objects library into the story board, as shown in Figure 3.6.

ptg12441863 Introducing the Built-In Data Apps 45

Run the app now, and you’ll see your text view with placeholder text. As you see in Figure 3.7, you’ll be able to edit the text in the simulator. (Also note that Retina dis-plays use pixel doubling; on the simulator, this may result in a double-size image.)

Figure 3.7 The text field is ready for use in the simulator.

Figure 3.6 Adding a text view

ptg12441863 Now you have an app that you can run; it contains a text field into which you can

type text. You’ll want to do something with that text, so, as shown in Figure 3.8, add a button above the text field (you may have to do some rearranging). The reason you may need to add the button above the text field is that on the iPhone, the keyboard may pop up over the button as soon as you tap in the text view if the button is at the bottom of the screen.

Note

You will notice in this chapter that the Xcode workspace window is changed from one figure to the next. That reflects the way many people work: the navigation area is sometimes critical, but at other times you can hide it and use the space for the code editor. Similarly, you may want to show or hide the utility area as you work.

Also, note that in many cases you see the iPhone storyboard in figures in this book. The reason for that is simple: the screen size is the smallest of the iOS devices, and so those storyboards reproduce best on the limited size of a printed page.

Figure 3.8 Adding a button

ptg12441863 Introducing the Built-In Data Apps 47

Creating the OS X Xcode Project

Now, you can turn your attention to creating a comparable OS X app. As is the case with the iOS app, you won’t be typing any code at this stage for an OS X app, but, as you’ll see, when you get into the functionality of the app, there are major similarities.

Begin by creating an OS X project in Xcode. In the OS X section of the New Prod-uct sheet, choose Application and Cocoa Application. (You do not have the variety of templates that you have with iOS.) Name the project PartII OS X, as shown in Figure 3.9. Do not choose any of the other options.

Figure 3.9 Using the Cocoa application template

As you did with the iOS project, start by attempting to run it. Again, you should see no errors or warnings, and not much of anything except the app’s empty window.

Because there are no storyboards in OS X projects, you use nib and xib files to build your interface graphically.

ptg12441863 Note

nib files got their name when OS X was first developed at NeXT. nib is an acronym for NeXT Interface Builder. In a major restructuring of Interface Builder, nib files were implemented in a different format that relies on XML, and the file extension became .xib. Many people pronounce xib as “nib” for reasons of history as well as the difficulty of figuring out what the correct pronunciation would be.

On both operating systems, you can also create the interfaces programmatically, although the classes you use in your code are not identical. Open the MainMenu.xib file to begin editing the interface, as shown in Figure 3.10. The project template pro-vides you with a window and a view.

Figure 3.10 Opening the nib file

Explore the nib file. Just as you did for iOS, add a text view to the window’s view.

(The objects in the OS X library are not always identical to those in the iOS library, but they are mostly similar.) Figure 3.11 shows the field added to the view.

ptg12441863 Introducing the Built-In Data Apps 49

Resize the text view and add a button, as you did for iOS. You should see the results shown in Figure 3.12.

Figure 3.11 Adding a text view

ptg12441863 Now run the app. Figure 3.13 should match your results. (The app’s menu bar is not

shown in Figure 3.12, but it’s there at the top of the screen.)

Figure 3.13 Running the app

In document Learning iCloud Data Management.pdf (Page 70-79)

Related documents