How to save contact info from vCard into iPhone

2019-03-28 06:26发布

In my iPhone app, I want to save the vCards into my iPhone's Contacts when I click onto the vCard which I have.

How can I do that?

I have seen an app on app store which does this:

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

Thanks

4条回答
叼着烟拽天下
2楼-- · 2019-03-28 06:30

Following is the code for adding user information in iPhone's Contact.

As I told you that i dont know anything about the vCard, but this code posted by malinois in their answer here might be of use:

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 
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-28 06:30
    ABMutableMultiValueRef date = ABRecordCopyValue(newPerson, kABPersonDateProperty);
    ABMultiValueAddValueAndLabel(date, dateTextField.text, kABPersonAnniversaryLabel, NULL);            
    ABRecordSetValue(newPerson, kABPersonDateProperty, date,nil);
查看更多
该账号已被封号
4楼-- · 2019-03-28 06:32

I can't help you for coding, but I have found the common format of vCard in previous post. You can try to achieve your task using this format. The left side values are always static. Hope if you can use that way.

For just reference, i found googling... (support for all 3 mobile platforms)

http://learnyii.blogspot.com/2011/04/vcard-qr-code-iphone-android-blackberry.html

查看更多
聊天终结者
5楼-- · 2019-03-28 06:51

New Contacts Framework introduced with iOS9, saving vCard data into iPhone's contacts is much easier and simpler with Swift4.

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.") //

    }
}
查看更多
登录 后发表回答