iOS_获取通讯录

本文介绍了一种在iOS设备上获取用户通讯录的方法。通过Objective-C代码实现,包括读取联系人的姓名、电话号码、地址及电子邮件等信息,并将这些信息组织成易于使用的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实获取通讯录的代码都是死得, 千篇一律,  并没有什么花儿, 网上一搜一大把, 我写出来的意义其实仅仅是记录一下

- (void) getAllContactsInfo {
    NSMutableArray *allContactsInfo = [[NSMutableArray alloc] initWithCapacity:1];
    
    // get all contacts info and upload them:
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    NSInteger numberOfPeople = [allPeople count];
    
    NSLog(@"%@",allPeople);
    for (NSInteger i = 0; i < numberOfPeople; i++) {
        NSMutableDictionary *contactInfo = [[NSMutableDictionary alloc] initWithCapacity:1];
        
        ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
        
        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);
        [contactInfo setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];
        
        NSMutableArray *allPhoneNumbers = [[NSMutableArray alloc] initWithCapacity:1];
        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
            NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
            NSLog(@"  phone:%@", phoneNumber);
            [allPhoneNumbers addObject:phoneNumber];
        }
        [contactInfo setObject:allPhoneNumbers forKey:@"phone_numbers"];
        
        NSMutableArray *allAddresses = [[NSMutableArray alloc] init];
        ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
        CFIndex numberOfAddresses = ABMultiValueGetCount(addresses);
        for (CFIndex i = 0; i < numberOfAddresses; i ++) {
            NSString *address = CFBridgingRelease(ABMultiValueCopyValueAtIndex(addresses, i));
            NSLog(@"  address:%@", address);
            [allAddresses addObject:address];
        }
        [contactInfo setObject:allAddresses forKey:@"addresses"];
        
        NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:1];
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        CFIndex numberOfEmails = ABMultiValueGetCount(emails);
        for (CFIndex i = 0; i < numberOfEmails; i ++) {
            NSString *email = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, i));
            NSLog(@"  email:%@", email);
            [allEmails addObject:email];
        }
        [contactInfo setObject:allEmails forKey:@"emails"];
        
        CFRelease(phoneNumbers);
        CFRelease(addresses);
        CFRelease(emails);
        
        NSLog(@"=============================================");
        
        [allContactsInfo addObject:contactInfo];
    }
    
    NSLog(@"All contacts: %@", allContactsInfo);
}

以上便是简单地获取通讯录, 并且格式也都写好有了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值