使用通讯簿框架iOS 9导出联系人时出错?
联系人未添加到通讯簿中。在iOS 9中将人员保存到通讯簿时出错,但在iOS 6中工作。当我添加联系人时,执行if块时会出现日志错误,将person保存到AddressBook使用通讯簿框架iOS 9导出联系人时出错?,ios,objective-c,iphone,cocoa-touch,ios9,Ios,Objective C,Iphone,Cocoa Touch,Ios9,联系人未添加到通讯簿中。在iOS 9中将人员保存到通讯簿时出错,但在iOS 6中工作。当我添加联系人时,执行if块时会出现日志错误,将person保存到AddressBook -(void)addContactToPhoneBook{ ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init]; addressBook = [peoplePicker add
-(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");
}
}
试试这个
- (void)viewDidLoad {
[super viewDidLoad];
arrContactName = [[NSMutableArray alloc]init];
arrPhoneNumber = [[NSMutableArray alloc]init];
arrAddContact = [[NSMutableArray alloc]init];
}
-(void)viewWillAppear:(BOOL)animated
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
[self getAllContacts];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Unable to Access!" message:@"Grant us access now! Change privacy setting in settings app." delegate:self cancelButtonTitle:@"Not now" otherButtonTitles:@"OK", nil];
[alert show];
}
}
#pragma mark -
#pragma mark - Get All Contacts
-(void)getAllContacts
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil)
{
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (lastName == nil) {
strLastname = @"";
}
else
strLastname = lastName;
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, strLastname];
NSString *phone=nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
[arrContactName addObject:fullName];
[arrPhoneNumber addObject:phone];
NSLog(@"Full Name =%@",fullName);
NSLog(@"Phone Number =%@",phone);
}
CFRelease(addressBook);
}
}
-(void)viewDidLoad{
[超级视图下载];
arrContactName=[[NSMutableArray alloc]init];
arrPhoneNumber=[[NSMutableArray alloc]init];
arrAddContact=[[NSMutableArray alloc]init];
}
-(无效)视图将显示:(BOOL)动画
{
ABAddressBookRef addressBookRef=ABAddressBookCreateWithOptions(NULL,NULL);
if(ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef,^(已授予bool,CFErrorRef错误){
如果(授予){
[自我解除视图控制器激活:是完成:无];
}否则{
[自我解除视图控制器激活:是完成:无];
}
});
}
else if(ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusAuthorized)
{
[自我获取联系人];
}
其他的
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“无法访问!”消息:@“立即授予我们访问权限!在设置应用程序中更改隐私设置”。代理:自取消按钮:@“现在不”其他按钮:@“确定”,无];
[警报显示];
}
}
#布拉格标记-
#pragma标记-获取所有联系人
-(void)getAllContacts
{
CFErrorRef error=NULL;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL,错误(&R);
如果(地址簿!=nil)
{
NSArray*所有联系人=(u_u桥u传输NSArray*)ABAddressBookCopyArrayFallPeople(addressBook);
整数i=0;
对于(i=0;i<[allContacts count];i++)
{
ABRecordRef contactPerson=(桥ABRecordRef)所有联系人[i];
NSString*firstName=(桥接传输NSString*)ABRECORDCYVALUE(contactPerson,KabbersonfirstNameProperty);
NSString*lastName=(uu_桥u传输NSString*)ABRECORDCYVALUE(contactPerson,kABPersonLastNameProperty);
如果(lastName==nil){
strLastname=@”;
}
其他的
strLastname=lastName;
NSString*fullName=[NSString stringWithFormat:@“%@%@”,firstName,strLastname];
NSString*phone=nil;
ABMultiValueRef phoneNumbers=ABRecordCopyValue(contactPerson,kABPersonPhoneProperty);
如果(ABMultiValueGetCount(电话号码)>0){
电话=(uuuu桥u传输字符串*)
ABMultiValueCopyValueAtIndex(电话号码,0);
}否则{
电话=@“[无]”;
}
[arrContactName addObject:fullName];
[arrPhoneNumber addObject:phone];
NSLog(@“全名=%@”,全名);
NSLog(@“电话号码=%@”,电话);
}
CFRelease(通讯录);
}
}
希望这能有所帮助。请重新检查问题。