私のアプリABPropertyID
ではデータベースに保存し、それに基づいての数を表示しましたがABRecordRef
、ABAddressBookGetPersonWithRecordID(addressBook, recordID).
連絡先で連絡先を削除して取得しようとするとABRecordRef
、アプリがクラッシュするのは明らかです。
だから私の質問は:
recordIDがaddressBookで使用可能であることを確認する方法はありますか?
私のアプリABPropertyID
ではデータベースに保存し、それに基づいての数を表示しましたがABRecordRef
、ABAddressBookGetPersonWithRecordID(addressBook, recordID).
連絡先で連絡先を削除して取得しようとするとABRecordRef
、アプリがクラッシュするのは明らかです。
だから私の質問は:
recordIDがaddressBookで使用可能であることを確認する方法はありますか?
この関数は例外をスローしませんが、この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
} }