Meet User Expectations
OS X incorporates the latest technologies for creating great apps. But what makes your app great is not the sophisticated technologies it uses but how well the app helps its users accomplish their goals. Users will ignore an app that gets in their way but will keep using one that makes working with it (or playing with it) an enjoyable and productive experience. This article introduces the common features of a great OS X app and the key technologies of OS X that help you to create great apps.
OS X 整合了各种创建伟大应用程序的最新科技。但是让你的应用程序变得优秀的不是它使用的尖端科技,而是应用如何完美的帮助用户完成他们的目标。 用户会忽视那些特立独行的应用,而始终选择使用那个跟它一起工作(或一起玩)是一件很愉快很有创造力的体验的应用。 本文介绍了伟大OS X应用程序里的一些通用功能,以及OS X 的一些关键科技,它们能帮助你创建各种伟大的应用程序。
Note: This article frequently refers to Cocoa. Cocoa is the application environment for creating OS X apps. It gives you access to all of the features of the operating system and enables you to integrate your app with the rest of the system.
注意:本文经常提及Cocoa. Cocoa 是创建OS X应用程序的应用环境。 它能让你访问操作系统的所有功能,能让你的应用程序跟其余的系统互相整合。
Design Your App for Ease of Use
设计你的应用程序,使其简单易用
As you design your app, you want to think about the tasks that your users will perform with it and then find ways to make those tasks easier to accomplish. The best apps have certain features that are based on time-tested design principles. Some of these features use technologies that were recently added to the operating system. For example, great OS X apps:
当你设计你的应用程序时,你想要仔细考虑你的用户们将会用它执行的各种任务,然后查找方法让这些任务能够更加简单的完成。最好的应用程序都有一些基于时间测试原则的特定功能。 其中一些使用了最近才被添加到操作系统的科技。比如,伟大的OS X 应用程序:
-
Save user data without user interaction. Users should not have to save their work manually. If you base your app on the document model, Cocoa does much of this work for you.
自动存储用户数据。 不应该让用户手动保存它们的工作。 如果你的应用程序基于文档模型,Cocoa会为你做很多工作。
-
Restore users’ work environment at login. Cocoa helps you archive the current state of your app’s interface (including the state of unsaved documents) and restores that state at launch time.
登陆时恢复用户的工作环境。 Cocoa 帮助你存档你的应用程序界面的当前状态(包括未保存的文档状态),并在重新载入时恢复这些状态。
-
Terminate automatically. A user should never have to quit your app explicitly. The automatic termination feature moves an app to the background (so it appears to have been quit) when the user closes the app’s windows. Subsequent launches are nearly instantaneous because the app simply moves back to the foreground. When there is memory pressure, the system may kill an app’s process even though the app is running.
自动终止。 绝对不应该让用户不得不明确地退出你的应用。 当用户关闭应用程序窗口之后,自动终止功能把应用程序转移到后台(因此看起来就是已经被退出)。下次载入应该几乎是立即的,因为应用程序只是简单的从后台转移到前台。 当有内存压力时,系统可能杀死一个应用程序进程即使该应用还在运行。
-
Save your documents in iCloud. If you enable iCloud in your app, when users create and edit documents all their changes are automatically pushed to all their devices—Mac, iPhone, iPad, or iPod touch—on which your app is installed. The document architecture of the AppKit framework helps you to do this.
把你的文档保存到iCloud. 如果你的应用程序中能用iCloud, 当用户创建并编辑文档时,他们做的所有的改变将自动推送到他们的所有设备上(只要安装了你的应用程序)---Mac, iPad, 或 iPod touch。
-
Make your app accessible. Your app should have an intuitive interface that enables users with disabilities to accomplish tasks quickly and efficiently. The frameworks provide support for assistive applications.
让你的应用程序可访问。 你的应用程序应该有一个直观(intuitive)界面能让残疾人用户 快速并有效的完成任务。苹果框架提供了对残疾人应用的支持。
-
Respond to trackpad gestures. Gestures in your app provide shortcuts for common tasks and can supplement existing controls and menu commands. OS X supports gestures through the normal event-handling mechanism.
响应触控板手势。 应用程序里的手势提供了常见任务的快捷方式,可以补充现有的控件和菜单命令。 OS X 通过正常的事件处理机制支持各种手势。
-
Offer a full-screen version of the app. A full-screen experience eliminates outside distractions and allows users to focus on an app’s content
提供一个应用程序的满屏版本。 一个满屏体验消除外部干扰,并允许用户只专注于应用程序的内容。
-
Let users view documents from outside the app. If your app supports custom documents, offer this extra benefit to users by using a Quick Look plug-in.
让用户在应用外部查看文档。 如果你的应用程序支持自定义文档,使用一个快速查看插件(Quick Look plug-in)给用户提供这一额外好处。
-
Support user-friendly features. Users of an app should always be in control, receive consistent feedback, and be encouraged to explore. Thus an app should implement direct manipulation, drag-and-drop, and reversible actions (undo).
支持用户友好功能。 应用程序的用户应该始终能控制并获得一致的反馈, 并鼓励他们去探索。 因此应用程序应该实现直接操作,拖放,以及可逆转动作(取消).
For your app you might consider minimizing or eliminating the user’s direct interactions with the file system. Some apps, sometimes called “shoebox” apps, give users a great experience by presenting documents or other user data in a simplified browser. iPhoto is an example of such an app. It lets users deal with thumbnail images of their photos rather than the cryptic file names generated by cameras.
你的应用程序也许考虑最小化或取消用户与文件系统的直接交互。 一些应用程序,有时被称为”鞋盒“应用,通过在一个简化浏览器里呈现文档或其它用户数据来给用户一个好的体验。 iPhoto就是这样的一个例子。 它让用户处理他们照片的缩略图,而不是由照相机所产生的神秘文件名。
Because Cocoa supports all of the preceding features, you can incorporate them with relatively little effort. You can find more information about many of these tasks and design principles in Mac App Programming Guide.
因为Cocoa支持所有前面所提到的功能, 你用相对较少的努力就可以结合它们。 你可以在 Mac App Programming Guide里找多更多信息,关于很多这样的任务以及各种设计原则。
Take Advantage of a Sophisticated Graphics Environment
利用一个精致的图形环境
High-quality graphics and animation make your app look great and can convey a lot of information to the user. Animations are a great way to give feedback about changes to your user interface; for example, the “genie effect” shows where minimized windows go. Both the AppKit and Core Animation frameworks have facilities for quickly creating sophisticated animations. Also include high-resolution versions of your art and graphics. OS X automatically loads high-resolution image resources when an app runs on a screen whose scaling factor is greater than 1.0. Including such image resources makes your app’s graphics look even sharper and crisper on screens with higher pixel counts.
高质量的图形和动画让你的应用程序看起来很出众。动画是过度到你的用户界面的一个很好途径;比如, "genie effect--精灵效果"显示了窗口最小化到哪里。AppKit 和 Core 动画框架都有快速创建复杂动画的功能。 同时还包含了高分辨率的艺术和图形。当应用程序运行在一个缩放因子大于1.0的屏幕上时, OS X 自动加载高分辨率的图片资源。包含这样的图片资源让应用程序里的图像在屏幕上看起来更加锐利更加清晰,因为它们拥有更高的像素数。
OS X Technology Overview contains information about the graphics technologies available in OS X.
OS X Technology Overview(OS X 技术概述) 包含了在OS X里能用的各种图像技术信息。
Take Advantage of the Runtime Environment
利用运行时环境
When you are ready to begin writing actual code, OS X gives you a lot of technologies that make your job as an app developer easier. Your app gets some of these features for free, with no coding required; for others, you need to write code. The following discussion covers several of the latter technologies.
当你准备好编写实际代码时,OS X 给你提供了很多技术,它们让你的开发工作变得更加简单。 你的应用程序能免费获得其中一部分这些功能,其中一些不需要编码的功能;而其它的则需要编写代码。 以下讨论覆盖了几项需要编码的技术。
Concurrency and Threading
并发和线程
Each process in OS X starts off with a single thread of execution—the main thread—but it can create more threads as needed. You create secondary threads to perform work concurrently and asynchronously on them, thereby improving overall application performance. For most types of work, the best way to create threads (indirectly) is to use Grand Central Dispatch (GCD) or operation objects, a Cocoa concurrency technology implemented by the NSOperation
class.
OS X的每个进程都以执行单个线程启动---主线程---但是如有必要可以创建多个线程。你创建辅助线程让工作同步和异步在它们上执行,从而提高整体应用程序的性能。 对于大多数类型的工作,(间接)创建线程的最好方法是使用 Grand Central Dispatch(GCD) 或 操作对象(由NSOperation类实现的Cocoa 同步技术)。
GCD and operation objects simplify or eliminate many of the problems normally associated with threaded programming, such as synchronization and locking. They define an asynchronous programming model in which you specify only the work to be performed and the order in which you want it performed. The system handles the tedious work required to schedule the necessary threads and execute your tasks as efficiently as possible on the current hardware.
GCD 和 操作对象 简化或消除多个正常跟线程有关编程的问题,比如同步和锁定。它们定义了一个异步编程模型,在那里只能执行你指定的工作,并指定执行顺序。 系统处理被要求用来安排必要线程的各种乏味的工作,并让你的任务在当前硬件上尽可能有效的执行。
Concurrency Programming Guide describes how to use GCD and operation objects to implement concurrency in your apps.
同步编程指导 描述了如何在应用程序里使用GCD 和 操作对象来实现同步。
The File System
OS X structures the file system to give users a great experience. For example, rather than exposing the entire file system to the user, the Finder and the system’s open and save panels hide any files and directories that a typical user should not need to use, such as the contents of low-level UNIX directories. Hiding unneeded directories results in a simpler interface for end users. Apps can still access any files and directories for which they have valid permissions, regardless of whether they are hidden by the Finder.
OS X 的文件系统给用户一个很好的用户体验。 比如,Finder 和 系统的 打开及保存面板隐藏了一个典型的用户应用不需要用到的文件和文件夹(比如,低层UNIX文件夹的内容),而不是把整个文件系统暴露给用户。隐藏不需要的文件夹意味着给最终用户提供一个更简单的界面。 只要它们有访问权限,应用程序任然可以访问任何文件和文件夹,不管它们是否被Finder隐藏。
The HFS+ file system of OS X is case insensitive but also case preserving. Therefore, when specifying filenames and directories in your code, always assume case sensitivity. Your app can always retrieve and sometimes manage file attributes and file permissions. By default, any files your app creates are owned by the current user and given appropriate permissions.
OS X 的 HFS+ 文件系统是不区分大小写的,但是也保留大小写。 因此,当你在代码里指定文件名和文件夹名时,请永远确保区分大小写。 你的应用程序总是能取回,有时候还能管理文件属性和文件权限。默认情况下,任何在你应用程序里创建的文件都归当前用户所有,并给于适当的权限。
File System Programming Guide is the repository of definitive information on file attributes, file permissions, standard system directories, and anything else related to the OS X file system.
文件系统编程指南 是描述文件属性,文件全歼,标准系统文件夹以及任何其它任何跟OS X 文件系统相关信息的权威信息库。
A Few Important System Directories
一些重要的系统目录
When writing and reading files of various types, you should understand and follow the conventions of the OS X file system, which expects related files and data in specific places. Knowing where to put files and how to get information out of the file system ensures a better user experience. And it is important if you are distributing your app through the Mac App Store, because the Store expects to find your app’s data files in specific directories.
当你编写或阅读各种类型的文件时,你应该理解并遵循OS X 文件系统的各种约定,它们在指定地方要求相关的文件和数据。 了解把文件放在哪里以及如何在文件系统外获取信息能确保一个更好的用户体验。并且,对于想通过Mac App Store 发布的应用程序很重要,因为商店要求能在指定目录里找到你的应用程序数据文件。
Table 1 lists the directories with which apps commonly interact. Note that home directories (which you might see preceded with a tilde (~
) character in places) can have a standard directory of the same name as a system (non-user) directory.
表格1 列出了应用程序经常与之交互的各个目录。 注意,home目录(你能看到以~字符开头的目录)可以一个跟系统(没有用户)目录同名的标准目录。
目录 |
Description(描述) |
---|---|
|
Is the installation directory for applications, whether global 应用程序的安装目录,可能就是全局 /Applications文件夹 或在用户的主目录下。要想访问应用程序包里的资源,使用一个NSBundle对象。 |
Home directory |
Contains the logged-in user’s files. It is the app’s container directory if the app is running in a sandbox (described in “Security,” below). Otherwise, it a user-specific subdirectory of 包含了登录用户的各种文件。 如果应用程序在一个沙盒里运行,则主目录是应用程序的容器目录(在下面的"Security"里有描述)。此外,它是一个用户指定的/Users的子目录。 |
|
Stores private app-related data and preferences. There are 存储应用程序相关的似有数据和偏好设置。 在顶层以及用户的主目录里面有一个Library目录。 始终使用在当前主目录下的那个Library目录。 在OS X 的新版本中,Fider默认隐藏用户主目录下的Library目录。因此,你绝不应该把你想让用户访问的数据存储在这个目录中。 |
|
Is a subdirectory of 这是Library目录的子目录,用来存储支持应用程序但是运行时不需要的的任何文件,比如文档模板或配置文件。这些文件不能包含用户数据,并存储在由应用程序或公司命名的子目录里。 |
|
Stores cache files and other temporary data that your app can re-create as needed. This directory is located inside the 存储缓存文件和其他应用程序需要时能重新创建的临时数据。该目录在Library目录里。把文件保存在Caches目录下的子目录里,子目录名由应用程序或公司命名。当它们不再被需要时,应用程序负责清除这些数据文件。 |
Temporary directory |
Stores files that do not need to persist between launches of your app (for example, scratch files). This directory is typically hidden from the user. 存储那些在应用程序载入时不需要保留的文件(比如,临时文件)。 该目录通常对用户隐藏。 |
Because the actual paths to standard file-system directories can differ based on certain conditions, you should use theURLsForDirectory:inDomains:
method of the NSFileManager
class to retrieve the actual directory path. You can then add any custom directory and filename information to the returned URL object to complete the path. Construct paths using the methods of the NSURL
class.
因为访问标准文件系统目录的实际路径基于特定的情况而不同,你应该使用 NSFileManager 类的 URLsForDirectory:inDomains:方法来重置 实际目录路径。然后你可以在返回的URL对象后添加任何自定义的目录或文件名信息来完成整个路径。 构造路径使用NSURL 类的各种方法。
Coordinating File Access with Other Processes
协调与其它进程的文件访问
In OS X, other processes might access the same files that your app does. Therefore, when working with files, you should use the file coordination API so you can be notified when other processes (including the Finder) attempt to read or modify files your app is currently using.
在OS X,其它进程可能跟你的应用程序一样也访问相同的文件。 因此,当跟文件一起工作时,你应该使用文件协调API, 这样当别的进程尝试读或修改应用程序正在使用的文件时能及时给你发送通知。
Coordinating file access is critical when your app adopts iCloud storage, because multiple apps can access your document files in iCloud. The simplest way to incorporate file coordination into your app is to use the NSDocument
class, which handles all of the file-related management for you. On the other hand, if you're writing a library-style (or “shoebox”) app, you must use the file coordination interfaces directly.
当应用程序采用iCloud存储时,协调文件访问很关键,因为多个应用程序都可以访问在iCloud里的文档文件。把文件协调整合进你的应用程序的最简单方法是使用 NSDocument 类,该类能帮你处理所有文件相关的管理。另一方面,如果你正编写一个库风格(或 鞋盒)的应用程序,你必须直接使用文件协调接口。
Document-Based App Programming Guide for Mac describes how to override the NSDocument
class and File System Programming Guide contains information on directly calling the file coordination methods.
《Mac基于文档的应用程序编程指南》 描述了如果重载NSDocument 类 ,另外, 《文件系统编程指南》 包含了直接调用文件协调方法的各种信息。
Security
The security technologies in OS X help you safeguard sensitive data created or managed by your app. They also help minimize damage caused by attacks from hostile code. These technologies impact how your app interacts with system resources and the file system.
OS X 的安全技术帮助你保护应用程序创建或管理的各种敏感数据。 它们也帮助你把恶意代码攻击造成的损害最小化。这些技术对你的应用程序如何跟系统资源以及文件系统相交互产生影响。
App Sandbox and XPC
App Sandbox defends against stolen, corrupted, or deleted user data if malicious code exploits your app. App Sandbox also minimizes the damage from coding errors. Its strategy is twofold:
如果恶意代码利用你的应用程序,应用程序沙盒能抵御被盗,损坏,或删除用户信息。 应用程序沙盒还把代码错误造成的损坏最小化。它有2种策略:
-
App Sandbox enables you to describe how your app interacts with the system. The system then grants your app the access it needs to get its job done, and no more. For your app to provide the highest level of damage containment, the best practice is to request as few entitlements as possible.
应用程序沙盒能让你对应用程序如何跟系统交互做出描述。 然后系统授予应用程序访问权限,这些权限仅限于用来完成它的工作。 为了让应用程序提供最高水平的损坏控制,最好的方法就是要求尽可能少配额。
-
App Sandbox allows the user to transparently grant your app additional access by way of Open and Save dialogs, drag and drop, and other familiar user interactions.
应用程序沙盒 允许用户通过打开和保存对话框,拖放以及其它熟悉的用户交互,透明地授予应用程序额外的访问权限。
Some app operations are more likely to be targets of malicious exploitation—for example if they parse data received over a network or decode video frames. XPC is an interprocess communication technology that complements App Sandbox by enabling privilege separation. By using XPC, you can improve the effectiveness of the damage containment offered by App Sandbox by separating such potentially dangerous activities into their own address spaces. Privilege separation, in turn, is a development strategy in which you divide an app into pieces according to the system resource access that each piece needs. The component pieces that you create are called XPC services.
一些应用程序的操作更容易成为恶意利用的目标--比如它们分析从网络或解码视屏帧获取的数据。XPC 是一个进程间通信技术,它通过使特权分离来实现应用程序沙盒。通过使用XPC, 你可以增进由应用程序提供的容错(damage containment)效果,它通过分离这些潜在危险活动到它们自己的地址空间。反过来说,特权分离是一个开发策略,在那里你根据每一块需要访问的系统资源来把应用程序分为几块。 你创建的块组件(component pieces)被称为XPC 服务。
App Sandbox Design Guide gives a complete explanation of App Sandbox and how to use it. Daemons and Services Programming Guide provides details on adopting XPC.
《应用程序沙盒设计指南》给出了应用程序沙盒的完整解释以及如何使用它。 《精灵和服务编程指南》提供了采用XPC的详情。
Code Signing
代码签名
OS X employs the security technology known as code signing to allow you to certify that your app was indeed created by you. After an app is code signed, the system can detect any change to the app—whether the change is introduced accidentally or by malicious code. Various security technologies, including App Sandbox and parental controls, depend on code signing.
OS X 雇佣了安全技术(即代码签名)来让你证明应用程序确实是由你创建的。 当应用程序完成代码签名之后,系统就能侦查应用程序的任何改变---改变是由意外引起的还是由恶意代码引起的? 多种安全技术,包括应用程序沙盒以及来源控制(parental controls)依赖于代码签名。
In Xcode you can typically rely on automatic code signing, which requires only that you specify a code signing identity in the build settings for your project. Tools Workflow Guide for Mac describes how to do this.
在Xcode 里, 你通常可以信赖自动代码签名,那里只在项目的 build settings里才要求你指定一个代码签名认证。 《Mac 工具工作流指南》描述了如何使用。
The Keychain
A keychain is a secure, encrypted container for storing a user’s passwords and other secrets. It is designed to help a user manage their multiple logins, each with its own ID and password. You should always use a keychain to store sensitive credentials for your app. Keychain Services Programming Guide describes keychains in detail.