MOSXApp Programming Guide
MOSXApp Programming Guide
General
2011-06-27
Apple Inc. 2011 Apple Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, mechanical, electronic, photocopying, recording, or otherwise, without prior written permission of Apple Inc., with the following exceptions: Any person is hereby authorized to store documentation on a single computer for personal use only and to print copies of documentation for personal use provided that the documentation contains Apples copyright notice. The Apple logo is a trademark of Apple Inc. No licenses, express or implied, are granted with respect to any of the technology described in this document. Apple retains all intellectual property rights associated with the technology described in this document. This document is intended to assist application developers to develop applications only for Apple-labeled computers. Apple Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 .Mac is a registered service mark of Apple Inc. App Store is a service mark of Apple Inc. Apple, the Apple logo, Bonjour, Cocoa, eMac, Finder, Instruments, iPhoto, iTunes, Keychain, Mac, Mac OS, Macintosh, Objective-C, QuickDraw, Sand, Spotlight, Time Machine, and Xcode are trademarks of Apple Inc., registered in the United States and other countries. IOS is a trademark or registered trademark of Cisco in the U.S. and other countries and is used under license. OpenGL is a registered trademark of Silicon Graphics, Inc. UNIX is a registered trademark of The Open Group
Even though Apple has reviewed this document, APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS DOCUMENT, ITS QUALITY, ACCURACY, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS DOCUMENT IS PROVIDED AS IS, AND YOU, THE READER, ARE ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND ACCURACY. IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES RESULTING FROM ANY DEFECT OR INACCURACY IN THIS DOCUMENT, even if advised of the possibility of such damages. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer, agent, or employee is authorized to make any modification, extension, or addition to this warranty. Some states do not allow the exclusion or limitation of implied warranties or liability for incidental or consequential damages, so the above limitation or exclusion may not apply to you. This warranty gives you specific legal rights, and you may also have other rights which vary from state to state.
Contents
Introduction
Chapter 1
Chapter 2
3
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
CONTENTS
Xcode Templates Provide the Main Menu 46 Connect Menu Items to Your Code or Your First Responder 47 Chapter 3
Chapter 4
Chapter 5
4
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
CONTENTS
Chapter 6
Chapter 7
5
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
CONTENTS
6
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
Chapter 2
Chapter 3
Chapter 4
Chapter 5
7
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
Chapter 6
Chapter 7
8
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
INTRODUCTION
This document is the starting point for learning how to create Mac OS X applications. It contains fundamental information about the Mac OS X environment and how your applications interact with that environment. It also contains important information about the architecture of Mac OS X applications and tips for designing key parts of your application.
At a Glance
Cocoa is the application environment that unlocks the full power of Mac OS X. Cocoa provides APIs, libraries, and runtimes that help you create fast, exciting applications that automatically inherit the beautiful look and feel of Mac OS X, as well as standard behaviors users expect.
At a Glance
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
INTRODUCTION
Relevant Chapters: The Application Environment (page 13) and The Core Application Design (page 23)
10
At a Glance
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
INTRODUCTION
See Also
The following documents provide additional information about designing Mac OS X applications, as well as more details about topics covered in this document:
To work through a tutorial showing you how to create a Cocoa application, see Your First Mac Application. For information about user interface design enabling you to create effective applications using Mac OS X, see Apple Human Interface Guidelines. For information about the design patterns used in Cocoa, see Cocoa Fundamentals Guide. For a general survey of Mac OS X technologies, see Mac OS X Technology Overview.
11
INTRODUCTION
12
See Also
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
CHAPTER 1
Like all modern operating systems, Mac OS X incorporates the latest technologies for creating powerful and fun-to-use applications. But the technologies by themselves are not enough to make every application great. What sets an application apart from its peers is how it helps the user achieve some tangible goal. After all, users are not going to care what technologies an application uses, as long as it helps them do what they need to do. An application that gets in the users way is going to be forgotten, but one that makes work (or play) easier and more fun is going to be remembered. Cocoa is the application environment that you use to write applications for Mac OS X. In its runtime aspect, Cocoa presents the applications user interface and integrates it tightly with the other components of the operating system. Cocoa also has a development aspect, as described in The Core Application Design (page 23). This chapter describes some of the key parts of the Mac OS X runtime environment that help you create great applications. The items described here are not a complete list but are simply a collection of some of the most commonly used techniques and technologies available for your applications. In particular, this chapter describes some of the important ease-of-use technologies introduced in Mac OS X v10.7. For a more thorough list of technologies available in Mac OS X, see Mac OS X Technology Overview.
Users should not have to save their work manually. The document model in Cocoa provides support for saving the users file-based documents without user interaction; see Documents Are Automatically Saved (page 63). Applications should restore the users work environment at login time. Cocoa provides support for archiving the current state of the applications interface (including the state of unsaved documents) and restoring that state at launch time; see User Interface Preservation (page 37). Applications should support automatic termination so that the user never has to quit them. Automatic termination means that when the user closes an applications windows, the application appears to quit but actually just moves to the background quietly. The advantage is that subsequent launches are nearly instant as the application simply moves back to the foreground; see Automatic and Sudden Termination of Applications Improves the User Experience (page 34)
13
CHAPTER 1
You should consider providing your users with an immersive, full-screen experience by implementing a full-screen version of your user interface. The full-screen experience eliminates outside distractions and allows the user to focus on their content; see Implementing the Full-Screen Experience (page 67). Support trackpad gestures for appropriate actions in your application. Gestures provide simple shortcuts for common tasks and can be used to supplement existing controls and menu commands. Mac OS X provides automatic support for reporting gestures to your application through the normal event-handling mechanism; see Cocoa Event-Handling Guide. Consider minimizing or eliminating the users interactions with the raw file system. Rather than expose the entire file system to the user through the open and save panels, some applications, in the manner of iPhoto and iTunes, can provide a better user experience by presenting the users content in a simplified browser designed specifically for the applications content. Mac OS X uses a well-defined file system structure that allows you to place and find files easily and includes many technologies for accessing those files; see The File System (page 16). For applications that support custom document types, provide a Quick Look plug-in so that users can view your documents from outside of your application; see Quick Look Programming Guide.
All of the preceding features are supported by Cocoa and can be incorporated with relatively little effort.
Use animations to provide feedback and convey changes. Cocoa provides mechanisms for creating sophisticated animations quickly in both the AppKit and Core Animation frameworks. For information about creating view-based animations, see Cocoa Drawing Guide. For information about using Core Animation to create your animations, see Core Animation Programming Guide. Include high-resolution versions of your art and graphics. Mac OS X automatically loads high-resolution image resources when an application runs on a screen whose scaling factor is greater than 1.0. Including such image resources makes your applications graphics look even sharper and crisper on those higher-resolution screens.
For information about the graphics technologies available in Mac OS X, see Graphics and Multimedia Technologies in Mac OS X Technology Overview.
14
CHAPTER 1
Based on UNIX
Mac OS X is powered by a 64-bit Mach kernel, which manages processor resources, memory, and other low-level behaviors. On top of the kernel sits a modified version of the Berkeley Software Distribution (BSD) operating system, which provides interfaces that applications can use to interact with the lower-level system. This combination of Mach and BSD provides the following system-level support for your applications:
Preemptive multitaskingAll processes share the CPU efficiently. The kernel schedules processes in a way that ensures they all receive the time they need to run. Even background applications continue to receive CPU time to execute ongoing tasks. Protected memoryEach process runs in its own protected memory space, which prevents processes from accidentally interfering with each other. (Applications can share part of their memory space to implement fast interprocess communication but take responsibility for synchronizing and locking that memory appropriately.) Virtual memory64-bit applications have a virtual address space of approximately 18 exabytes (18 billion billion bytes). (If you create a 32-bit application, the amount of virtual memory is only 4 GB.) When an applications memory usage exceeds the amount of free physical memory, the system transparently writes pages to disk to make more room. Written out pages remain on disk until they are needed in memory again or the application exits. Networking and BonjourMac OS X provides support for the standard networking protocols and services in use today. BSD sockets provide the low-level communication mechanism for applications, but higher-level interfaces also exist. Bonjour simplifies the user networking experience by providing a dynamic way to advertise and connect to network services over TCP/IP.
For detailed information about the underlying environment of Mac OS X, see Kernel and Device Drivers Layer in Mac OS X Technology Overview.
15
CHAPTER 1
For applications running outside of a sandbox (including those running in versions of Mac OS X before 10.7), the home directory is the user-specific subdirectory of /Users that contains the users files.
To retrieve the path to the home directory, use the NSHomeDirectory function.
16
CHAPTER 1
Directory
Description
Library directory The Library directory is the top-level directory for storing private application-related data and preferences. There are several Library directories scattered throughout the system but you should always use the one located inside the current home directory. Do not store files directly at the top-level of the Library directory. Instead, store them in one of the specific subdirectories described in this table. In Mac OS X 10.7 and later, the Finder hides the Library directory in the users home folder by default. Therefore, you should never store files in this directory that you want the user to access. To get the path to this directory use the NSLibraryDirectory search path key with the NSUserDomainMask domain. Application Support directory The Application Support directory is where your application stores any type of file that supports the application but is not required for the application to run, such as document templates or configuration files. The files should be application-specific but should never store user data. This directory is located inside the Library directory. Never store files at the top level of this directory: Always put them in a subdirectory named for your application or company. If the resources apply to all users on the system, such as document templates, place them in /Library/Application Support. To get the path to this directory use the NSApplicationSupportDirectory search path key with the NSLocalDomainMask domain. If the resources are user-specific, such as workspace configuration files, place them in the current users ~/Library/Application Support directory. To get the path to this directory use the NSApplicationSupportDirectory search path key with the NSUserDomainMask domain. Caches directory The Caches directory is where you store cache files and other temporary data that your application can re-create as needed. This directory is located inside the Library directory. Never store files at the top level of this directory: Always put them in a subdirectory named for your application or company. Your application is responsible for cleaning out cache data files when they are no longer needed. The system does not delete files from this directory. To get the path to this directory use the NSCachesDirectory search path key with the NSUserDomainMask domain. Movies directory The Movies directory contains the users video files. To get the path to this directory use the NSMoviesDirectory search path key with the NSUserDomainMask domain. Music directory The Music directory contains the users music and audio files. To get the path to this directory use the NSMusicDirectory search path key with the NSUserDomainMask domain. Pictures directory The Pictures directory contains the users images and photos. To get the path to this directory use the NSPicturesDirectory search path key with the NSUserDomainMask domain.
17
CHAPTER 1
Description The Temporary directory is where you store files that do not need to persist between launches of your application. You normally use this directory for scratch files or other types of short-lived data files that are not related to your applications persistent data. This directory is typically hidden from the user. Your application should remove files from this directory as soon as it is done with them. The system may also purge lingering files from this directory at system startup. To get the path to this directory use the NSTemporaryDirectory function.
Listing 1-1 shows an example of how to retrieve the base path to the Application Support directory and then append a custom application directory to it. Listing 1-1 Getting the path to the Application Support directory
NSFileManager* fileManager = [NSFileManager defaultManager]; NSURL* appSupportDir = nil; NSArray *urls = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]; if ([paths count] > 0) { appSupportDir = [[urls objectAtIndex:0] URLByAppendingPathComponent:@"com.example.MyApp"]; }
For more information about how to access files in well known system directories, see Low-Level File Management Programming Topics.
18
CHAPTER 1
Attributes for the Mac OS X file system Description The HFS+ file system is case-insensitive but also case-preserving. Therefore, when specifying filenames and directories in your code, it is best to assume case-sensitivity. Construct paths using the methods of the NSURL and NSString classes. The NSURL class is preferred for path construction because of its ability to specify not only paths in the local file system but paths to network resources. Many file-related attributes can be retrieved using the getResourceValue: forKey:error: method of the NSURL class. You can also use an NSFileManager object to retrieve many file-related attributes. File permissions are managed using access control lists (ACLs) and BSD permissions. The system uses ACLs whenever possible to specify precise permissions for files and directories, but it falls back to using BSD permissions when no ACLs are specified. By default, any files your application creates are owned by the current user and given appropriate permissions. Thus, your application should always be able to read and write files it creates explicitly. In addition, the applications sandbox may allow it to access other files in specific situations. For more information about the sandbox, see The App Sandbox (page 19).
Case sensitivity
Path construction
File attributes
File permissions
Applications that cannot use the File Coordination interfaces (see Coordinating File Access with Other Processes (page 18)) to track changes to files and directories can use the FSEvents API instead. This API provides a lower-level interface for tracking file system interactions and is available in Mac OS X v10.5 and later. For information on how to use the FSEvents API, see File System Events Programming Guide.
Security
The security technologies in Mac OS X help you secure sensitive data created or managed by your application and also help minimize the damage caused by successful exploits.
The Keychain
A keychain is a secure, encrypted container for storing the users passwords and other secrets. Users often have to manage multiple accounts that require logins with user IDs and passwords. For more on the keychain, see Keychain Services Concepts in Keychain Services Programming Guide.
19
CHAPTER 1
An app sandbox prevents the code involved in those breaches from taking advantage of your applications status as a user process to gain access to other parts of the system or to other unrelated (but potentially enticing) user data. It prevents these exploits by limiting access to unrelated files and by limiting access to system resources (such as the network or specific hardware) that your application does not use. An app sandbox comprises two key components:
Entitlements for declaring the resources that the application uses A container directory for storing the applications files
You must specifically request that your application be put into a sandbox by signing it with at least the minimal set of entitlements. When the sandboxing entitlements are present, the system automatically creates a container directory for the application and runs it inside of an appropriately configured sandbox. Entitlements Declare the System Resources Your Application Needs Entitlements let the system know both that you want your application placed in a sandbox and that your application needs access to specific resources while in that sandbox. You specify the entitlements for your application using an entitlements property-list file that you create in Xcode and keep with your project. Before distributing your application (or submitting it to the App Store), you sign your application with this entitlements file. The codesign command-line tool embeds your entitlements into the applications signature. When the user subsequently launches your application, the system retrieves the entitlements from the signature and uses them to configure your applications sandbox. To request that your application be placed in a sandbox, you must include the following key in your entitlements property-list file at a minimum:
com.apple.security.app-sandbox
In addition to including the preceding key, your entitlements property-list file should include the other keys that reflect which resources your application uses. An application can request access to the Open and Save panels, access to the network, access to a camera (when present), and access to the microphone. You must request the items that your application uses but should not request access to items your application does not use. If you attempt to use a feature for which you do not have an appropriate entitlement, the system denies access and logs an appropriate message to the console. You can use these console messages during debugging to help identify features your application uses. For a complete list of entitlement keys and information on how to create an entitlements property-list file and apply it to your application, see Code Signing And Application Sandboxing Guide. The Container Directory Is Your Applications Home Directory A container directory is where a sandboxed application stores all of its private files and data. System frameworks use this directory when writing out application-specific data such as preferences and autosave information. Your application similarly uses this directory to store cache files, temporary data files, or any files that the user would not normally see. The only thing you do not store in the container directory are files that the user explicitly saves using the system Save panel.
20
CHAPTER 1
Applications in a sandbox are specifically prohibited from accessing files outside the container directory with some exceptions. An application may work with files opened using the system Open panel or saved using the system Save panel. In such a situation, the app sandbox is modified to allow access to those specific files. Similarly, the use of established system interfaces may also give your application indirect access to data in other parts of the file system. For example, the Address Book framework gives you access to the users contact information, which is stored outside of your application sandbox. The first time your application is launched, the system creates the container directory automatically in the users ~/Library/Containers directory and uses your applications bundle identifier for the directory name. For example, the container directory for the TextEdit application is ~/Library/Containers/com.apple.TextEdit. Note: Although the directory name is derived from the bundle identifier of the application, ownership of the directory is linked to your developer identity for security. Using the developer identity means that malicious applications cannot try to spoof the system into granting access to your container using your bundle identifier. The structure of the sandbox directory loosely parallels the structure of the users home directory. To retrieve the path to directories in your sandbox, you must always use system-provided methods such as the URLsForDirectory:inDomains: method of NSFileManager. Never try to build paths to these directories manually. Doing so will cause your application to break if the location of the paths changes later. For more information about the directories you should be using in your applications, see A Few Important Application Directories (page 16).
21
CHAPTER 1
22
CHAPTER 2
To connect with the power of Mac OS X, you develop applications using the Cocoa application environment. Cocoa has a runtime aspect and a development aspect. In its runtime aspect, Cocoa presents the applications user interface and integrates it tightly with the other components of the operating system. In its development aspect, Cocoa for Mac OS X provides an integrated suite of object-oriented software components packaged in two core class libraries, the AppKit and Foundation frameworks, and a number of underlying frameworks providing supporting technologies. Cocoa classes are reusable and extensibleyou can use them as is or extend them for your particular requirements. Cocoa provides program infrastructure to make the creation of applications easy. The extent of this infrastructure is such that you can create a new Cocoa application project in Xcode and without adding any code have a functional application. Such an application is able to display its window (or create new documents) and implements many standard system behaviors. And although the Xcode templates provide some code to make this all happen, the amount of code they provide is minimal. Most of the actual runtime behavior is provided by Cocoa itself. To make your application fully functional, you need to understand the Cocoa infrastructure and know what it expects you to provide. There are many ways to define a Cocoa application, so it is best to understand these basics before you start your implementation. Otherwise, fixing your design later could be much harder.
Design pattern
Model-View-Controller Use of the Model-View-Controller (MVC) design pattern ensures that the objects you create now can be reused or updated easily in future versions of your application. Cocoa framework provides most of the classes used to build your applications controller and view layers. It is your job to customize the classes you need and provide the necessary data model objects to go with them. Delegation The delegation design pattern allows you to change the runtime behavior of an object without subclassing. Delegate objects conform to a specific protocol that defines the interaction points between the delegate and the object it modifies. At specific points, the master object calls the methods of its delegate to provide it with information or ask what to do. The delegate can then take whatever actions are appropriate.
23
CHAPTER 2
Why it is important The responder chain defines the relationships between event-handling objects in your application. As events arrive, the application dispatches them to the first responder object for handling. If that object does not want the event, it passes it to the next responder, which can either handle the event or send it to its next responder, and so on up the chain. Windows and views are the most common types of responder objects and are always the first responders for mouse events. Other types of objects, such as your applications controller objects, may also be responders.
Target-action
Controls use the target-action design pattern to notify your application of user interactions. When the user interacts with a control in a predefined way (such as by touching a button), the control sends a message (the action) to an object you specify (the target). Upon receiving the action message, the target object can then respond in an appropriate manner. The garbage-collected memory model eliminates the need to retain and release objects and memory explicitly. For more information about the garbage collection system, see Garbage Collection Programming Guide.
Garbage-collected memory
Block objects
Block objects are a convenient way to encapsulate code and local stack variables in a form that can be executed later. Blocks are used in lieu of callback functions by many frameworks and are also used in conjunction with Grand Central Dispatch to perform tasks asynchronously. For more information about using blocks, see Blocks Programming Topics.
Notifications
Notifications are used throughout Cocoa to deliver news of changes to your application. Many objects send notifications at key moments in the objects life cycle. Intercepting these notifications gives you a chance to respond and add custom behavior. KVO tracks changes to a specific property of an object. When that property changes, the change generates automatic notifications for any objects that registered an interest in that property. Those observers then have a chance to respond to the change. Cocoa bindings provide a convenient bridge between the model, view, and controller portions of your application. You bind a view to some underlying data object (which can be static or dynamic) through one of your controllers. Changes to the view are then automatically reflected in the data object, and vice versa. The use of bindings is not required for applications but does minimize the amount of code you have to write. You can set up bindings programmatically or using Interface Builder.
Bindings
For a more detailed discussion of Cocoa and the design patterns you use to implement Mac OS X applications, see Cocoa Fundamentals Guide.
24
CHAPTER 2
You should choose a basic application style early in your design process because that choice affects everything you do later. The single-window styles are preferred in many cases, especially for developers bringing applications over from iOS. The single-window style typically yields a more streamlined user experience, and it also makes it easier for your application to support a full-screen mode. However, if your application works extensively with complex documents, the multiwindow style may be preferable because it provides more document-related infrastructure to help you implement your application. The Calculator application provided with Mac OS X, shown in Figure 2-1, is an example of a single-window utility application. Utility applications typically handle ephemeral data or manage system processes. Calculator does not create or deal with any documents or persistent user data but simply processes numerical data entered by the user into the text field in its single window, displaying the results of its calculations in the same field. When the user quits the application, the data it processed is simply discarded. Figure 2-1 The Calculator single-window utility application
Single-window shoebox applications do handle persistent user data. One of the most prominent examples of a shoebox application is iPhoto, shown in Figure 2-2. The user data handled by iPhoto are photos (and associated metadata), which the application edits, displays, and stores. All user interaction with iPhoto happens in a single window. Although iPhoto stores its data in files, it doesnt present the files to the user. The application presents a simplified interface so that users dont need to manage files in order to use the application. Instead, they work directly with their photos. Moreover, iPhoto hides its files from regular
25
CHAPTER 2
manipulation in the Finder by placing them within a single package. In addition, the application saves the users editing changes to disk at appropriate times. So, users are relieved of the need to manually save, open, or close documents. This simplicity for users is one of the key advantages of the shoebox application design. Figure 2-2 The iPhoto single-window application
A good example of a multiwindow document application is TextEdit, which creates, displays, and edits documents containing plain or styled text and images. TextEdit does not organize or manage its documentsusers do that with the Finder. Each TextEdit document opens in its own window, multiple documents can be open at one time, and the user interacts with the frontmost document using controls in the windows toolbar and the applications menu bar. Figure 2-3 shows a document created by TextEdit.
26
CHAPTER 2
Figure 2-3
In Mac OS X v10.7, TextEdit implements automatic saving, an idea borrowed from the single-window design. Users no longer need to save their editing changesthe document architecture writes them to disk at appropriate times. Although users can manually save a version of the document if they wish, doing so is optional. Implementing a Document-Based Design (page 49) explains how to create a multiwindow, document-based application.
27
CHAPTER 2
Figure 2-4
View
Event Loop
Window Controller
NSWindow
Table 2-2 (page 28) describes the roles played by the objects in the diagram. Table 2-2 Object The core objects used by all Cocoa applications Description
NSApplication (Required) All applications have a single application object to run the event loop and
object
manage interactions between your application and the system. You typically use the NSApplication class as is, putting any custom application-object-related code in your application delegate object. (Expected) The application delegate object is a custom object that you provide. It works closely with the NSApplication object to run the application and manage the transitions between different application states. Your application delegate object must conform to the NSApplicationDelegate Protocol.
Data model objects store your applications content and are therefore specific to your application. For example, a banking application might store a database containing financial transactions, whereas a painting application might store an image object or even the sequence of drawing commands that led to the creation of that image. (In the latter case, an image object is still a data object because it is just a container for the image data. The actual rendering of that image still takes place elsewhere in your application.)
28
CHAPTER 2
Description The NSWindowController class defines the base implementation for all window controller objects. Each instance of this class is responsible for loading and managing a single window and coordinating with the system to handle standard window behaviors. In your applications, you subclass NSWindowController to manage both the window and its contents. Each window controller object is ultimately responsible for everything that happens in its window. If the contents of your window are simple, the window controller object may do all of the management itself. If your window is more complex, the window controller might use one or more view controllers to manage portions of the window.
Window objects
You use instances of NSWindow as is to represent your onscreen windows. Window objects can be configured in different styles depending on your applications needs. For example, most windows have title bars and borders but you can also configure windows without those visual adornments. Although not strictly required, a window object is almost always managed by a corresponding window controller.
View controllers
The NSViewController class defines the base implementation for all view controller objects. This class coordinates the loading of a single view hierarchy into your application. You can use view controllers to divide up the work for managing more sophisticated window layouts. In such a scenario, your view controllers would work together (and with the overarching window controller) to present the overall window contents. If you have developed iOS applications, be aware that AppKit view controllers play a less prominent role than UIKit view controllers. In Mac OS X, AppKit view controllers are assistants to the window controller, which is ultimately responsible for everything that goes in the window. The main job of an AppKit view controller is to load its view hierarchy. Everything else is custom code that you write.
View objects
The NSView class defines the basic behavior for all views. A view defines a rectangular region in a window, draws the contents of that region, and handles events in that region. Visually, views can be layered on top of each other to create view hierarchies, whereby one view obscures a portion of the underlying view. Cocoa provides several classes that represent the standard system controls. These classes provide standard visual items such as buttons, text fields, and tables that you can use to build your user interface. Although a few views are used as is to present visual adornments, most work with your code to manage user interactions with your applications content.
Any application can also have secondary windows, also known as dialogs and panels. These windows are subordinate to the current document window or, in the case of single-window applications, to the main window. They support the document or main window in various waysfor example, allowing selection of fonts and color, allowing the selection of tools from a palette, or displaying a warning A secondary window is often modal.
29
CHAPTER 2
Event Loop
NSDocumentController
NSDocument
View controllers
(optional)
Window controller
View
NSWindow
Table 2-3 (page 31) describes the role of the insertedNSDocumentController and NSDocument objects. (For information about the roles of the other objects in the diagram, see Table 2-2 (page 28).)
30
CHAPTER 2
Document Controller The NSDocumentController class defines a high-level controller for creating and object managing all document objects. In addition to managing documents, the document controller also manages many document-related menu items, such as the Open Recent menu and the open and save panels. Document object The NSDocument class is the base class for implementing documents in a multiwindow application. This class acts as the controller for the data objects associated with the document. You define your own custom subclasses to manage the interactions with your applications data objects and to work with one or more NSWindowController objects to display the document contents on the screen.
For more detailed information about multiwindow, document-based applications, see Implementing a Document-Based Design (page 49).
31
CHAPTER 2
The NSApplicationMain function initializes your application and prepares it to run. As part of the initialization process, this function does several things:
The NSApplicationMain function creates an instance of the NSApplication class. You can access this object from anywhere in your application using the sharedApplication class method. It configures the memory management objects needed for the main thread.
For garbage-collected applications, it installs a collector on the applications main thread. For memory-managed applications, it installs a last-chance autorelease pool to catch autoreleased objects.
It loads the nib file specified by the NSMainNibFile key in the Info.plist file and instantiates all of the objects in that file. This is your applications main nib file and should contain your application delegate and any other critical objects that must be loaded early in the launch cycle. Any objects that are not needed at launch time should be placed in separate nib files and loaded later. It calls the run method of your application object to finish the launch cycle and begin processing events.
By the time the run method is called, the main objects of your application are loaded into memory but the application is still not fully launched. The run method notifies the application delegate that the application is about to launch, shows the application menu bar, opens any any files that were passed to the application, does some framework housekeeping, and starts the event processing loop. All of this work occurs on the applications main thread with one exception. Files may be opened in secondary threads if the canConcurrentlyReadDocumentsOfType: class method of the corresponding NSDocument object returns YES.
32
CHAPTER 2
Note: Although you can subclass NSApplication to customize the application behaviors, you should avoid doing so whenever possible and use the default NSApplication class to run your application. If you do decide to subclass, you must let Cocoa know that you have done so by setting the value of the NSPrincipalClass key to your custom class name in your applications Info.plist file. The NSApplicationMain function uses the value of that key when creating the application object. For more about your applications Info.plist file, see The Information Property List File (page 80). If your application preserves its user interface between launch cycles, Cocoa loads any preserved data at launch time and uses it to re-create the windows that were open the last time your application was running. For more information about how to preserve your applications user interface, see User Interface Preservation (page 37).
event
Window Server
event
The run method of the NSApplication object is the workhorse of the main event loop. In a closed loop, this method executes the following steps until the application terminates: 1. The run method services window-update notifications, which results in the redrawing of any windows that are marked as dirty. It dequeues an event from its internal event queue using the
nextEventMatchingMask:untilDate:inMode:dequeue: method and converts the event data into an NSEvent object.
2.
3.
It dispatches the event to the appropriate target object using the sendEvent: method of NSApplication.
33
CHAPTER 2
The nextEventMatchingMask:untilDate:inMode:dequeue: method is the funnel point for the main event loop. If there are no events in the event queue, this method blocks the main thread. During that time, the window server is still processing events and may place new ones on the event queue. When that happens, the system wakes up the applications main thread so that it can process the newly received events. When the application dispatches an event, the sendEvent: method uses the type of the event to determine the appropriate target. There are two major types of input events: key events and mouse events. Key events are sent to the key windowthe window that is currently accepting key presses. Mouse events are dispatched to the window in which the event occurred. For mouse events, the window looks for the view in which the event occurred and dispatches the event to that object first. Views are responder objects and are capable of responding to any type of event. If the view is a control, it typically uses the event to generate an action message for its associated target. Other types of views may process the events directly, use them to generate notifications or send messages to your code, pass them to other objects for handling, or ignore them entirely. The overall process for handling events is described in detail in Cocoa Event-Handling Guide.
Automatic termination eliminates the need for users to quit an application. Instead, the system manages application termination transparently behind the scenes, terminating applications that are not in use to reclaim needed resources such as memory. Sudden termination allows the system to kill an applications process immediately without waiting for it to perform any final actions. The system uses this technique to improve the speed of operations such as logging out of, restarting, or shutting down the computer.
Automatic termination and sudden termination are independent techniques, although both are designed to improve the user experience of application termination. Although Apple recommends that applications support both, an application can support one technique and not the other. Applications that support both techniques can be terminated by the system without the application being involved at all. On the other hand, if an application supports sudden termination but not automatic termination, then it must be sent a Quit event, which it needs to process without displaying any user interface dialogs. Automatic termination transfers the job of managing processes from the user to the system, which is better equipped to handle the job. Users do not need to manage processes manually anyway. All they really need is to run applications and have those applications available when they need them. Automatic termination makes that possible while ensuring that system performance is not adversely affected. Applications must opt in to both automatic termination and sudden termination and implement appropriate support for them. In both cases, the application must ensure that any user data is saved well before termination can happen. And because the user does not quit an autoterminable application, such an application should also save the state of its user interface using the built-in Cocoa support. Saving and restoring the interface state provides the user with a sense of continuity between application launches.
34
CHAPTER 2
Note: If you do not opt in to automatic termination for your applications, the user must explicitly quit your application to remove it from memory. For information on how to support for automatic termination in your application, see Automatic Termination (page 35). For information on how to support sudden termination, see Sudden Termination (page 36).
Automatic Termination
Automatic termination is a feature that you must explicitly code for in your application. Declaring support for automatic termination is easy, but applications also need to work with the system to save the current state of their user interface so that it can be restored later as needed. The system can quit an auto-terminable application at any time, so saving this information maintains continuity for the application. Usually, the system quits the application some time after the user has closed all of its windows. However, the system may also quit an application with open windows if the application is not currently on screen, perhaps because the user hid it or switched spaces. Thus, to support automatic termination, you should do the following:
Declare your applications support for automatic termination, either programmatically or using an Info.plist key; see Enabling Automatic Termination in Your Application (page 35). Support saving and restoring your window configurations; see User Interface Preservation (page 37). Save the users data at appropriate times.
Single-window applications should implement strategies for saving data at appropriate checkpoints; see Data-Saving Strategies for Single-Window Applications (page 36). Multiwindow applications can use the autosaving and saveless documents capabilities in Cocoa; see Implementing a Document-Based Design (page 49).
Whenever possible, support sudden termination for your application as well; see Sudden Termination (page 36).
35
CHAPTER 2
Include the NSSupportsAutomaticTermination key (with the value YES) in the applications Info.plist file. Use the NSProcessInfo class to declare support for automatic termination dynamically. You can also use this class to change the default support of an application that includes the NSSupportsAutomaticTermination key in its Info.plist file.
When the user closes the application window or quits the application (applicationWillTerminate:) When the application is deactivated (applicationWillResignActive:) When the user hides your application (applicationWillHide:) Whenever the user makes a valid change to data in your application
The last item means that you have the freedom to save the users data at any time it makes sense to do so. For example, if the user is editing fields of a data record, you can save each field value as it is changed or you can wait and save all fields when the user displays a new record. Making these types of incremental changes ensures that the data is always up-to-date but also requires more fine-grained management of your data model. In such an instance, Core Data can help you make the changes more easily. For information about Core Data, see Core Data Starting Point.
Sudden Termination
Sudden termination lets the system know that your applications process can be killed directly without any additional involvement from your application. The benefit of supporting sudden termination is that it lets the system close applications more quickly, which is important when the user is shutting down a computer or logging out. An application has two ways to declare its support for sudden termination:
Include the NSSupportsSuddenTermination key (with the value YES) in the applications Info.plist file. Use the NSProcessInfo class to declare support for sudden termination dynamically. You can also use this class to change the default support of an application that includes the NSSupportsSuddenTermination key in its Info.plist file.
36
CHAPTER 2
One solution is to declare global support for the feature globally and then manually override the behavior at appropriate times. Because sudden termination means the system can kill your application at any time after launch, you should disable it while performing actions that might lead to data corruption if interrupted. When the action is complete, reenable the feature again. You disable and enable sudden termination programmatically using the disableSuddenTermination and enableSuddenTermination methods of the NSProcessInfo class. These methods increment and decrement a counter, respectively, maintained by the process. When the value of this counter is 0, the process is eligible for sudden termination. When the value is greater than 0, sudden termination is disabled. Enabling and disabling sudden termination dynamically also means that your application should save data progressively and not rely solely on user actions to save important information. The best way to ensure that your applications information is saved at appropriate times is to support the interfaces in Mac OS X v10.7 for saving your document and window state. Those interfaces facilitate the automatic saving of relevant user and application data. For more information about saving your user interface state, see User Interface Preservation (page 37). For more information about saving your documents, see Implementing a Document-Based Design (page 49). For additional information about enabling and disabling sudden termination, see NSProcessInfo Class Reference.
For each window, you must set whether the window should be preserved using the setRestorable: method. For each preserved window, you must specify an object whose job is to re-create that window at launch time. Any objects involved in your user interface must write out the data they require to restore their state later. At launch time, you must use the provided data to restore your objects to their previous state.
37
CHAPTER 2
The actual process of writing out your application state to disk and restoring it later is handled by Cocoa, but you must tell Cocoa what to save. Your applications windows are the starting point for all save operations. Cocoa iterates over all of your applications windows and saves data for the ones whose isRestorable method returns YES. Most windows are preserved by default, but you can change the preservation state of a window using the setRestorable: method. In addition to preserving your windows, Cocoa saves data for most of the responder objects associated with the window. Specifically, it saves the views and window controller objects associated with the window. (For a multiwindow document-based application, the window controller also saves data from its associated document object.) Figure 2-7 shows the path that Cocoa takes when determining which objects to save. Window objects are always the starting point, but other related objects are saved, too. Figure 2-7
Archived state
NSWindowController
NSDocument
Although the Cocoa responder objects save a lot of useful information about the state of your windows and views, they do not have any inherent knowledge about your applications data structures. Therefore, it is your responsibility to save any additional information needed to restore the window to its current state. There are several places where you can write out your custom state information:
If you subclass NSWindow or NSView, implement the encodeRestorableStateWithCoder: method in your subclass and use it to write out any relevant data. Alternatively, your custom responder objects can override the restorableStateKeyPaths method and use it to specify key paths for any attributes to be preserved. Cocoa uses the key paths to locate and save the data for the corresponding attribute. Attributes must be compliant with key-value coding and key-value observing.
If your window has a delegate object, implement the window:willEncodeRestorableState: method for the delegate and use it to store any relevant data. In your window controller, use the encodeRestorableStateWithCoder: method to save any relevant data or configuration information.
38
CHAPTER 2
Be judicious when deciding what data to preserve, and strive to write out the smallest amount of information that is required to reconfigure your window and associated objects. All Cocoa window and view objects save basic information about their size and location, plus information about other attributes that might affect the way they are currently displayed. For example, a tab view saves the index of the selected tab, and a text view saves the location and range of the current text selection. Those objects do not, however, save the actual data they are displaying. You are expected to save enough information during the save process to reattach the window to the same data objects later. Important: Never use the user interface preservation mechanism as a way to save your applications actual data. The archive created for interface preservation can change frequently and may be ignored altogether if there is a problem during the restoration process. Your application data should always be saved independently in data files that are managed by your application. For information on how to use coder objects to archive state information, see Archives and Serializations Programming Guide.
39
CHAPTER 2
these controller objects too, you typically use high-level application classes for your restoration classes. An application might use the application delegate, a document controller, or even a window controller as a restoration class. During the launch cycle, Cocoa does the following to create each preserved window: 1. Cocoa retrieves the windows restoration class from the preserved data and calls its restoreWindowWithIdentifier:state:completionHandler: class method. The restoreWindowWithIdentifier:state:completionHandler: class method is expected to call the provided completion handler with the desired window object. To do this, it typically does one of the following:
2.
It creates any relevant controller objects (including the window controller) that might normally be created to display the window. If the controller objects already exist (perhaps because they were already loaded from a nib file), the method gets the window from those existing objects.
If the window could not be created, perhaps because the associated document was deleted by the user, the restoreWindowWithIdentifier:state:completionHandler: should pass an error object to the completion handler. 3. Cocoa uses the returned window to restore it and any preserved responder objects to their previous state.
Standard Cocoa window and view objects are restored to their previous state without additional help. If you subclass NSWindow or NSView, implement the restoreStateWithCoder: method to restore any custom state. If you implemented the restorableStateKeyPaths method in your custom responder objects, Cocoa automatically sets the value of the associated attributes to their preserved values. Thus, you do not have to implement the restoreStateWithCoder: to restore these attributes.
For the window delegate object, Cocoa calls the window:didDecodeRestorableState: method to restore the state of that object. For your window controller, Cocoa calls the restoreStateWithCoder: method to restore its state.
When re-creating each window, Cocoa passes the windows unique identifier string to the restoration class. You are responsible for assigning identifier strings to your windows prior to preserving the window state. Your restoration class can use these identifiers to determine which window and associated objects it needs to re-create. The contents of an identifier string can be anything you want but should be something to help you identify the window later. For example, you might assign the string preferences as the unique identifier of your applications preferences window. For a single-window application whose main window controller and window are loaded from the main nib file, the job of your restoration class is fairly straightforward. Here, you could use the application delegates class as the restoration class and implement the restoreWindowWithIdentifier:state:completionHandler: method similar to the implementation shown in Listing 2-2. Because the application has only one window, it returns the main window directly. If you used the app delegates class as the restoration class for other windows, your own implementation could use the identifier parameter to determine which window to create.
40
CHAPTER 2
Listing 2-2
+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler { // Get the window from the window controller, // which is stored as an outlet by the delegate. // Both the app delegate and window controller are // created when the main nib file is loaded. MyAppDelegate* appDelegate = (MyAppDelegate*)[[NSApplication sharedApplication] delegate]; NSWindow* mainWindow = [appDelegate.windowController window]; // Pass the window to the provided completion handler. completionHandler(mainWindow, nil); }
+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler { // Get the window that has already been restored. NSWindow *window = getMyAlreadyRestoredWindow(); // Pass the window to the provided completion handler. completionHandler(window, nil); }
Cocoa triggers the restoration of your windows between calls to the applicationWillFinishLaunching: and applicationDidFinishLaunching: methods of your application delegate. This timing gives your application delegate an opportunity to create and restore its windows in the applicationWillFinishLaunching: method and be able to provide those windows to Cocoa later. If Cocoa asks for a window that (for whatever reason) you cannot restore or do not want to restore, pass nil for the window when calling the completion handler.
41
CHAPTER 2
42
CHAPTER 2
Figure 2-8
Levels
Menu bar Dock Modal panels Restart alert Font panel Application windows Untitled Untitled-1 Untitled-2
A view, an instance of the NSView class, defines the content for a rectangular region of a window. Views are the primary mechanism for presenting content and interacting with the user and have several responsibilities. For example:
Drawing and animation support. Views draw content in their rectangular area. Views that support Core Animation layers can use those layers to animate their contents. Layout and subview management. Each view manages a list of subviews, allowing you to create arbitrary view hierarchies. Each view defines layout and resizing behaviors to accommodate changes in the window size. Event handling. Views receive events. Views forward events to other objects when appropriate.
Z-order
For information about creating and configuring windows, see Window Programming Guide. For information about using and creating view hierarchies, see View Programming Guide.
Event Handling
The system window server is responsible for tracking mouse, keyboard, and other events and delivering them to your application. When the system launches an application, it creates both a process and a single thread for the application. This initial thread becomes the applications main thread. In it, the NSApplication object sets up the main run loop and configures its event-handling code, as shown in Figure 2-9. As the window server delivers events, the application queues those events and then processes them sequentially in the applications main run loop. Processing an event involves dispatching the event to the application object best suited to handle it. For example, mouse events are usually dispatched to the view in which the event occurred.
43
CHAPTER 2
Figure 2-9
Event queue
Application
Core objects
Note: A run loop monitors sources of input on a specific thread of execution. The applications event queue represents one of these input sources. While the event queue is empty, the main thread sleeps. When an event arrives, the run loop wakes up the thread and dispatches control to the NSApplication object to handle the event. After the event has been handled, control passes back to the run loop, which can then process another event, process other input sources, or put the thread back to sleep if there is nothing more to do. For more information about how run loops and input sources work, see Threading Programming Guide. Distributing and handling events is the job of responder objects, which are instances of the NSResponder class. The NSApplication, NSWindow, NSDrawer, NSView, NSWindowController, and NSViewController classes are all descendants of NSResponder. After pulling an event from the event queue, the application dispatches that event to the window object where it occurred. The window object, in turn, forwards the event to its first responder. In the case of mouse events, the first responder is typically the view object (NSView) in which the touch took place. For example, a mouse event occurring in a button is delivered to the corresponding button object. If the first responder is unable to handle an event, it forwards the event to its next responder, which is typically a parent view, view controller, or window. If that object is unable to handle the event, it forwards it to its next responder, and so , until the event is handled. This series of linked responder objects is known as the responder chain. Messages continue traveling up the responder chaintoward higher-level responder objects, such as a window controller or the application objectuntil the event is handled. If the event isn't handled, it is discarded. The responder object that handles an event often sets in motion a series of programmatic actions by the application. For example, a control object (that is, a subclass of NSControl) handles an event by sending an action message to another object, typically the controller that manages the current set of active views. While processing the action message, the controller might change the user interface or adjust the position of views in ways that require some of those views to redraw themselves. When this happens, the view and graphics infrastructure takes over and processes the required redraw events in the most efficient manner possible. For more information about responders, the responder chain, and handling events, see Cocoa Event-Handling Guide.
44
CHAPTER 2
An application can use native drawing technologies (such as Core Graphics and AppKit). An application can use OpenGL.
The native Mac OS X drawing technologies typically use the infrastructure provided by Cocoa views and windows to render and present custom content. When a view is first shown, the system asks it to draw its content. System views draw their contents automatically, but custom views must implement a drawRect: method. Inside this method, you use the native drawing technologies to draw shapes, text, images, gradients, or any other visual content you want. When you want to update your views visual content, you mark all or part of the view invalid by calling its setNeedsDisplay: or setNeedsDisplayInRect: method. The system then calls your views drawRect: method (at an appropriate time) to accommodate the update. This cycle then repeats and continues throughout the lifetime of your application. If you are using OpenGL to draw your applications content, you still create a window and view to manage your content, but those objects simply provide the rendering surface for an OpenGL drawing context. Once you have that drawing context, your application is responsible for initiating drawing updates at appropriate intervals. For information about how to draw custom content in your views, see Cocoa Drawing Guide.
Text Handling
The Cocoa text system, the primary text-handling system in Mac OS X, is responsible for the processing and display of all visible text in Cocoa. It provides a complete set of high-quality typographical services through the text-related AppKit classes, which enable applications to create, edit, display, and store text with all the characteristics of fine typesetting. The Cocoa text system provides all these basic and advanced text-handling features, and it also satisfies additional requirements from the ever-more-interconnected computing world: support for the character sets of all of the worlds living languages, powerful layout capabilities to handle various text directionality and nonrectangular text containers, and sophisticated typesetting capabilities such as control of kerning, ligatures, line breaking, and justification. Cocoas object-oriented text system is designed to provide all these capabilities without requiring you to learn about or interact with more of the system than is necessary to meet the needs of your application. Underlying the Cocoa text system is Core Text, which provides low-level, basic text layout and font-handling capabilities to higher-level engines such as Cocoa and WebKit. Core Text provides the implementation for many Cocoa text technologies. Application developers typically have no need to use Core Text directly. However, the Core Text API is accessible to developers who must use it directly, such as those writing applications with their own layout engine and those porting older ATSUI- or QuickDraw-based codebases to the modern world. For more information about the Cocoa text system, see Cocoa Text Architecture Guide.
45
CHAPTER 2
Open Recent > Clear Menu clearRecentDocuments: Close Save Save As... Revert to Saved Page Setup... Print...
performClose: saveDocument: saveDocumentAs: revertDocumentToSaved: runPageLayout: printDocument:
The template has similar ready-made connections for the Edit, Format, View, Window, and Help menus. If your application does not support any of the supplied actions (for example, printing), you should remove the associated menu items (or menu) from the nib. Alternatively, you may want to repurpose and rename menu commands and action methods to suit your own application, taking advantage of the menu mechanism in the template to ensure that everything is in the right place.
46
CHAPTER 2
Connect the corresponding menu item to a first responder method. Connect the menu item to a method of your custom application object or your application delegate object.
Of these two techniques, the first is more common given that many menu commands act on the current document or its contents, which are part of the responder chain. The second technique is used primarily to handle commands that are global to the application, such as displaying preferences or creating a new document. It is possible for a custom application object or its delegate to dispatch events to documents, but doing so is generally more cumbersome and prone to errors. In addition to implementing action methods to respond to your menu commands, you must also implement the methods of the NSMenuValidation protocol to enable the menu items for those commands. Step-by-step instructions for connecting menu items to action methods in your code are given in Designing User Interfaces in Xcode. For more information about menu validation and other menu topics, see Application Menu and Pop-up List Programming Topics.
47
CHAPTER 2
48
CHAPTER 3
A multiwindow, document-based application is one of the more common types of applications. Documents are identically contained but uniquely composed sets of data that can be stored in files. Traditional word processors and spreadsheet applications are examples of document-based applications. One application typically handles multiple documents, each in its own window, and often displays more than one document at a time. Cocoa provides excellent support for this design through a subsystem called the document architecture.
49
CHAPTER 3
Figure 3-1
NSDocument
NSDocument
NSWindowController
NSWindowController
NSWindowController
A document controller creates empty documents in response to the New item in the File menu It creates documents initialized with data from a file in response to the Open item in the File menu It tracks and manages those documents It handles document-related menu items, such as Open Recent
When a user chooses New from the File menu, the NSDocumentController object gets the appropriate NSDocument subclass from the applications Information property list and allocates and initializes an instance of this class. Likewise, when the user chooses Open, the NSDocumentController object displays the Open panel, gets the users selection, finds the NSDocument subclass for the file, allocates an instance of the class, and initializes it with data from the file. In both cases, the NSDocumentController object adds a reference to the document object to an internal list to help manage its documents.
50
CHAPTER 3
the application starts up, when it terminates, when the system is shutting down, and when documents are opened or printed from the Finder. Most of the time, you can use this class as is to manage your applications documents. Alternatively, you can make the application delegate a custom object and implement the delegate methods invoked as a result of the same events, and these methods will be invoked instead (see NSApplicationDelegate Protocol Reference). Generally, the default NSDocumentController object is an adequate application controller for most situations. If the default object does not meet all of your requirements for an application controllerfor example, handling user preferences or responding to uncommon application delegate messagesusually you should create a separate controller object (instead of subclassing NSDocumentController). However, it is possible to create a custom subclass to tweak your applications document-handling behavior, as described in You Rarely Need to Subclass NSDocumentController (page 58).
An NSDocument object manages the display and capture of the data in its windows (with the assistance of its window controllers) It loads and stores (that is, reads and writes) the persistent data associated with its document It responds to action messages to save, print, revert, and close documents It runs and manages the Save and Page Setup dialogs
A fully implemented NSDocument object also knows how to track its edited status, perform undo and redo operations, print document data, and validate its menu items. Although these behaviors arent completely provided by default, the NSDocument object does assist the developer in implementing each, in the following ways:
For tracking edited status, NSDocument provides an API for updating a change counter. For undo and redo operations, NSDocument lazily creates an NSUndoManager instance when one is requested, responds appropriately to Undo and Redo menu commands, and updates the change counter when undo and redo operations are performed. For printing, NSDocument facilitates the display of the Page Setup dialog and the subsequent modification of the NSPrintInfo object used in printing. To do this, subclasses of NSDocument must override printOperationWithSettings:error:. To validate menu items, NSDocument implements validateUserInterfaceItem: to manage the enabled state of the Revert and Save As menu items. If you want to validate other menu items, you can override this method, but be sure to invoke the superclass implementation. For more information on menu item validation, see NSUserInterfaceValidations Protocol Reference.
51
CHAPTER 3
When designing your document objects, you should always maintain a clean separation between these data-handling activities of the document object itself and the code for managing the visual presentation of that data. The document object is responsible for the data, including the reading and writing of that data to disk. The visual presentation of that data is the responsibility of the associated window controller object. Keeping a clean separation between these two activities makes for a more modular design that can be updated more easily in the future. Any and all objects that are part of the persistent state of a document should be considered part of that documents model. Sometimes the NSDocument object itself has some data that would be considered part of the model. For example, the Sketch example application has a subclass of NSDocument named SKTDrawDocument; objects of this class might have an array of SKTGraphic objects that comprises the model of the document. In addition to the actual SKTGraphic objects, the SKTDrawDocument object contains some data that should technically be considered part of the model, because the order of the graphics within the documents array matters in determining the front-to-back ordering of the SKTGraphic objects. An NSDocument object should not contain or require the presence of any objects that are specific to the applications user interface. Although a document can own and manage NSWindowController objectswhich present the document visually and allow the user to edit itit should not depend on these objects being there. For example, it might be desirable to have a document open in your application without having it visually displayed. For details about subclassing NSDocument, see You Must Subclass NSDocument (page 53). If you have a large data set or require a managed object model, you may want to use NSPersistentDocument, a subclass of NSDocument, to create a document-based application that uses Core Data. For more information, see Core Data Starting Point.
52
CHAPTER 3
Note: NSWindowController does not depend on being the controlled window's delegate to do its job, and it doesn't implement any NSWindow delegate methods. A subclass of NSWindowController, however, is a fine place to put implementations of NSWindow delegate methods, and if you do so you'll probably need to connect the delegate outlet of the window to the File's Owner of the nib file as described. But you do not have to do so for NSWindowController itself to work properly. The Xcode document-based application template does not subclass NSWindowController, and you do not need to do so if you are writing a simple application. However, if you are writing an application with more advanced requirements, you will almost certainly want to do so. For more information, see You May Want to Subclass NSWindowController (page 56).
Document Architecture Objects and Subclasses Number of objects Subclassing 1 per document 1 per window Required Optional (but likely) Optional (but unlikely)
53
CHAPTER 3
Subclass only NSDocument for documents that have only one window and are simple enough that there isnt much benefit in splitting the controller layer into a model-controller and a view-controller. The NSDocument subclass provides storage for the model and the ability to load and save document data. It also has any outlets and actions required for the user interface. It overrides the windowNibName method to return the nib file name used for documents of this type. The NSDocument object automatically creates an NSWindowController object to manage that nib file, but the NSDocument object serves as the Files Owner proxy object for the nib file. The document architecture requires that you override some NSDocument methods (among several choices), and recommends overriding several others in certain situations. You must override one reading and one writing method. In the simplest case, you can override the two data-based reading and writing methods. If you need to deal with the location of the file, then you can override the URL-based reading and writing methods instead. If your application supports document files that are file packages, then you can override the file-wrapper reading and writing methods instead. Note that these methods read and write entire files at once. If your application has a large data set, you may want instead to read and write pieces of your files at different times. For information about reading and writing files, see File System Programming Guide.
54
CHAPTER 3
The readFromData:ofType:error: method may be overridden to convert an NSData object containing document data of a certain type into the documents internal data structures and display that data in a document window. The NSData object usually results from the document reading a document file. The readFromURL:ofType:error: method reads data from a file, creates an NSFileWrapper object from it, and gives this object to the readFromFileWrapper:ofType:error: method. If this object represents a simple file, it is passed to the readFromData:ofType:error: method for processing; otherwise (if the object represents a directory), the readFromFileWrapper:ofType:error: method can be overridden to handle the situation. Subclasses can override any of these methods instead of the data-based reading methods if the way NSDocument reads and writes document data is not sufficient; their override implementations, however, must also assume the loading duties of the data-based reading methods.
The dataOfType:error: method may be overridden to create and return document data (packaged as an NSData object) of a supported type, usually in preparation for writing that data to a file. By default, the writeToURL:ofType:error: method writes data to a file after obtaining the data from the fileWrapperOfType:error: method, which gets it from the dataOfType:error: method. You can override the writeToURL:ofType:forSaveOperation:originalContentsURL:error: method instead of one of the other three, simple writing methods if your document writing machinery needs access to the on-disk representation of the document revision that is about to be overwritten. This method is responsible for doing document writing in a way that minimizes the danger of leaving the disk to which writing is being done in an inconsistent state in the event of an application crash, system crash, hardware failure, power outage, and so on.
55
CHAPTER 3
By default, when NSDocument runs the Save panel and the document has multiple writable document types, NSDocument inserts an accessory view near the bottom of the panel. This view contains a pop-up menu of the writable types. If you dont want this pop-up menu, override shouldRunSavePanelWithAccessoryView to return NO. You can also override prepareSavePanel: to customize of the Save panel. Warning: If your NSDocument subclass uses an NSTextView object to display its data and it has an active NSUndoManager object, your document-writing method override (or another override called during document saving) should send the NSTextView object a breakUndoCoalescing message. Send this message when saving the text views contents to preserve proper tracking of unsaved changes and the documents dirty state. See Implementing Undo (page 61) for more information about supporting undo. You can define types for your application using the Xcode Property List editor, which provides an editable list of document types as described in Supported Document Types Are Declared in the Application Property List (page 59).
56
CHAPTER 3
57
CHAPTER 3
If you have multiple window controllers for a single document, you may want to explicitly control document closing. By default, a document closes when its last remaining window controller closes. However, if you want the document to close when a particular window closesthe documents main window, for examplethen you can send the main window controller a setShouldCloseDocument: message with a value of YES.
You can make an instance of your subclass in your applications main nib file. This instance becomes the shared instance. You can create an instance of your subclass in your application delegates applicationWillFinishLaunching: method.
The first NSDocumentController object to be created becomes the shared instance. The AppKit framework creates the shared instance (using the NSDocumentController class) during the finish launching phase of application startup. So if you need a subclass instance, you must create it before AppKit does.
A skeletal NSDocument subclass implementation By default, the subclass of NSDocument is named MyBigDocument. The project includes MyBigDocument.h and MyBigDocument.m. The latter file includes commented blocks for important methods, including an init method that initializes and returns self, providing a location for subclass-specific initialization. It also includes a fully implemented windowNibName method that returns the name of the document window nib file, MyBigDocument, and an override of windowControllerDidLoadNib:. In addition, the template includes skeletal implementations of the dataOfType:error: and readFromData:ofType:error: basic writing and reading methods showing how to set the value of the NSError object passed as an indirect parameter.
58
CHAPTER 3
This nib file is named MyBigDocument.xib (assuming you use the default subclass name MyBigDocument). The subclass of NSDocument is made Files Owner of the nib file. It has an outlet named window connected to its window object. The window has only a text field on it, with the words "Your document contents here".
The applications main menu nib file The main menu nib file, named MainMenu.xib, contains an application menu (named with the applications name), a File menu (with all of its associated document commands), an Edit menu (with text editing commands and Undo and Redo menu items), and Format, View, Window, and Help menus (with their own menu items representing commands). These menu items, as well as all of the menu items of the File menu, are connected to the appropriate first-responder action methods. For example, the About menu item is connected to the orderFrontStandardAboutPanel: action method that displays a standard About window. See Implementing the Application Menu Bar (page 46) for more information about the main menu nib file provided by the Xcode application templates.
The application's information property list The <applicationName>-Info.plist file contains placeholder values for global application keys, as well as for the CFBundleDocumentTypes key, whose associated value is a dictionary containing key-value pairs specifying information about the document types the application works with including their NSDocument subclass.
The Xcode Cocoa Application project template dialog also provides an option checkbox labeled Use Core Data which, if used in conjunction with the Create Document-Based Application option, creates a document-based application containing a subclass of NSPersistentDocument. For a tutorial that shows how to implement a simple document-based application, see Building a Text Editor in 15 Minutes.
59
CHAPTER 3
Similarly, Launch Services may use information about the icon file for the type. Document types information is associated with the CFBundleDocumentTypes key as an array of dictionaries, each of which contains the key-value pairs that define the document type. If your application has some types that it can read but not write, you can declare this by setting the role for those types to Viewer instead of Editor in Xcode. If your application has some types that it can write but not read, you can declare this by using the NSExportableAs key. You can include the NSExportableAs key in the type dictionary for another type that your document class supports, usually the type dictionary for the most native type for your document class. Its value is an array of type names that your document class can write, but not read. The Sketch example uses this key to allow it to export TIFF and EPS images even though it cannot read those types. Write-only types can be chosen only when doing Save As operations. They are not allowed for Save operations. Sometimes an application might understand how to read a type, but not how to write it, and when it reads documents of that type, it should automatically convert them to another type that you can write. An example of this would be an application that can read documents from an older version or from a competing product. It might want to read in the old documents and automatically convert them to the new native format. The first step is to add the old type as a read-only type. By doing this, your application is able to open the old files, but they come up as untitled files. If you want to automatically convert them to be saved as your new type, you can override the readFrom... methods in your NSDocument subclass to call super and then reset the filename and type afterwards. You should use setFileType: and setFileURL: to set an appropriate type and name for the new document. When setting the filename make sure to strip the filename extension of the old type from the original filename, if it is there, and add the extension for the new type. For more information about the property list, see The Information Property List File (page 80).
60
CHAPTER 3
Once the user selects a type, your action method can use the NSDocumentController method makeUntitledDocumentOfType:error: to create a document of the correct type. After creating the document, your method should add it to the document controller's list of documents, and it should send the document makeWindowControllers and showWindows messages. Alternatively, if you subclass NSDocumentController, you can override the defaultType method to determine the document type and return it, when the user chooses New from the File menu.
Implementing Undo
The key to implementing undo properly is to have well-defined primitives for changing your document. Each model object, plus the NSDocument subclass itself, should define the set of primitive methods that can change it. Each primitive method is then responsible for using the undo manager to enqueue invocations that undo the action of the primitive method. For example, if you decide that setColor: is a primitive method for one of your model objects, then inside of setColor: your object would do something like the following:
[[[myDocument undoManager] prepareWithInvocationTarget:self] setColor:oldColor]
This message causes the undo manager to construct and save an invocation. If the user later chooses Undo, the saved invocation is invoked and your model object receives another setColor: message, this time with the old color. You dont have to keep track of whether commands are being undone to support redo. In fact, the way redo works is by watching what invocations get registered as the undo is happening and recording them on the redo stack. You can use the setUndoManager: method if you need to use a subclass or otherwise need to change the undo manager used by the document. Because many discrete changes might be involved in a user-level action, all the undo registrations that happen during a single cycle of the event loop are usually grouped together and are undone all at once. NSUndoManager has methods that allow you to control the grouping behavior further if you need to. Another aspect of good undo implementation is to provide action names so that the Undo and Redo menu items can have more descriptive titles. Undo action names are usually best set in action methods instead of the change primitives in your model objects. This is because many primitive changes might go into one user action, or different user actions might result in the same primitives being called in different ways. The Sketch example application implements undo in action methods. For more information on supporting undo in your application, see Undo Architecture.
61
CHAPTER 3
62
CHAPTER 3
63
CHAPTER 3
YES. The ideal baseline for saveless documents is this: The document data that users see in an application
window is identical to the document on disk at all times. For practical reasons, the system does not attempt to save every change immediately, but it saves documents often enough and at the correct times to ensure that the document in memory and the one on disk are effectively the same. Part of the implementation of saveless documents is file coordination, a mechanism that serializes access to files among processes to prevent inconsistencies due to nonsequential reading and writing. Applications use file coordination so that users dont need to remember to save document changes before causing the documents file to be read by another application. Document-based Cocoa applications use file coordination automatically. Automatic document saving is supported by the implementation of autosaving in place. Autosaving in place and autosaving elsewhere both protect against the user losing work due to application crashes, kernel panics, and power failures. However, autosaving in place differs from autosaving elsewhere in that it overwrites the actual document file rather than writing a new file next to it containing the autosaved document contents. (Autosaving in place performs a safe save by writing to a new file first, then moving it into the place of the document file when done.) The document architecture still uses autosaving elsewhere to save untitled documents that have content but have not been explicitly saved and named by the user. In this case, untitled documents are autosaved in ~/Library/Autosave Information. In addition, NSDocument saves earlier revisions of documents elsewhere, giving the user access to previous versions. The saveless-documents model automates crash protection but preserves the ability for users to save documents explicitly. It also automates maintenance of multiple older versions. Users can save immediately in the traditional way (by choosing File > Save or pressing Command-S). For an untitled document, an explicit Save command presents a dialog enabling the user to name the document and specify the location where it is to be written to disk. You should not invoke the autosavesInPlace method to find out whether autosaving is being done. Instead, the document architecture passes one of two new autosaving-related enumerators as an NSSaveOperationType parameter to your overrides of the NSDocument methods beginning with save... and write..., and you can examine those values. The autosave enumerators are NSAutosaveInPlaceOperation and NSAutosaveElsewhereOperation. The old NSAutosaveOperation enumerator is equivalent to NSAutosaveElsewhereOperation and is deprecated in Mac OS X v10.7. Before you enable autosaving, consider the saving performance of your application. If your application saves quickly, there is little reason not to enable it. But if your application saves slowly, enabling autosaving could cause periodic blocking of your user interface while saving is happening. So, for example, if you have already implemented the autosaving behavior introduced in Mac OS X v10.4 (sending setAutosavingDelay: to the NSDocumentController object with a nonzero value), then your applications saving performance is probably acceptable, and opting into autosaving in place is as simple as overriding autosavesInPlace to return YES. Otherwise, you may first need to address any issues with your document model or saving logic that could hinder saving performance.
64
CHAPTER 3
YES to enable asynchronous saving. In this case, NSDocument creates a separate writing thread and invokes writeSafelyToURL:ofType:forSaveOperation:error: on it. However, the main thread remains blocked until an object on the writing thread invokes the unblockUserInteraction method.
When unblockUserInteraction is invoked, the application resumes dequeueing user interface events, the user is able to continue editing the document, even if the writing of document data takes some time. The right moment to invoke unblockUserInteraction is when an immutable snapshot of the documents contents has been taken, so that writing out the snapshot of the documents contents can continue safely on the writing thread while the user continues to edit the document on the main thread. For more information on asynchronous document saving, see Application Kit Release Notes (Lion) and comments in the NSDocument header file.
65
CHAPTER 3
1.
The NSWindowController method setDocument: sets the restoration class of document windows to the class of the shared NSDocumentController object. The NSWindow object invalidates its restorable state whenever its state changes by sending invalidateRestorableState to itself. At the next appropriate time, Cocoa sends the window an encodeRestorableStateWithCoder: message, and the window encodes identification and status information into the passed-in encoder. When the system restarts, Cocoa relaunches the application and sends the restoreWindowWithIdentifier:state:completionHandler: message to the NSApp application object. Applications can override this method to do any general work needed for window restoration, such as substituting a new restoration class or loading it from a separate bundle.
NSApp decodes the restoration class for the window, sends the restoreWindowWithIdentifier:state:completionHandler: message to the restoration class object, and returns YES.
2.
3.
4. 5.
The restoration class reopens the document and locates its window. Then it invokes the passed-in completion handler with the window as a parameter. Cocoa sends the restoreStateWithCoder: message to the window, which decodes its restorable state from the passed-in NSCoder object and restores the details of its content.
Although the preceding steps describe only window restoration, in fact every object inheriting from NSResponder has its own restorable state. For example, an NSTextView object stores the selected range (or ranges) of text in its restorable state. Likewise, an NSTabView object records its selected tab, an NSSearchField object records the search term, an NSScrollView object records its scroll position, and an NSApplication object records the z-order of its windows. An NSDocument object has state as well. Although NSDocument does not inherit from NSResponder, it implements many NSResponder methods, including these restoration methods. When the application is relaunched, Cocoa sends the restoreStateWithCoder: message to the relevant objects in turn, proceeding top-down: first to the NSApplication object, then to each NSWindow object, then to the NSWindowController object, then to the NSDocument object, and then to each view that has saved state.
66
CHAPTER 4
Enabling a window of your application to assume full-screen mode, taking over the entire screen, provides users with a more immersive, cinematic experience. Full-screen appearance can be striking and can make your application stand out. From a practical standpoint, full-screen mode presents a better view of users data, enabling them to concentrate fully on their content without the distractions of other applications or the desktop. In full-screen mode, by default, the menu bar and Dock are autohidden; that is, they are normally hidden but reappear when the user moves the pointer to the top or bottom of the screen. A full-screen window does not draw its titlebar and may have special handling for its toolbar. The full-screen experience makes sense for many applications but not for all. For example, the Finder, Address Book, and Calculator would not provide any benefit to users by assuming full-screen mode. The same is probably true for most utility applications. Media-rich applications, on the other hand, can often benefit from full-screen presentation. Beginning with Mac OS X v10.7, Cocoa includes support for full-screen mode through APIs in NSApplication, NSWindow, and NSWindowDelegate protocol. When the user chooses to enter full-screen mode, Cocoa dynamically creates a space and puts the window into that space. This behavior enables the user to have one window of an application running in full-screen mode in one space, while using other windows of that application, as well as other applications, on the desktop in other spaces. While in full-screen mode, the user can switch between windows in the current application or switch applications. Applications that have implemented full-screen user interfaces in previous versions of Mac OS X should consider standardizing on the full-screen APIs in Mac OS X v10.7.
67
CHAPTER 4
Behavior The frontmost window with this collection behavior becomes the full-screen window. A window with this collection behavior has a full-screen button in the upper right of its titlebar. Windows with this collection behavior can be shown in the same space with the full-screen window.
NSWindowCollectionBehaviorFullScreenAuxiliary
When a window goes into full-screen mode, the styleMask changes to NSFullScreenWindowMask to reflect the state of the window.The setting of the styleMask goes through the setStyleMask: method. As a result, a window can override this method if it has customization to do when entering or exiting full-screen. A window can be taken into or out of full-screen mode using the toggleFullScreen: method. If an application supports full-screen mode, it should add a menu item to the View menu with toggleFullScreen: as the action, and nil as the target.
The window delegate has the following corresponding window delegate notification methods:
windowWillEnterFullScreen: windowDidEnterFullScreen: windowWillExitFullScreen: windowDidExitFullScreen:
The NSWindowDelegate protocol methods supporting full-screen mode are listed in Table 4-1.
68
CHAPTER 4
Window delegate methods supporting full-screen mode Description Invoked to allow the delegate to modify the full-screen content size. Returns the presentation options the window will use when transitioning to full-screen mode. Invoked when the window is about to enter full-screen mode.
window: willUseFullScreenContentSize:
window: willUseFullScreenPresentationOptions:
customWindowsToEnterFullScreenForWindow:
window: Invoked to start the window animation startCustomAnimationToEnterFullScreenWithDuration: into full-screen mode, including
transitioning to a new space. You can implement this method to perform custom animation with the given duration to be in sync with the system animation.
windowDidFailToEnterFullScreen:
Invoked if the window failed to enter full-screen mode. The system has started its animation out of full-screen mode, including transitioning back to the desktop space. You can implement this method to perform custom animation with the given duration to be in sync with the system animation. Invoked when the window is about to exit full-screen mode. Invoked if the window failed to exit full-screen mode.
customWindowsToExitFullScreenForWindow:
For details, see NSWindowDelegate Protocol Reference and the Application Kit Release Notes (Lion).
69
CHAPTER 4
70
CHAPTER 5
During the design phase of creating your application, you need to think about how to implement certain features that users expect in well-formed Mac OS X applications. Integrating these features into your application architecture can have an impact on the data model or may require cooperation between significantly different portions in the application.
71
CHAPTER 5
Mac OS X is designed to accommodate assistive technologies and has many built-in features to help people with disabilities. Users access most of this functionality through the Universal Access pane of System Preferences. Some of these built-in technologies take advantage of the same accessibility architecture that allows external assistive technologies to access your application. Designing your application with accessibility in mind not only allows you to reach a larger group of users, it results in a better experience for all your users. As a first step in designing your application, be sure to read Apple Human Interface Guidelines. That book provides detailed specifications and best practices for designing and implementing an intuitive, consistent, and aesthetically pleasing user interface that delivers the superlative experience Macintosh users have come to expect. During the design process, you also should be aware of the accessibility perspective on many basic design considerations. Consider the following basic accessibility-related design principles:
Support full keyboard navigation. For many users, a mouse is difficult, if not impossible, to use. Consequently, a user should be able to perform all your applications functions using the keyboard alone. Dont override built-in keyboard shortcuts. This applies both to the keyboard shortcuts Mac OS X reserves (listed in Keyboard Shortcuts Quick Reference) and to the accessibility-related keyboard shortcuts (listed in Accessibility Keyboard Shortcuts). As a general rule, you should never override reserved keyboard shortcuts. In particular, you should take care not to override accessibility-related keyboard shortcuts or your application will not be accessible to users who enable full keyboard access. A corollary to this principle is to avoid creating too many new keyboard shortcuts that are specific to your application. Users should not have to memorize a new set of keyboard commands for each application they use.
Provide alternatives for drag-and-drop operations. If your application relies on drag-and-drop operations in its workflow, you should provide alternate ways to accomplish the same tasks. This may not be easy; in fact, it may require the design of an alternate interface for applications that are heavily dependent on drag and drop. Make sure theres always a way out of your applications workflow. This is important for all users, of course, but its essential for users of assistive technologies. A user relying on an assistive application to use your application may have a somewhat narrower view of your applications user interface. For this reason, its especially important to make canceling operations and retracing steps easy.
In addition to the basic design principles, you should consider the requirements of specific disabilities and resulting design solutions and adaptations you can implement. The main theme of these suggestions is to provide as many alternate modes of content display as possible, so that users can find the way that suits their needs best. Consider the following categories of disabilities:
Visual Disabilities. Visual disabilities include blindness, color blindness, and low vision. Make your application accessible to assistive applications, such as screen readers. Ensure that color is not the only source of come particular information in your user interface. Provide an audio option for all visual cues and feedback, as well as succinct descriptions of images and animated content. Hearing Disabilities. When you design the audio output of your application, remember that some users may not be able to hear your applications sound effects well or at all. And, of course, there are situations in which any user could not use audio output effectively, such as in a library. Providing redundant audio and visual output can aid comprehension for users with other types of disabilities as well, such as cognitive and learning disabilities.
72
CHAPTER 5
Motor and Cognitive Disabilities. People with motor disabilities may need to use alternatives to the standard mouse and keyboard input devices. Other users may have difficulty with the fine motor control required to double-click a mouse or to press key combinations on the keyboard. Users with cognitive or learning disabilities may need extra time to complete tasks or respond to alerts.
Mac OS X provides support for many types of disabilities at the system level through solutions offered in the Universal Access system preferences, illustrated in Figure 5-1. In addition, most standard Cocoa objects implement accessibility through the NSAccessibility protocol, providing reasonable default behavior in most cases, Cocoa applications built with standard objects are automatically accessible. In general, you need to explicitly implement the NSAccessibility protocol methods only if you subclass one of them, adding new behavior. Figure 5-1 Universal Access system preference dialog
The NSAccessibility informal protocol defines methods that Cocoa classes must implement to make themselves available to an external assistive application. An assistive application interacts with your application to allow persons with disabilities to use your application. For example, a person with a visual impairment could use an application to convert menu items and button labels into speech and then perform actions by verbal command. An accessible object is described by a set of attributes that define characteristics such as the object type, its value, its size and position on the screen, and its place in the accessibility hierarchy. For some objects, the set of attributes can include parameterized attributes. Parameterized attributes behave similar to a function by allowing you to pass a parameter when requesting an attribute value. For more information, see Accessibility Overview.
73
CHAPTER 5
74
CHAPTER 5
Figure 5-2
System Store Spotlight Importers Spotlight Importers MDItem kMDItemTitle kMDItemAuthors kMDItemCopyright . . .
Spotlight
Apple provides importers for many standard file types that the system uses, including RTF, JPEG, Mail, PDF and MP3. However, if you define a custom document format, you must create a metadata importer for your own content. Xcode provides a template for writing Spotlight importer plug-ins. For information about writing a Spotlight importer, see Spotlight Importer Programming Guide. In addition, you may wish to provide metadata search capability in your application. Cocoa provides the NSMetadataQuery class which enables you to construct queries using a subset of the NSPredicate classes and execute the queries asynchronously. For information about providing Spotlight search capability in your application, see Spotlight Query Programming Guide. For more information about Spotlight, see Spotlight Overview.
75
CHAPTER 5
For applications that depend on a graphical user interface, the dwindling size of user interface elements can make them too small for users to interact with effectively. For example, a checkbox might be too small to click easily. The solution is to make the drawing sizes specified by the application independent of the display's pixel resolution and allow arbitrary scaling between the two. Depending on the type of application, the user interface, and the drawing technologies used, you may need to update your code to provide the best user experience on a resolution-independent system. Cocoa applications require little work to support resolution independence because the Cocoa frameworks handle the scaling for you. Standard Cocoa controls, such as buttons and checkboxes, automatically scale correctly. However, depending on how you manipulate windows and the views within them, you may need to make some changes. In addition, you may need to provide high-resolution versions of any custom artwork. If you draw directly to the screen on a pixel-by-pixel basis, you need to obtain the current scale factor and scale all your images manually. To obtain the scale factor for a particular window, use the userSpaceScaleFactor method in the NSWindow class. In some cases, you may need to convert rectangles or points from the coordinate system of one NSView instance to another (typically the superview or subview), or from one NSView instance to the containing window. The NSView class defines methods for this purpose and, in Mac OS X v10.5 and later, methods that should be used when performing pixel alignment of view content. See Cocoa Drawing Guide and View Programming Guide for more information about coordinate systems. Make sure your application's custom artwork can scale properly as the pixel density of displays increases. That is, the art needs to be larger in terms of pixel dimensions to avoid loss of resolution at high scale factors onscreen. For example, application icons, custom buttons and other controls, and custom images may need high-resolution versions of their artwork. If an existing standard control or icon provides the same function as your custom version, you should consider adopting the standard version. For application icons, you should make sure that the icon family includes images up to 512 x 512 pixels. For new icons, its easiest to design the large icons first and then scale them down. You can use the Icon Composer utility available in the /Developer/Applications/Utilities/ folder to create your icons. Icon Composer v2.0 and later includes support for larger icon sizes. For more information about resolution independence, see Resolution Independence Guidelines.
76
CHAPTER 5
To support fast user switching, there are certain guidelines you should follow in developing your applications, most of which describe safe ways to identify and share system resources. A summary of these guidelines is as follows:
Incorporate session ID information into the name of any entity that appears in a global namespace, including file names, shared memory regions, semaphores, and named pipes. Tagging such application-specific resources with a session ID is necessary to distinguish them from similar resources created by applications in a different session. The Security layer of the system assigns a unique ID to each login session. Incorporating this ID into cache file or temporary directory names can prevent namespace collisions when creating these files. See Supporting Fast User Switching for information on how to get the session ID.
Don't assume you have exclusive access to any resource, especially TCP/IP ports and hardware devices. Don't assume there is only one instance of a per-user service running. Use file-level or range-level locks when accessing files. Accept and handle user switch notifications. See User Switch Notifications for more information.
77
CHAPTER 5
78
CHAPTER 6
Configuring your application properly is an important part of the development process. Mac OS X applications use a structured directory configuration to manage their code and resource files. And although most of the files are custom and are there to support your application, some are required by the system or the App Store and must be configured properly. All of the requirements for preparing your application and distributing it on the App Store are described in the App Store Resource Center.
79
CHAPTER 6
For more information about using Xcode to create, configure, build, and debug your project, as well as archiving your program to package it for distribution or submission to the Mac App Store, see Xcode 4 User Guide.
Xcode automatically adds some important keys to the Info.plist file of all new projects and sets their initial values. However, there are several keys that Mac OS X applications use commonly to configure their launch environment and runtime behavior. Here are some keys that you might want to add to your applications Info.plist file specifically for Mac OS X:
80
CHAPTER 6
For detailed information about these and other keys that you can include in your applications Info.plist file, see Information Property List Key Reference.
A typical application bundle Description The executable file containing your applications code. The name of this file is the same as your application name minus the .app extension. This file is required.
Contents/Info.plist
Also known as the information property list file, a file containing configuration data for the application. The system uses this data to determine how to interact with the application at specific times. This file is required. For more information, see The Information Property List File (page 80).
81
CHAPTER 6
Files
Description
nib file. This file contains the default interface objects to load at application launch time. Typically, this nib file contains the applications menu bar and application delegate object. It may also contain other controller objects that should always be available at launch time. (The name of the main nib file can be changed by assigning a different value to the NSMainNibFile key in the Info.plist file.) This file is optional but recommended. For more information, see The Information Property List File (page 80)
Contents/Resources/sun.png (or other resource files)
Nonlocalized resources are placed at the top level of the Resources directory (sun.png represents a nonlocalized image file in the example). The application uses nonlocalized resources when no localized version of the same resource is provided. Thus, you can use these files in situations where the resource is the same for all localizations. Localized resources are placed in subdirectories with an ISO 639-1 language abbreviation for a name plus an .lproj suffix. Although more human readable names (such as English.lproj, French.lproj, and German.lproj) can be used for directory names, the ISO 639-1 names are preferred because they allow you to include an ISO 3166-1 regional designation. (For example, the en_GB.lproj directory contains resources localized for English as spoken in Great Britain, the es.lproj directory contains resources localized for Spanish, and the de.lproj directory contains resources localized for German.)
A Mac OS X application should be internationalized and have a language.lproj directory for each language it supports. Even if you provide localized versions of your resources, though, include a default version of these files at the top level of your Resources directory. The default version is used when a specific localization is not available. At runtime, you can access your applications resource files from your code using the following steps: 1. 2. Obtain a reference to your applications main bundle object (typically using theNSBundle class). Use the methods of the bundle object to obtain a file-system path to the desired resource file.
82
CHAPTER 6
3.
You obtain a reference to your applications main bundle using the mainBundle class method of NSBundle. The pathForResource:ofType: method is one of several NSBundle methods that you can use to retrieve the location of resources. The following example shows how to locate a file called sun.png and create an image object using it. The first line gets the bundle object and the path to the file. The second line creates an NSImage object that you could use to display the image in your application.
NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"sun" ofType:@"png"]; NSImage* sunImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
Note: If you prefer to use Core Foundation to access bundles, you can obtain a CFBundleRef opaque type using the CFBundleGetMainBundle function. You can then use that opaque type plus the Core Foundation functions to locate any bundle resources. For information on how to access and use resources in your application, see Resource Programming Guide. For more information about the structure of a Mac OS X application bundle, see Bundle Programming Guide.
Code-generated text, including locale-specific aspects of date, time, and number formatting Static textfor example, strings you specify programmatically and display in parts of your user interface or an HTML file containing application help Icons (including your application icon) and other images when those images either contain text or have some culture-specific meaning Sound files containing spoken language Nib files
Users select their preferred language from the Language and Text system preference, shown in Figure 6-2.
83
CHAPTER 6
Figure 6-2
Your application bundle can include multiple language-specific resource directories. The names of these directories consist of three components: an ISO 639-1 language code, an optional ISO 3166-1 region code, and a .lproj suffix. For example, to designate resources localized to English, the bundle would be named en.lproj. By convention, these directories are called lproj directories. Note: You may use ISO 639-2 language codes instead of those defined by ISO 639-1. For more information about language and region codes, see Language and Locale Designations in Internationalization Programming Topics. Each lproj directory contains a localized copy of the applications resource files. When you request the path to a resource file using the NSBundle class or the CFBundleRef opaque type, the path you get back automatically reflects the resource for the users preferred language. For more information about internationalization and how you support localized content in your Mac OS X applications, see Internationalization Programming Topics.
84
CHAPTER 7
As you develop your application and your project code stabilizes, you can begin performance tuning. Of course, you want your application to launch and respond to the users commands as quickly as possible. A responsive application fits easily into the users workflow and feels well crafted.
85
CHAPTER 7
Frameworks not used directly by your applicationAvoid calling code that uses nonessential frameworks until after launch.
86
CHAPTER 7
Nib files whose contents are not displayed immediatelyMake sure your nib files and awakeFromNib: code are not doing too much at launch time. See Simplify Your Main Nib File (page 86) for more information. User preference filesUser preferences may not be local so read them later if you can. Font filesConsider delaying font initialization until after the application has launched. Network filesAvoid reading files located on the network if at all possible.
If you must read a file at launch time, do so only once. If you need multiple pieces of data from the same file, such as from a preferences file, consider reading all of the data once rather than accessing the file multiple times.
Compiler-Level Optimizations
The Xcode compiler supports optimization options that let you choose whether you prefer a smaller binary size, faster code, or faster build times. For new projects, Xcode automatically disables optimizations for the debug build configuration and selects the Fastest, Smallest option for the release build configuration. Code optimizations of any kind result in slower build times because of the extra work involved in the optimization process. If your code is changing, as it does during the development cycle, you do not want optimizations enabled. As you near the end of your development cycle, though, the release build configuration can give you an indication of the size of your finished product, so the Fastest, Smallest option is appropriate.
87
CHAPTER 7
Table 7-1 lists the optimization levels available in Xcode. When you select one of these options, Xcode passes the appropriate flags to the compiler for the given group or files. These options are available at the target level or as part of a build configuration. See the Xcode Build System Guide for information on working with build settings for your project. Table 7-1 Compiler optimization options Description The compiler does not attempt to optimize code. Use this option during development when you are focused on solving logic errors and need a fast compile time. Do not use this option for shipping your executable. The compiler performs simple optimizations to boost code performance while minimizing the impact to compile time. This option also uses more memory during compilation. The compiler performs nearly all supported optimizations that do not require a space-time tradeoff. The compiler does not perform loop unrolling or function inlining with this option. This option increases both compilation time and the performance of generated code. The compiler performs all optimizations in an attempt to improve the speed of the generated code. This option can increase the size of generated code as the compiler performs aggressive inlining of functions. This option is generally not recommended. Fastest, Smallest The compiler performs all optimizations that do not typically increase code size. This is the preferred option for shipping code because it gives your executable a smaller memory footprint. As with any performance enhancement, do not make assumptions about which option will give you the best results. You should always measure the results of each optimization you try. For example, the Fastest option might generate extremely fast code for a particular module, but it usually does so at the expense of executable size. Any speed advantages you gain from the code generation are easily lost if the code needs to be paged in from disk at runtime.
Fast
Faster
Fastest
88
CHAPTER 7
89
CHAPTER 7
90
REVISION HISTORY
This table describes the changes to Mac OS X Application Programming Guide. Date 2011-06-27 Notes New document describing the development process for Mac OS X applications.
91
2011-06-27 | 2011 Apple Inc. All Rights Reserved.
REVISION HISTORY
92
2011-06-27 | 2011 Apple Inc. All Rights Reserved.