IPhone 获取通讯录信息

ios6新增加了隐私授权功能,访问通讯录也需要授权。

ios6的sdk新增加了一个函数ABAddressBookRequestAccessWithCompletion

说明如下:


注意这个函数是在ios6.0和以上版本才有效。其他版本这个函数是个空值。还有如果代码里面使用了这个函数,那么xcode工程里面需要设置ios6 sdk,不然编译会出错。


下面的代码在xcode 4.5和iOS 6 sdk里面通过,ios4.3和以上版本运行正常,其中ios 6上面会弹出一个对话框,让用户选择是否给当前的app授权使用通讯录。(只会弹一次,即使重装app也不会弹第二次了,当然可以在设置里面去修改是否授权)

+ (NSString*) GetAllContacts
{
    NSString* contacts = @"";
    
    ABAddressBookRef addressBook = ABAddressBookCreate();
    
    if (addressBook == nil) {
        return nil;
    }
    
    __block BOOL accessGranted = NO;
    
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        dispatch_release(sema);
    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }
    
    if (!accessGranted) {
        return nil;
    }
    
    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(int i = 0; i < CFArrayGetCount(results); i++)
    {
        ABRecordRef person = CFArrayGetValueAtIndex(results, i);
        
        NSString* ContactName = @"";
        //读取firstname
        NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        if(personName != nil)
            ContactName = [ContactName stringByAppendingFormat:@"%@",personName];
        
        //读取middlename
        NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
        if(middlename != nil)
            ContactName = [ContactName stringByAppendingFormat:@"%@",middlename];
        
        //读取lastname
        NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        if(lastname != nil)
            ContactName = [ContactName stringByAppendingFormat:@"%@",lastname];
        
        NSString* separator = @"#41#";//随便搞个分隔符
        NSString* numbers = @"";
        //读取电话多值
        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (int k = 0; k<ABMultiValueGetCount(phone) ; k++)
        {
            NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);
            
            if (personPhone) { 
                    numbers = [numbers stringByAppendingFormat:@"%@%@", personPhone, separator];
            }
            
        }
        
        if (numbers.length > 0) {
            NSString* contact = [NSString stringWithFormat:@"<Contact><ContactName>%@</ContactName><Phones>%@</Phones><emailaddress></emailaddress></Contact>", ContactName, numbers];
            contacts = [contacts stringByAppendingFormat:@"%@", contact];
        }
    }
    
    CFRelease(results);
    CFRelease(addressBook);
    
    if (contacts.length > 0) {
        return [NSString stringWithFormat:@"<AddressBook>%@</AddressBook>", contacts];
    }
    else {
        return @"";
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值