2014-10-04 7 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); 

오류가 null로 표시됩니다. 이걸 본 사람 있어요? 해결 방법은 무엇입니까?

답변

2

오류가 등록되어 있지 않으므로 오류는 NULL을 반환합니다.

문제는 [pp addressBook]nil을 반환한다는 것입니다. 따라서 ABAddressBookRef addressBook 참조 번호는 nil입니다.

해결 방법은 [pp addressBook] 메서드 대신 ABAddressBookCreateWithOptions 메서드를 ABPeoplePickerNavigationController으로 사용하는 것입니다. 여기

은 모두 iOS에서 7.1 & 아이폰 OS 8.1 잘 작동합니다 샘플입니다 : ABAddressBookRequestAccessWithCompletion``````코드 블록에서

-(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

을 - 당신이 확인하는 경우 문이 안되는'' 사용자가 실제로 앱 액세스 권한을 부여했는지 확인하기위한 'BOOL'' 값 – Supertecnoboff

+1

@Supertecnoboff 그것은 내재적 결함이있는 예일뿐입니다 ... 나는 OP에 답변했습니다. – Razvan