【问题标题】:How do I set an Instant Message property on an Address Book contact?如何在通讯簿联系人上设置即时消息属性?
【发布时间】:2013-04-18 23:47:48
【问题描述】:

我尝试使用以下代码设置要添加到通讯簿的新联系人的 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 字段之外的所有内容都会被填充。我做错了什么?

【问题讨论】:

    标签: ios ios6 ios6.1


    【解决方案1】:

    实例是 kABMultiDictionaryPropertyType 类型的多值属性。将一个 Skype id 插入 ABRecordRef 的片段如下所示。您可以在一个 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);
    

    【讨论】:

    • 你能解释一下NSDictionary *dictionary的内容吗?我对此以及如何为多个 IM 使用相同的字典有点困惑。请同时提供该代码
    • @milanpanchal,字典包含您要添加的信息。 (kABPersonInstantMessageServiceSkype, kABPersonInstantMessageServiceKey) 表示它将是 Skype id。 (@"YourSkypeIDGoesHere", kABPersonInstantMessageUsernameKey) - Skype id 本身。在所有这些联系人都具有相同的 Skype id 之前,您不能将此字典用于多个 AB 记录。所有三个常量都来自 ABPerson.h 头文件。为您提供代码?抱歉,没有。上面的代码是自我解释的。请参阅 Apple 文档。
    • 感谢您提供信息。我试图重用导致错误的同一个字典。现在一切就绪
    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多