所以我需要在ABRecordRef中添加一个Twitter帐户。然而,似乎最快的方法是获取社交配置文件属性的多值引用,创建它的可变版本,查找它是否有twitter条目,如果它已经存在,则创建一个可变副本字典并用新的替换它。否则,使用twitter密钥和用户名创建一个新的字典条目。
实际上它实际上非常冗长:
ABRecordRef person;
ABMultiValueRef oldsocials = ABRecordCopyValue(abperson, kABPersonSocialProfileProperty);
ABMutableMultiValueRef newsocials;
if(oldsocials){
newsocials = ABMultiValueCreateMutableCopy(oldsocials);
} else {
newsocials = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
}
CFIndex socialsCount = ABMultiValueGetCount(socials);
bool foundtwitter = false;
for (int k=0 ; k<socialsCount ; k++) {
CFDictionaryRef socialValue = (CFDictionaryRef)ABMultiValueCopyValueAtIndex(socials, k);
if(socialValue){
if(CFStringCompare( (CFStringRef)CFDictionaryGetValue(socialValue, kABPersonSocialProfileServiceKey), kABPersonSocialProfileServiceTwitter, 0)==kCFCompareEqualTo) {
CFMutableDictionaryRef tomutate = CFDictionaryCreateMutableCopy(socialValue);
CFDictionarySetValue(tomutate, kABPersonSocialProfileUsernameKey, toNS(thetwitter));
ABMultiValueReplaceValueAtIndex(socials, tomutate, k);
CFRelease(tomutate);
foundtwitter = true;
}
CFRelease(socialValue);
}
if(foundtwitter)
break;
}
if(!foundtwitter){
ABMultiValueAddValueAndLabel(newsocials, [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
@"justinbieber", kABPersonSocialProfileUsernameKey,
nil], kABPersonSocialProfileServiceTwitter, NULL);
}
ABRecordSetValue(abperson, kABPersonSocialProfileProperty, newsocials, NULL);
if(oldsocials)
CFRelease(oldsocials);
CFRelease(newsocials);
实际上只是添加推特很长,感觉我做错了。我的意思是,理想情况下,用另一种语言添加类似的东西:
person["socialServices"]["twitter"] = "@SomeUserName";
person.save();
我错过了什么?
答案 0 :(得分:7)
在我的朋友看下面,对于facebook和twitter - 它会起作用:
//adding social
ABMultiValueRef social = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
[tempVCard objectForKey:@"twitterUserName"], kABPersonSocialProfileUsernameKey,
nil]), kABPersonSocialProfileServiceTwitter, NULL);
ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceFacebook, kABPersonSocialProfileServiceKey,
[tempVCard objectForKey:@"facebookUserName"], kABPersonSocialProfileUsernameKey,
nil]), kABPersonSocialProfileServiceFacebook, NULL);
ABRecordSetValue(person, kABPersonSocialProfileProperty, social, NULL);
CFRelease(social);
我的私有数组“tempVCard”将是您用户动态数据的数组。