iOS Address Book

 

 

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

 

iOS Address Book_第1张图片 

Screenshot   系统自带的通讯录选择界面

 

// .........SEPERATED LINE......

//------------------------------------------------------------------------------------------------------------------------

二。手动处理通讯录(API操作)

 

同样需要先导包AddressBook

import AddressBook

 

但需要注意的是,通过API获取通讯录需要用户授权,可以通过以下代码判断是否拥有权限:

iOS Address Book_第2张图片

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

 

 

 

 

你可能感兴趣的:(ios,swift,oc,Address,book)