0

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?

Kent
  • 1,705
  • 3
  • 16
  • 26
  • Any suggestions? Could it be something to do with kABPersonSocialProfileServiceFacebook? – Kent Apr 08 '14 at 18:46

1 Answers1

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.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • Thanks so much. I'll give this a try later today! – Kent Apr 12 '14 at 14:00
  • The Address Book seems complicated enough, and then they throw this into the ring, and it makes you want to just give up. – erdekhayser Apr 12 '14 at 17:05
  • Absolutely the right answer. Thanks! (P.S. "ref" became "newPerson" in my example, "yourFacebookAccount" became "theFacebook", and "social" became "multiSocial" to match my original code.) – Kent Apr 12 '14 at 21:59
  • This works great and I even got it working for Twitter, but how could I extend this to add a "custom" service this way as well? – Kent Apr 13 '14 at 02:30
  • Short: yes. Long: ask another question :] – erdekhayser Apr 13 '14 at 03:04
  • Posted new question here: http://stackoverflow.com/questions/23038691/how-do-i-write-a-custom-social-profile-entry-to-the-ios-addressbook – Kent Apr 13 '14 at 03:13
  • Seems to all work great on simulator, but on live iPad (iOS 7) none of the social info gets saved to Contact. :( – Kent Apr 13 '14 at 04:03
  • Can you actually open such Facebook pages on iPhone? In my case the FB app opens but doesn't display the FB page I linked. The same works fine on Mac and the iOS Simulator using Safari, though. I cross-checked and found that FB person records created by the system open correctly in the FB app. They also include the userid and URL when exported as VCF. – Ortwin Gentz Jan 26 '15 at 14:10
  • Does anyone have a lead on how to do this in Swift? – Dan Mar 24 '15 at 16:52