Iphone 使用KabbersonPhoneProperty检索电话号码

Iphone 使用KabbersonPhoneProperty检索电话号码,iphone,ios5,ios6,addressbook,Iphone,Ios5,Ios6,Addressbook,我无法从下面的代码中检索手机号码,我正在尝试很多事情,但无法解决它。请帮帮我 //以下代码用于储存手机号码和电话号码 ABRecordRef newPerson = ABPersonCreate(); ABMultiValueAddValueAndLabel(phone, (__bridge CFTypeRef)(txtMob2.text), kABHomeLabel, NULL); ABRecordSetValue(newPerson, kA

我无法从下面的代码中检索手机号码,我正在尝试很多事情,但无法解决它。请帮帮我

//以下代码用于储存手机号码和电话号码

        ABRecordRef newPerson = ABPersonCreate(); 

        ABMultiValueAddValueAndLabel(phone, (__bridge CFTypeRef)(txtMob2.text), kABHomeLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonPhoneProperty, phone, &error);

//image
        NSData *dataref = UIImagePNGRepresentation(imgProfile.image);
        ABPersonSetImageData(newPerson, (__bridge CFDataRef)(dataref), &error);

        ABAddressBookAddRecord(addressbook, newPerson, &error);
        ABAddressBookSave(addressbook, nil);
        CFRelease(newPerson);
//下面的代码是显示详细信息的代码

 - (void)displayContacts
{
    int i;
    appDelegate.arr_contact = [[NSMutableArray alloc] init];

    ABAddressBookRef contactBook =ABAddressBookCreateWithOptions(nil, NULL);
    NSArray *allData = (__bridge_transfer NSArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
    NSUInteger contactNum = 0;

    NSInteger recordId;
    ABRecordRef recId;

    for (i =0; i < [allData count]; i++)
    {
        appDelegate.dict_contact =[[NSMutableDictionary alloc] init];
        ABRecordRef ref =(__bridge ABRecordRef)(allData[i]);

        //mobile no and phone no
        ABMultiValueRef mobNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFIndex PhoneCount = ABMultiValueGetCount(mobNum);
        for (int k  = 0; k <PhoneCount; k++) {

           strMobile = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(mobNum, k));
           strPhone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(mobNum, k));

            if ([strMobile isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                mobileno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
            }
            else if ([strPhone isEqualToString:(NSString *)kABHomeLabel])
            {
                phoneno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
                break;
            }

            NSLog(@"Mobile: %@ phone: %@, %@",mobileno, phoneno, strPhone);
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",mobileno] forKey:@"mobno"];
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",phoneno] forKey:@"phnno"];
        }

        //image
        NSData *imgData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
        UIImage *image;
        if (imgData)
        {
           image = [UIImage imageWithData:imgData];
            NSLog(@"add image: %@",image);
        }else
        {
            image = [UIImage imageNamed:@"dummy.png"];
        }
        [appDelegate.dict_contact setObject:image forKey:@"image"];
        [appDelegate.arr_contact addObject:appDelegate.dict_contact];
    }
}
-(无效)显示联系人
{
int i;
appDelegate.arr_contact=[[NSMutableArray alloc]init];
ABAddressBookRef contactBook=ABAddressBookCreateWithOptions(无,空);
NSArray*所有数据=(u_桥_传输NSArray*)(ABAddressBookCopyArrayFallPeople(contactBook));
nsu整数contactNum=0;
NSInteger记录ID;
abrecordrefrecid;
对于(i=0;i<[allData count];i++)
{
appDelegate.dict_contact=[[NSMutableDictionary alloc]init];
ABRecordRef ref=(u_桥ABRecordRef)(所有数据[i]);
//手机号码和电话号码
ABMultiValueRef mobNum=ABRecordCopyValue(ref,kABPersonPhoneProperty);
CFIndex PhoneCount=ABMultiValueGetCount(mobNum);

对于(int k=0;k请参见此代码…)

-(void)GetAddressBook
{
   Contacts = [[NSMutableArray alloc]init];

   if (ABAddressBookCreateWithOptions) {

   @try {

        ABAddressBookRef addressBook = ABAddressBookCreate();
        // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        if (!addressBook) {
            NSLog(@"opening address book");
        }
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        NSLog(@"opening address book ==%ld",nPeople);

        for (int i=0;i < nPeople;i++) {

            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
            NSString *Contact;
            ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
            CFStringRef firstName, lastName;
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSString *email;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
            array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
            email = ([array count] > 0) ? array[0] : @"";

            if(firstName)
            {
                Contact = [NSString stringWithFormat:@"%@", firstName];
                if(lastName)
                    Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
            }
            [dOfPerson setObject:Contact forKey:@"name"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
            [dOfPerson setObject:email forKey:@"email"];

            NSString* mobileLabel;
            for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
            {
                mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    break ;
                }
            }
            [Contacts addObject:dOfPerson];
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);


    }
    dispatch_async(dispatch_get_main_queue(), ^{



    });
-(void)GetAddressBook
{
Contacts=[[NSMutableArray alloc]init];
如果(ABAddressBookCreateWithOptions){
@试一试{

ABAddressBookRef addressBook=ABAddressBookCreate(); //NSArray*人=(NSArray*)abAddressBookCopyArrayFallPeople(addressBook); 如果(!地址簿){ NSLog(@“期初通讯簿”); } CFArrayRef allPeople=abAddressBookCopyArrayFallPeople(addressBook); CFIndex nppeople=ABAddressBookGetPersonCount(地址簿); NSLog(@“打开通讯簿==%ld”,NPEOPE); for(int i=0;i0)?数组[0]:@“”; 如果(名字) { 联系人=[NSString stringWithFormat:@“%@”,名字]; 如果(姓氏) Contact=[NSString stringWithFormat:@“%@%@”,firstName,lastName]; } [dOfPerson setObject:联系forKey:@“name”]; [dOfPerson setObject:[NSString stringWithFormat:@“%d”,i]forKey:@“id”]; [dOfPerson setObject:[NSString stringWithFormat:@“%@”,“@”“]forKey:@“找到”]; [dOfPerson setObject:email forKey:@“email”]; NSString*移动标签; 对于(CFIndex j=0;j
在这里,您将获得电话号码、名字、姓氏和电子邮件Id。这里的联系人是一个
NSMutableArray
。还可以查看此答案以获取完整的代码集


请查看此代码

-(void)GetAddressBook
{
   Contacts = [[NSMutableArray alloc]init];

   if (ABAddressBookCreateWithOptions) {

   @try {

        ABAddressBookRef addressBook = ABAddressBookCreate();
        // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        if (!addressBook) {
            NSLog(@"opening address book");
        }
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        NSLog(@"opening address book ==%ld",nPeople);

        for (int i=0;i < nPeople;i++) {

            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
            NSString *Contact;
            ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
            CFStringRef firstName, lastName;
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSString *email;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
            array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
            email = ([array count] > 0) ? array[0] : @"";

            if(firstName)
            {
                Contact = [NSString stringWithFormat:@"%@", firstName];
                if(lastName)
                    Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
            }
            [dOfPerson setObject:Contact forKey:@"name"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
            [dOfPerson setObject:email forKey:@"email"];

            NSString* mobileLabel;
            for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
            {
                mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    break ;
                }
            }
            [Contacts addObject:dOfPerson];
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);


    }
    dispatch_async(dispatch_get_main_queue(), ^{



    });
-(void)GetAddressBook
{
Contacts=[[NSMutableArray alloc]init];
如果(ABAddressBookCreateWithOptions){
@试一试{

ABAddressBookRef addressBook=ABAddressBookCreate(); //NSArray*人=(NSArray*)abAddressBookCopyArrayFallPeople(addressBook); 如果(!地址簿){ NSLog(@“期初通讯簿”); } CFArrayRef allPeople=abAddressBookCopyArrayFallPeople(addressBook); CFIndex nppeople=ABAddressBookGetPersonCount(地址簿); NSLog(@“打开通讯簿==%ld”,NPEOPE); for(int i=0;i0)?数组[0]:@“”; 如果(名字) { 联系人=[NSString stringWithFormat:@“%@”,名字]; 如果(姓氏) Contact=[NSString stringWithFormat:@“%@%@”,firstName,lastName]; } [dOfPerson setObject:联系forKey:@“name”];