2014-10-04 166 views
3

我正在嘗試將聯繫人添加到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); 

錯誤顯示爲空。有沒有人見過這個?任何解決方法?

回答

2

錯誤返回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); 
     } 
    } 
+3

在代碼塊'''ABAddressBookRequestAccessWithCompletion''' - 你不應該有一個if語句,它檢查'' 'BOOL'''的價值,看看用戶是否真的授予應用程序訪問? – Supertecnoboff 2015-07-30 05:58:53

+1

@Supertecnoboff這只是一個固有缺陷的例子......我已經回答了OP。 – Razvan 2015-07-30 09:02:08