2013-04-18 6 views
0

I가 내가 주소록에 추가 해요 새 연락처의 AIM 스카이프 속성을 설정하는 데 사용하기 위해 노력하고있어 다음 코드를주소록 연락처에 인스턴트 메시지 속성을 설정하는 방법은 무엇입니까?

ABRecordRef record = ABPersonCreate(); 
CFErrorRef an_error = NULL; 

ABMutableMultiValueRef multi_aim = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
ABMultiValueAddValueAndLabel(multi_aim, @"ExampleNick", kABPersonInstantMessageServiceAIM, NULL); 
ABRecordSetValue(record, kABPersonInstantMessageProperty, multi_aim, &an_error); 

ABMutableMultiValueRef multi_skype = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
ABMultiValueAddValueAndLabel(multi_skype, @"example.nick", kABPersonInstantMessageServiceSkype, NULL); 
ABRecordSetValue(record, kABPersonInstantMessageProperty, multi_skype, &an_error); 

if (an_error != NULL) { 
    NSLog(@"Error while creating person"); 
} 

CFErrorRef error = NULL; 
ABAddressBookRef address_book = ABAddressBookCreateWithOptions(NULL, &error); 
ABAddressBookRequestAccessWithCompletion(address_book, ^(bool granted, CFErrorRef error) { 
    if (!granted) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"Unable to access address book" 
                 delegate:self 
               cancelButtonTitle:nil 
               otherButtonTitles:nil]; 
     [alert show]; 
    } else { 
     BOOL is_added = ABAddressBookAddRecord(address_book, record, &error); 

     if (is_added) { 
      NSLog(@"Address book entry added"); 
     } else { 
      NSLog(@"ERROR adding address book entry: %@", error); 
     } 

     error = NULL; 

     BOOL is_saved = ABAddressBookSave(address_book, &error); 

     if (is_saved) { 
      NSLog(@"Saved address book"); 
     } else { 
      NSLog(@"ERROR saving address book: %@", error); 
     } 
    } 

    CFRelease(record); 
    CFRelease(multi_aim); 
    CFRelease(multi_skype); 
    CFRelease(address_book); 
}); 

(나는 또한 이름과 성을 추가 해요 간결하게하기 위해 예제에서 제외 시켰습니다.)

레코드가 추가되면 AIM과 Skype 필드가 모두 채워집니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

3

인스턴스는 kABMultiDictionaryPropertyType 유형의 다중 값 속성입니다. ABRecordRef에 하나의 skype id를 삽입하는 스 니펫이 아래에 있습니다. 하나의 ABRecordRef에 여러 인스턴트를 삽입 할 수 있습니다.

ABRecordRef record = ABPersonCreate(); 
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
     (NSString*)kABPersonInstantMessageServiceSkype, (NSString*)kABPersonInstantMessageServiceKey, 
     @"YourSkypeIDGoesHere", (NSString*)kABPersonInstantMessageUsernameKey, 
     nil 
    ]; 
    CFStringRef label = NULL; // in this case 'IM' will be set. But you could use something like = CFSTR("Personal IM"); 
    CFErrorRef error = NULL; 
    ABMutableMultiValueRef values = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 
    BOOL didAdd = ABMultiValueAddValueAndLabel(values, (__bridge CFTypeRef)(dictionary), label, NULL); 
    BOOL didSet = ABRecordSetValue(record, kABPersonInstantMessageProperty, values, &error); 
    if (!didAdd || !didSet) { 
     CFStringRef errorDescription = CFErrorCopyDescription(error); 
     NSLog(@"%s error %@ while inserting multi dictionary property %@ into ABRecordRef", __FUNCTION__, dictionary, errorDescription); 
     CFRelease(errorDescription); 
    } 
    CFRelease(values); 
+0

NSDictionary * 사전의 내용을 설명 할 수 있습니까? 나는 그것에 대해 조금 혼란스럽고 여러 IM에 대해 동일한 사전을 사용하는 방법에 대해 혼란스러워합니다. 해당 코드를 제공하십시오. – milanpanchal

+1

@milanpanchal, 사전은 귀하가 추가하고자하는 정보를 포함합니다. (kABPersonInstantMessageServiceSkype, kABPersonInstantMessageServiceKey)는 Skype ID임을 의미합니다. @ Skype ID 자체. 이 연락처에 동일한 Skype ID가있을 때까지 여러 AB 레코드에 대해이 사전을 다시 사용할 수 없습니다. 세 개의 상수는 모두 ABPerson.h 헤더 파일에 있습니다. 당신에게 코드를 제공합니까? 미안, 안돼. 위의 코드는 스스로 설명됩니다. Apple 설명서를 참조하십시오. –

+0

정보를 제공해 주셔서 감사합니다. 오류가 발생하는 동일한 사전을 다시 사용하려고했습니다. 지금 모든 세트 – milanpanchal