在iOS里面调用通讯录主要有以下两种方式:
一。使用系统自带的通讯录界面
首先需要导入AddressBookUI包
import AddressBookUI
以下是具体代码实现
let addressController:ABPeoplePickerNavigationController = ABPeoplePickerNavigationController(); // 实现ABPeoplePickerNavigationControllerDelegate协议 // addressController.peoplePickerDelegate = self; self.presentViewController(addressController, animated: true, completion: nil);
实现 ABPeoplePickerNavigationControllerDelegate协议,处理选择后的事件
// MARK: Implements ABPeoplePickerNavigationControllerDelegate func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) { //person. self.dismissViewControllerAnimated(true, completion: nil) NSLog("%@", person.debugDescription!) // var pid:ABRecordID? = ABRecordGetRecordID(person); var st:Unmanaged<CFString> = ABRecordCopyCompositeName(person); var ss:CFString = st.takeRetainedValue(); NSLog("%@", ss as String); var phones:ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue(); let countOfPhones = ABMultiValueGetCount(phones); for index in 0..<countOfPhones { let phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as NSString; NSLog("phone:%@", phone); } //NSString. }
Screenshot 系统自带的通讯录选择界面
// .........SEPERATED LINE......
//------------------------------------------------------------------------------------------------------------------------
二。手动处理通讯录(API操作)
同样需要先导包AddressBook
import AddressBook
但需要注意的是,通过API获取通讯录需要用户授权,可以通过以下代码判断是否拥有权限:
switch ABAddressBookGetAuthorizationStatus() {
case .Authorized:
println("Already authorized");
case .Denied:
println("You are denied access to address book");
case .NotDetermined:
createAddressBook()
if let theBook: ABAddressBookRef = addressBook{
ABAddressBookRequestAccessWithCompletion(theBook,
{(granted: Bool, error: CFError!) in
if granted{
println("Access is granted")
}else{
println("Access is not granted")
}
})
}
case .Restricted: println("Access is restricted")
default:
println("Unhandled");
}
在获取权限后,就可以获取通讯录的数据了
let address: ABAddressBook = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue(); let allPeople = ABAddressBookCopyArrayOfAllPeople(address).takeRetainedValue() as NSArray; for person : ABRecordRef in allPeople { let name = ABRecordCopyCompositeName(person).takeRetainedValue() as NSString; println(name); }
相关推荐
【Address Book Putting People in Your App】[PDF] [iPhone/iPad/iOS]
ABUIGroups shows how to check and request access to a user’s address book database. It also demonstrates how to retrieve, add, and remove group records from the address book database using ...
Address Book UI 框架 16 Event Kit UI 框架 16 Game Kit 框架 17 iAd 框架 17 Map Kit 框架 17 Message UI 框架 17 UIKit 框架 18 媒体层 19 图形技术 19 音频技术 19 视频技术 20 媒体层包含的框架 21 资产库框架 ...
When details of iOS 6 were first announced at the Apple World Wide Development Conference in June, 2012 it seemed, on the surface at least, that the...book is intended to address both category of reader.
Contact-FrameWork iOS9 新特性Contact FrameWork 替代9之前Address Book FrameWork
World-renowned iOS expert Erica Sadun covers device-specific development, document/data sharing, Core Text, networking, image processing, onboard cameras, audio, Address Book, Core Location, GameKit,...
• Working with the address book • Consuming RESTful web services • Blocks (closures) in Objective-C • Building advanced location-based applications • Developing database applications using the ...
C Runtime features Play audio and video files and access the iPod library Retrieve contacts and groups from the Address Book Determine camera availability and access the Photo Library Create ...
10. Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 10.1 Getting a Reference to Address Book 550 10.2 Retrieving All ...
Access user libraries: music, photos, address book, and calendar Examine additional topics including files, threading, and networking Paperback: 930 pages Publisher: O'Reilly Media; Fourth Edition ...
Retrieve contacts and groups from the Address Book Determine camera availability and access the Photo Library Create multitasking-aware apps Maintain persistent storage in your apps Use Event Kit to ...
从address book中检索联系人和群组 确定相机的可用性和访问photo库 创建多任务感知的应用 使用event kit来管理日程表、日期和事件 运用加速计和陀螺仪 使用icloud服务来增强你的应用 vandad ...
第5章 Address Book框架初步 93 第6章 Music Libraries框架 109 第7章 实现HealthKit框架 125 第8章 实现HomeKit框架 139 第9章 JSON的使用和解析 155 第10章 通知机制 165 第11章 基于CloudKit的云存储 183 第12章 ...
to make use of the Media Player and Address Book. Chapter 13, Distributing Your Application This chapter talks about how to add some final polish to your application and walks you through the process ...
In addition, you’ll learn how to address the specific features made available through Apple's iOS, especially with regard to designing Web-based touch-screen interfaces. Pro iOS Web Design and ...
本书章节内容如下: 1. Just Enough C 2. Object-Based Programming...31. Address Book 32. Calendar 33. Mail 34. Maps 35. Sensors 36. Persistent Storage 37. Basic Networking 38. Threads 39. Undo 40. Epilogue
Getting Started with Address Book Chapter 6. Working with Music Libraries Chapter 7. Implementing HealthKit Chapter 8. Implementing HomeKit Chapter 9. Working with and Parsing JSON Chapter 10. ...
This is the eBook version of the printed book. Programming in Objective-C, Fourth Edition Updated for iOS 5 and ARC Programming in Objective-C is a concise, ...B Address Book Example Source Code
官方demo程序采用的是 代码如下: // Write the new address book back to disk. fstream output(argv[1], ios::out | ios::trunc | ios::binary); if (!address_book.SerializeToOstream(&output)) { cerr &...