【发布时间】:2016-07-20 19:37:11
【问题描述】:
通讯录未添加联系人。在 iOS 9 中将人员保存到地址簿时出错,但在 iOS 6 中工作。当我添加联系人时,if 块将使用日志错误将人员保存到地址簿执行。
-(void)addContactToPhoneBook{
ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
addressBook = [peoplePicker addressBook];
// create person record
person = ABPersonCreate();
cfError = nil;
if (firstName) {
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
}
if (jobTitle) {
ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef)(jobTitle), nil);
}
if (personEmail)
{
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
if (phoneNo)
{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
}
// add address
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
//Add person Object to addressbook Object.
ABAddressBookAddRecord(addressBook, person, &cfError);
if (ABAddressBookSave(addressBook, nil))
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Contact added sucessfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
NSLog(@"\nPerson Saved successfuly");
} else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to add contact" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
NSLog(@"\n Error Saving person to AddressBook");
}
}
【问题讨论】:
标签: ios objective-c iphone cocoa-touch ios9