`
panqili2120
  • 浏览: 88162 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

iOS Address Book

    博客分类:
  • iOS
阅读更多

 

 

在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);
            
        }

 

 

 

 

  • 大小: 172.7 KB
  • 大小: 32.6 KB
分享到:
评论

相关推荐

    【Address Book Putting People in Your App】[PDF] [iPhone/iPad/iOS]

    【Address Book Putting People in Your App】[PDF] [iPhone/iPad/iOS]

    苹果IOS官方Demo-Group

    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 ...

    iOS技术概述

    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 资产库框架 ...

    iPhone iOS6 Development Essentials

    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

    Contact-FrameWork iOS9 新特性Contact FrameWork 替代9之前Address Book FrameWork

    The.Advanced.iOS.6.Developer.Cookbook

    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,...

    iOS 4 高级编程

    • Working with the address book • Consuming RESTful web services • Blocks (closures) in Objective-C • Building advanced location-based applications • Developing database applications using the ...

    iOS 6 Programming Cookbook

    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 ...

    IOS5 Programming Cookbook

    10. Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 10.1 Getting a Reference to Address Book 550 10.2 Retrieving All ...

    Programming iOS 7 4th Edition

    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 ...

    iOS 5 Programming Cookbook Solutions & Examples for iPhone, iPad

    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 ...

    iOS 6 Programming Cookbook.pdf

     从address book中检索联系人和群组  确定相机的可用性和访问photo库  创建多任务感知的应用  使用event kit来管理日程表、日期和事件  运用加速计和陀螺仪  使用icloud服务来增强你的应用  vandad ...

    精通iOS框架(第2版) ([美]Kyle Richter)

    第5章 Address Book框架初步 93 第6章 Music Libraries框架 109 第7章 实现HealthKit框架 125 第8章 实现HomeKit框架 139 第9章 JSON的使用和解析 155 第10章 通知机制 165 第11章 基于CloudKit的云存储 183 第12章 ...

    Learning iOS Programming, 2nd Edition.pdf

    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 ...

    Pro.iOS.Web.Design.and.Development.Dec.2011

     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 ...

    iOS 4 编程(英文版)

    本书章节内容如下: 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

    AW.Mastering.iOS.Frameworks.Beyond.the.Basics.2nd.Edition.0134052498

    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. ...

    Programming in Objective-C 4th Edition Stephen G. Kochan

    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

    基于Protobuf C++ serialize到char*的实现方法分析

    官方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 &...

Global site tag (gtag.js) - Google Analytics