如何将联系信息从vCard保存到iPhone的联系人应用程序中

Par*_*att 7 iphone cocoa-touch vcf-vcard objective-c ios

在我的iPhone应用程序中,当我点击我拥有的vCard时,我想将vCards保存到我的iPhone的联系人中.

我怎样才能做到这一点?

我在应用商店看到了一个应用商店的应用:

http://itunes.apple.com/us/app/read-vcard/id402216831?mt=8

谢谢

小智 5

iOS9引入的新联系人框架,使用Swift4将vCard数据保存到iPhone的联系人中更容易,更简单.

import Contacts

    func saveVCardContacts (vCard : Data) { // assuming you have alreade permission to acces contacts

    if #available(iOS 9.0, *) {

        let contactStore = CNContactStore()

        do {

            let saveRequest = CNSaveRequest() // create saveRequests

            let contacts = try CNContactVCardSerialization.contacts(with: vCard) // get contacts array from vCard

            for person in contacts{

                saveRequest.add(person as! CNMutableContact, toContainerWithIdentifier: nil) // add contacts to saveRequest

            }

            try contactStore.execute(saveRequest) // save to contacts

        } catch  {

            print("Unable to show the new contact") // something went wrong

        }

    }else{

        print("CNContact not supported.") //

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您对此帖的增值.. !! (2认同)

Dev*_*ang 3

以下是添加用户信息的代码iPhone's Contact

正如我告诉过你的,我对 不了解vCard,但是malinois 在他们的答案中发布的这段代码可能有用:

ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record 
ABRecordRef person = ABPersonCreate(); // create a person  

NSString *phone = @"0123456789"; // the phone number to add  

//Phone number is a list of phone number, so create a multivalue  
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty); 
ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phone,kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); // first name of the new person 
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); // his last name 
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property 
ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

ABRecordRef group = ABGroupCreate(); //create a group 
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name 
ABGroupAddMember(group, person, &error); // add the person to the group         
ABAddressBookAddRecord(addressBook, group, &error); // add the group   

ABAddressBookSave(addressBook, nil); //save the record  

CFRelease(person); // relase the ABRecordRef  variable 
Run Code Online (Sandbox Code Playgroud)