0

Been a while since I had to deal with AddressBook framework.

I am wondering if there is a way to get some "Add to Addressbook" functionality for free when setting UIActivityViewController.activityItems to a vCard with something like:

    let vCardString = [
        "BEGIN:VCARD",
        "VERSION:3.0",
        "N:Gump;Forrest",
        "FN:Forrest Gump",
        "ORG:Bubba Gump Shrimp Co.",
        "TEL;TYPE=WORK,VOICE:(111) 555-1212",
        "END:VCARD"
        ].joinWithSeparator("\n")

    let vCardData = vCardString.dataUsingEncoding(NSUTF8StringEncoding)!
    let vCardActivity = NSItemProvider(item: vCardData, typeIdentifier: kUTTypeVCard as String)

    // URL, test,
    let activityViewController = UIActivityViewController(activityItems: [vCardActivity], applicationActivities: activities)

I want to circumvent going the long way round, especially when supporting iOS 8 and 9: With AddressBook and Contacts framework I would create a person, request permission and present the view controller.

Is there a shorter way, if I "just" want to provide a way to add or share a contact via UIActivityViewController ?

Edit: The above code does not work, speaking of no corresponding iOS activity for the system's contacts is shown. Evernote takes up and shows an activity with correct name. When added to evernote, the vCard is shown in a note with correct details.

Frederik Winkelsdorf
  • 4,383
  • 1
  • 34
  • 42
  • @Hiren Dhamecha: Little over edited? Sorry but, why are you Code Highlighting all those things? "Add to Addressbook" is neither code nor an official term. "iOS 8 and 9" too. And iOS itself in backticks? Regarding the 3 mods that accepted the changes: ???. – Frederik Winkelsdorf Apr 04 '16 at 11:59
  • 1
    No mods involved in this, it's a community process. These were suggested edits by a user, *approved by user reviewers*, not mods. As the owner of the question you can refuse these edits or rollback to a previous version anytime. // And yes, "robo approved suggested edits" is a problem that SO is trying to address. – Eric Aya Apr 04 '16 at 12:15
  • @EricD Yikes, I meant something like "community mods", just by their privilege. But thanks for the clarification about the whole process, especially about the possibility to refuse the changes! I did a manual rollback (a.k.a. typing). I do fully agreed about the "robo approving", editing/approving should only be done if someone is familiar with the technical terms. – Frederik Winkelsdorf Apr 04 '16 at 12:30

1 Answers1

1

You can try using UIDocumentInteractionController instead. With something like

NSURL * url = [NSURL fileURLWithPath:"file://my.vcard"];
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:url];
[controller presentOpenInMenuFromRect:CGRectMake(0, 0, 300, 300) inView:self.view animated:YES];

This would have option to "Copy to Contacts"

Shchvova
  • 458
  • 5
  • 15