2013-04-18 26 views
0

我有我試圖用設置一個新的聯繫人我添加到地址簿的AIM和Skype性質下面的代碼:如何在地址簿聯繫人上設置即時消息屬性?

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

Instants是類型爲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 *字典的內容嗎?我對此有點困惑,以及如何爲多個即時消息使用相同的字典。請提供該代碼 – milanpanchal

+1

@milanpanchal,字典包含您想要添加的信息。 (kABPersonInstantMessageServiceSkype,kABPersonInstantMessageServiceKey)意味着它將是Skype的id。 (@「YourSkypeIDGoesHere」,kABPersonInstantMessageUsernameKey) - Skype ID本身。在所有這些聯繫人具有相同的Skype ID之前,您不能重複使用該字典中的多個AB記錄。所有這三個常量都來自ABPerson.h頭文件。爲你提供代碼?抱歉,沒有。上面的代碼是自我解釋的。請參閱Apple文檔。 –

+0

感謝您提供信息。我試圖重複使用導致錯誤的相同字典。現在全部設置 – milanpanchal

相關問題