How to write Facebook/Twitter to address book in iOS?

时间秒杀一切 提交于 2019-12-02 19:04:43

问题


In my app I export some data to the address book in order to create some new contact entries. I can export everything I want to without problem, except Facebook and Twitter addresses. I'm completely lost on these two.

Here's some code I'm using to export non-Facebook/Twitter data that DOES work:

ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(thePhoneMobile), kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(thePhoneHome), kABHomeLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, NULL);
CFRelease(multiPhone);

Here's how I'm trying to export Facbeook which does NOT work:

ABMutableMultiValueRef multiSocial = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiSocial, (__bridge CFTypeRef)(theFacebook), kABPersonSocialProfileServiceFacebook, NULL);
ABRecordSetValue(newPerson, kABPersonSocialProfileProperty, multiSocial, NULL);
CFRelease(multiSocial);

Where am I going wrong?


回答1:


Setting social profiles is very tricky for some reason, but here is the code necessary to doing so:

ABMultiValueRef multiSocial = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

ABMultiValueAddValueAndLabel(multiSocial, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:(NSString *)kABPersonSocialProfileServiceFacebook, kABPersonSocialProfileServiceKey, theFacebook, kABPersonSocialProfileUsernameKey,nil]), kABPersonSocialProfileServiceFacebook, NULL);

ABRecordSetValue(newPerson, kABPersonSocialProfileProperty, multiSocial, NULL);

According to the documentation, the social profiles is a kABMultiDictionaryPropertyType, not kABMultiStringType.

That also means that you need to add dictionaries to multiSocial, not strings. The format does not make sense to me for the dictionary, so just copy and paste whenever you need to use it.

Then, you set it how you did before.



来源:https://stackoverflow.com/questions/22897007/how-to-write-facebook-twitter-to-address-book-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!