У меня есть следующий код:
ABAddressBookRef ab;
ab = ABAddressBookCreate();
int len = (int) ABAddressBookGetPersonCount(ab);
int i;
for(i = 1; i < (len + 1); i++)
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
static char* fallback = "";
int fbLength = strlen(fallback);
int firstNameLength = fbLength;
bool firstNameFallback = true;
int lastNameLength = fbLength;
bool lastNameFallback = true;
if (firstName != NULL)
{
firstNameLength = (int) CFStringGetLength(firstName);
firstNameFallback = false;
}
if (lastName != NULL)
{
lastNameLength = (int) CFStringGetLength(lastName);
lastNameFallback = false;
}
if (firstNameLength == 0)
{
firstNameLength = fbLength;
firstNameFallback = true;
}
if (lastNameLength == 0)
{
lastNameLength = fbLength;
lastNameFallback = true;
}
firstNameString = malloc(sizeof(char)*(firstNameLength+1));
lastNameString = malloc(sizeof(char)*(lastNameLength+1));
if (firstNameFallback == true)
{
strcpy(firstNameString, fallback);
}
else
{
CFStringGetCString(firstName, firstNameString, 10*CFStringGetLength(firstName), kCFStringEncodingASCII);
}
if (lastNameFallback == true)
{
strcpy(lastNameString, fallback);
}
else
{
CFStringGetCString(lastName, lastNameString, 10*CFStringGetLength(lastName), kCFStringEncodingASCII);
}
printf("%d.\t%s %s\n", i, firstNameString, lastNameString);
NSString *fname= [NSString stringWithFormat:@"%s",firstNameString];
NSString *lname= [NSString stringWithFormat:@"%s",lastNameString];
[dict setValue:fname forKey:@"fname"];
[dict setValue:lname forKey:@"lname"];
[self.arrname addObject:[dict copy]];
if (firstName != NULL)
{
CFRelease(firstName);
}
if (lastName != NULL)
{
CFRelease(lastName);
}
free(firstNameString);
free(lastNameString);
}
, он работает хорошо в первый раз.
Но когда я удаляю запись из списка контактов, а затем пытаюсь добавить запись в мой список приложений наследующее положение.
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
Может кто-нибудь решить эту проблему ??Любая идея с благодарностью.