1
我試圖將聯繫人信息插入到地址組中的特定組中,但我無法保存除名字,姓氏以外的任何值。我正在使用以下方法保存聯繫人信息,並且應用程序崩潰時沒有跟蹤(BAD ACCESS)。無法將聯繫人信息保存到地址簿中
- (void) addSingleContact:(NSDictionary*)contact {
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle:@"Cannot Add Contact"
message:@"You must give the app permission to add the contact first."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantAddContactAlert show];
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef error, anError;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); // create address book record
ABRecordRef person = ABPersonCreate(); // create a person
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue , (__bridge CFTypeRef)([contact objectForKey:@"mobile"]), kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)([contact objectForKey:@"firstName"]) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)([contact objectForKey:@"lastName"]), nil);
// *** CRASHES ON NEXT LINE ***
ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)([contact objectForKey:@"email"]), nil);
ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFTypeRef)([contact objectForKey:@"designation"]), nil);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError);
ABAddressBookAddRecord(addressBook, person, nil);
ABRecordRef group = [self getAddressGroup:addressBook];
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, nil); //save the record
CFRelease(person); // relase the ABRecordRef variable
} else { //ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool authorized, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!authorized){
UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle:@"Cannot Add Contact"
message:@"You must give the app permission to add the contact first."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantAddContactAlert show];
}
});
});
}
}
的getAddressGroup:
方法如下,但我懷疑它是負責任的,因爲名稱保存成功:
- (ABAddressBookRef) getAddressGroup:(ABAddressBookRef)addressBook {
ABAddressBookRef group = NULL;
NSArray *groups = (__bridge NSArray *) ABAddressBookCopyArrayOfAllGroups(addressBook);
for (id _group in groups) {
NSString *currentGroupName = (__bridge NSString*) ABRecordCopyValue((__bridge ABRecordRef)(_group), kABGroupNameProperty);
if ([currentGroupName isEqualToString:GROUP_NAME]){
group = (__bridge ABAddressBookRef)(_group);
CFRelease((__bridge CFTypeRef)(currentGroupName));
break;
}
CFRelease((__bridge CFTypeRef)(currentGroupName));
}
if (groups) {
groups = nil;
}
if (group == NULL) {
group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty, GROUP_NAME, nil);
}
return group;
}
如果我試圖保存任何其他價值,包括職務,組織,電子電子郵件,手機等,該應用程序崩潰與BAD ACCESS
。我只開始使用ABAddressBook
和地址簿編程,我根本沒有得到我應該做的。