iOS 地址簿编程指南(Address Book Programming Guide for iOS)
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
- 概述(Introduction)
iOS 的地址簿技术提供了一种方式用于存储人们的联系人信息和其它个人信息在一个中心数据库中,并在应用之间共享该信息。该技术由几部分组成:
地址簿框架提供对联系人信息的访问。
地址簿用户界面框架提供用户界面用于显示信息。
地址簿数据库用于存储信息。
联系人应用为用户提供一种访问他们的联系信息的方式。
本文档涵盖了地址簿技术的关键概念,而且说明了可执行的基本操作。当你添加该技术到你的应用时,用户就能在你的应用中使用他们在其它应用诸如邮件和文本中使用的联系信息。该文档告诉你如何来做如下的事情:要想有效地使用本文档,你应该已经理解了导航控制器和视图控制器,并且理解代理和协议。
- 访问用户的地址簿数据库
- 提示用户联系信息
- 显示联系信息给用户
- 更改用户的地址簿数据库
注意:在 Mac OS X 上使用过地址簿技术的开发者应该了解该技术的编程接口与 iOS 上不同。The Address Book technology for iOS provides a way to store people’s contact information and other personal information in a centralized database, and to share this information between applications. The technology has several parts:
The Address Book framework provides access to the contact information.
The Address Book UI framework provides the user interface to display the information.
The Address Book database stores the information.
The Contacts application provides a way for users to access their contact information.
This document covers the key concepts of the Address Book technology and explains the basic operations you can perform. When you add this technology to your application, users will be able to use the contact information that they use in other applications, such as Mail and Text, in your application. This document tells you how to do the following:
- Access the user’s Address Book database
- Prompt the user for contact information
- Display contact information to the user
- Make changes to the user’s Address Book database
To get the most out of this document, you should already understand navigation controllers and view controllers, and understand delegation and protocols.
Note: Developers who have used the Address Book technology on Mac OS X should be aware that the programming interface for this technology is different on iOS.
本文档的组织结构(Organization of This Document)
本文档包含如下章节:
- 通过向你展示一个使用地址簿技术的简单应用使你可以起步开始学习。
- 描述如何创建一个地址簿对象,如何创建联系人和分组记录以及如何获取和设置属性。
- 描述如何使用地址簿用户界面框架提供的视图显示联系方式,让用户选择联系方式,创建一条新的联系方式和编辑联系方式。
- 描述了你的应用可以直接读写联系信息的方式。
This document contains the following chapters:
-
“Quick Start Tutorial”
gets you up and running by showing you how to create a simple application that uses the Address Book technology. -
“Building Blocks: Working with Records and Properties”
describes how to create an address book object, how to create person and group records, and how to get and set properties. -
“User Interaction: Prompting for and Displaying Data”
describes how to use the views provided by the Address Book UI framework to display a contact, let the user select a contact, create a new contact, and edit a contact. -
“Direct Interaction: Programmatically Accessing the Database”
describes the ways your application can read and write contact information directly.
参见(See Also)
下面的文档讨论了你应该理解的一些基本概念,以便充分利用本文档:
下面的文档包含有关地址簿框架的额外信息:
- 引导新接触 iOS 平台的开发者浏览可用的技术,以及如何使用这些技术构建应用。这篇文档包括窗口、视图和视图控制器的相关讨论。
界面构建器用户指南
说明了如何使用界面构建器创建应用。它包括一个应用的用户界面相关的讨论,以及从界面到代码建立连接。( Xcode 4 User Guide 的 Edit User Interfaces 部分取代了该文档 )- 和 Objective-C 编程语言 讨论了许多你所需要的编写应用的基本概念。它包括代理和协议相关的讨论。
- 描述了与地址簿数据库中的记录直接交互的 API。
- 描述了便于显示、编辑、选择和创建地址簿数据库中记录的控制器,以及这些控制器的代理协议。
The following documents discuss some of the fundamental concepts you should understand in order to get the most out of this document:
-
iOS App Programming Guide
guides developers who are new to the iOS platform through the available technologies and how to use them to build applications. It includes relevant discussion of windows, views, and view controllers. -
Interface Builder User Guide
explains how to use Interface Builder to create applications. It includes relevant discussion of the user interface for an application and making connections from the interface to the code. -
Cocoa Fundamentals Guide
and The Objective-C Programming Language discuss many basic concepts you will need to write any application. It includes relevant discussion of delegation and protocols.
The following documents contain additional information about the Address Book frameworks:
-
Address Book Framework Reference for iOS
describes the API for direct interaction with records in the Address Book database. -
Address Book UI Framework Reference for iOS
describes the controllers that facilitate displaying, editing, selecting, and creating records in the Address Book database, and their delegate protocols.
- 快速起步教程(Quick Start Tutorial)
在本教程中,你将构建一个简单的应用,该应用提示用户从他/她的联系人列表中选择一个联系人,并呈现选择的联系人的名字和电话号码。
In this tutorial, you will build a simple application that prompts the user to choose a person from his or her contacts list and then shows the chosen person’s first name and phone number.
- 创建并配置项目(Create and Configure the Project)
1、在 Xcode 中,从 Single View Application 模板创建一个新的项目。
2、链接地址簿用户界面和地址簿框架到你的项目。
提示:如果你没有链接这两个框架,项目就会构建失败(出现一个链接错误)。
In Xcode, create a new project from the Single View Application template.
Link
the Address Book UI and Address Book frameworks to your project.
Important: The project will fail to build (with a linker error) if you do not link against both of these framework.
- 创建用户界面和头文件(Create the UI and Header File)
While you are creating the user interface, you will take advantage of Xcode’s ability to declare the necessary actions and properties, creating the majority of the header file for you.
Open the main storyboard file (
MainStoryboard.storyboard
).- a button and two labels to the view by dragging them in from the library panel. Arrange them as shown in Figure 1-1.
Open the assistant editor.
- the button to a new action method called
showPicker:
on the view controller. -
This sets the target and action of the button in the storyboard, adds a declaration of the method to the header file, and adds a stub implementation of the method to the implementation file. You will fill in the stub implementation later.
- the two labels to two new properties called
firstName
andphoneNumber
of the view controller.
This creates a connection in the storyboard, adds a declaration of the properties to the header file, and adds the@synthesize
line for the properties in the implementation file. Laying out the interface At this pointViewController.h
, the header file for the view controller, is almost finished. At the end of the@interface
line, declare that the view controller class adopts theABPeoplePickerNavigationControllerDelegate
protocol by adding the following:<ABPeoplePickerNavigationControllerDelegate>
Listing 1-1 shows the finished header file. The finished header file
#import <UIKit/UIKit.h> #import <AddressBookUI/AddressBookUI.h> @interface ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UILabel *firstName; @property (weak, nonatomic) IBOutlet UILabel *phoneNumber; - (IBAction)showPicker:(id)sender; @end
- 编写实现部分(Write the Implementation)
- 构建和运行应用(Build and Run the Application)
- 构建块:使用记录和属性(Building Blocks:Working with Records and Properties)
- 地址簿(Address Books)
- 记录(Records)
- 联系人记录(Person Records)
- 分组记录(Group Records)
- 属性(Properties)
- 单值属性(Single-Value Properties)
- 多值属性(Multivalue Properties)
- 可变的多值属性(Mutable Multivalue Properties)
- 街道地址(Street Addresses)
- 用户界面:提示并显示数据(User Interaction: Prompting for and Displaying Data)
- 哪些界面可用(What’s Available)
- 提示用户选择联系人记录(Prompting the User to Choose a Person Record)
- 显示并编辑联系人记录(Displaying and Editing a Person Record)
- 提示用户创建新的联系人记录(Prompting the User to Create a New Person Record)
- 提示用户从已有数据创建新的联系人记录(Prompting the User to Create a New Person Record from Existing Data)
- 直接交互:编程访问数据库(Direct Interaction: Programmatically Accessing the Database)
- 使用记录标识(Using Record Identifiers)
- 使用联系人记录(Working with Person Records)
- 使用分组记录(Working with Group Records)
- 文档修订历史(Document Revision History)