使用kABPersonPhoneProperty检索电话号码

时间:2013-08-01 09:31:32

标签: iphone ios5 ios6 addressbook

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

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

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

请检查我编辑的代码。

2 个答案:

答案 0 :(得分:-1)

请参阅此代码......

-(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(), ^{



    });

在这里,您将获得电话号码,名字,姓氏和电子邮件ID。此处通讯录NSMutableArray。还可以查看完整代码的答案。

contact details from ipad using objective-c

答案 1 :(得分:-1)

试试这个在这段代码中,如果您希望手机号码只使用此kABPersonPhoneProperty,我会得到姓名和手机号码:

CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i=0;i < nPeople;i++)
{
    NSString* name;
    NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    CFStringRef firstName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    name = (__bridge NSString *)(firstName);
    [tempContactDic setValue:name forKey:@"name"];
    //fetch email id
    NSString *strEmail;
    NSMutableString* strMobile;
    ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonPhoneProperty);
    CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
    strEmail = (__bridge  NSString *)tempEmailref;
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"-" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"(" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@")" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@" " withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"+" withString:@""];
    strMobile = [[NSMutableString alloc] initWithString:strEmail];
    if ([strEmail length] == 10) {
        [strMobile insertString:@"91" atIndex:0];
    }
    else {

    }
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * myNumber = [f numberFromString:strMobile];
    [tempContactDic setValue:myNumber forKey:@"phone"];

    [contactsArray addObject:tempContactDic];
}
/// With below two steps of code you'll get the contacts in alphabetical order
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[contactsArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];