在我的应用程序中,我已存储ABPropertyID
在数据库中,并基于此显示了数量,ABRecordRef
但是ABAddressBookGetPersonWithRecordID(addressBook, recordID).
当在联系人中删除联系人并尝试获取ABRecordRef
时,应用程序崩溃是显而易见的。
所以我的问题是:
有什么方法可以检查地址簿中的记录 ID 是否可用?
在我的应用程序中,我已存储ABPropertyID
在数据库中,并基于此显示了数量,ABRecordRef
但是ABAddressBookGetPersonWithRecordID(addressBook, recordID).
当在联系人中删除联系人并尝试获取ABRecordRef
时,应用程序崩溃是显而易见的。
所以我的问题是:
有什么方法可以检查地址簿中的记录 ID 是否可用?
此函数不会抛出异常,但如果没有具有此 ID 的记录,则返回 NULL。
试试这个
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, @"", &error);
ABRecordSetValue(newPerson, kABPersonJobTitleProperty,@"", &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, @"", kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, @"", kABOtherLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail,(__bridge CFTypeRef)(contact_email) , kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
// ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
// NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
// [addressDictionary setObject:@"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
// [addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
// [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
// [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
// ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
// ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
// CFRelease(multiAddress);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
NSLog(@"Danger Will Robinson! Danger!");
}
// ABAddressBookRef addressBook = ABAddressBookCreate();
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
CFErrorRef error = NULL;
// ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
NSLog(@"%@",(__bridge CFTypeRef)(firstname));
ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
NSLog(@"%@",(__bridge CFTypeRef)(lastname));
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, @"", &error);
ABRecordSetValue(newPerson, kABPersonJobTitleProperty,@"", &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone,(__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, @"", kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, @"", kABOtherLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)(contact_email), kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
// ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
// NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
// [addressDictionary setObject:@"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
// [addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
// [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
// [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
// ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
// ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
// CFRelease(multiAddress);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
alertvalue++;
if (error != NULL)
{
NSLog(@"Error not found!");
}
// ABAddressBookRef addressBook = ABAddressBookCreate();
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
} }