通过ABAddressBookGetPersonWithRecordID获取NULL [英] Getting NULL by ABAddressBookGetPersonWithRecordID

查看:202
本文介绍了通过ABAddressBookGetPersonWithRecordID获取NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ABAddressBookGetPersonWithRecordID 来获取新的记录。但返回的值是 NULL 。我在AddressBook中有三个联系人,但它返回Null。我正在测试iOS 5模拟器上的代码。我使用的代码如下所示。

I am using the ABAddressBookGetPersonWithRecordID in order to get the contant Record. But the value returned is NULL. I have three Contacts in the AddressBook, but it returns Null. I am testing the code on the iOS 5 Simulator. The code which I used is given below.

for(int counter = 1;counter <= ABAddressBookGetPersonCount(addressBookRef); counter++)
{
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBookRef, (ABRecordID)counter);
    NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
    NSLog(@"record id is %@",recordId);

    if (person == NULL)
    {
        continue;
    }
    else
    {
        [VCard generateVCardStringWithRecID:counter];
    }
}


推荐答案

尝试下面的代码从电话簿获取人的所有信息

TRY below code for get all the information of people from phonebook

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABAddressBookRef addressBook = ABAddressBookCreate();

    int i;
    NSString *strName = @"";
    NSString* company = @"";
    NSString *address = @"";
    NSString *suburb = @"";
    NSString *postalcode = @"";
    NSString *state = @"";
    NSString *country = @"";
    NSString *mobile = @"";
    NSString *phone = @"";
    NSString *emailid = @"";


    strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
    CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
    company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);

    NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
    CFRelease(name);

    for (i = 0; i < [allPeople count]; i++)
    {
        ABRecordRef record = [allPeople objectAtIndex:i];

        ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
        for(CFIndex i=0; i<ABMultiValueGetCount(multiValue); i++)
        {
            NSString* HomeLabel = (NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
            if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
            {
                CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
                address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
                suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
                postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
                state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
                country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCountryKey)];

                CFRelease(dict);
            }
            CFRelease(HomeLabel);
        }
        CFRelease(multiValue);
    }
    CFRelease(allPeople);


    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* mobileLabel = nil;
    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",mobile);
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",phone);

            CFRelease(mobileLabel);
            break ;
        }
        CFRelease(mobileLabel);

    }
    CFStringRef value, label;
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
    if (count == 1)
    {
        value = ABMultiValueCopyValueAtIndex(multi, 0);
        emailid = (NSString*) value;
        NSLog(@"self.emailID   %@",emailid);
        CFRelease(value);
    }
    else
    {
        for (CFIndex i = 0; i < count; i++)
        {
            label = ABMultiValueCopyLabelAtIndex(multi, i);
            value = ABMultiValueCopyValueAtIndex(multi, i);

            // check for Work e-mail label
            if (CFStringCompare(label, kABWorkLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }
            else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }

            CFRelease(label);
            CFRelease(value);
        }
    }
    CFRelease(multi);

        }


    CFRelease(phones);
    CFRelease(addressBook);
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}

这篇关于通过ABAddressBookGetPersonWithRecordID获取NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆