iOS 8将地址簿添加到地址簿
问题描述:
我正在尝试将联系人添加到iOS8中的地址簿。无法再这样做了。这里是我的代码如下:iOS 8将地址簿添加到地址簿
-(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
而不是[pp addressBook]
方法ABPeoplePickerNavigationController
。
下面是其工作只是罚款不论是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);
}
}
在代码块'''ABAddressBookRequestAccessWithCompletion''' - 你不应该有一个if语句,它检查'' 'BOOL'''的价值,看看用户是否真的授予应用程序访问? – Supertecnoboff 2015-07-30 05:58:53
@Supertecnoboff这只是一个固有缺陷的例子......我已经回答了OP。 – Razvan 2015-07-30 09:02:08