我正在尝试将联系人添加到 iOS8 中的地址簿。不能再这样做了。下面是我的代码:
-(void)addPersonToAddressBook {
NSString * fullName = integrationDictionary[@"fullName"];
ABPeoplePickerNavigationController *pp =[ABPeoplePickerNavigationController new];
ABAddressBookRef addressBook = [pp addressBook];
ABRecordRef entry = ABPersonCreate();
CFErrorRef cfError=nil;
ABRecordSetValue(entry, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fullName) , nil);
ABAddressBookAddRecord(addressBook, entry, &cfError);
if (ABAddressBookSave(addressBook, &cfError)) {
NSString *saveMessage = [NSString stringWithFormat:@"%@ has been added to your address book.", fullName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact Added" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
NSString *saveMessage = [NSString stringWithFormat:@"There was an error adding %@ to your address book.", fullName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
NSLog(@"error is %@", cfError);
错误显示为空。有没有人见过这个?有什么解决方法吗?
最佳答案
错误正在返回 NULL
,因为没有注册错误。
问题是 [pp addressBook]
正在返回 nil
。所以你的 ABAddressBookRef addressBook
引用是 nil
。
解决方法是使用 ABAddressBookCreateWithOptions
而不是 ABPeoplePickerNavigationController
的 [pp addressBook]
方法。
这是一个在 iOS 7.1 和 iOS 8.1 上都运行良好的示例:
-(void)requestAuthorizationAndAddPersonToAddressBook
{
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
[self addPersonToAddressBook];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addPersonToAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
}
-(void)addPersonToAddressBook {
NSString * fullName = @"James Bond";
CFErrorRef abCreateError = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &abCreateError);
if (abCreateError) {
NSLog(@"Error occurred: %@", abCreateError);
}
ABRecordRef entry = ABPersonCreate();
CFErrorRef cfError=nil;
ABRecordSetValue(entry, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fullName) , nil);
ABAddressBookAddRecord(addressBook, entry, &cfError);
if (ABAddressBookSave(addressBook, &cfError)) {
NSString *saveMessage = [NSString stringWithFormat:@"%@ has been added to your address book.", fullName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact Added" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
NSString *saveMessage = [NSString stringWithFormat:@"There was an error adding %@ to your address book.", fullName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
if (cfError) {
NSLog(@"error is %@", cfError);
}
}
关于iOS 8 添加联系人到地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196882/