ios - 如何在通讯簿联系人上设置即时消息属性?

标签 ios ios6 ios6.1

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

最佳答案

Instants 是 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);

关于ios - 如何在通讯簿联系人上设置即时消息属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16094970/

相关文章:

iphone - 更改 UIImage 上触摸部分的 Alpha 值?

iphone - 无法在 tableviewcell ios 7 上显示删除按钮

ios - AIR 移动调试 - "Enter ip address or hostname"

ios - 可以用 iPad 从网页上拍照吗?

IOS UITableview 在 IOS6 中滚动不流畅

ios-ui-automation - iOS 仪器自动化 "setValue"不工作

ios - 应用商店提交期间 Xcode 中的错误 ITMS-90203

iphone - 如何在 iOS 6 中为 UIActivityViewController 设置收件人?

ios - 应用程序在 IOS 6 上崩溃 - UIViewControllerHierarchyInconsistency

ios6 - iOS 6 UIView动画补全