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

发布于 2024-12-15 13:35:34 字数 294 浏览 3 评论 0 原文

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

我怎样才能做到这一点?

我在应用程序商店中看到一个应用程序可以执行以下操作:

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

姜生凉生 2024-12-22 13:35:34

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

    }
}

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

    }
}
毁梦 2024-12-22 13:35:34

以下是在 iPhone 的联系人 中添加用户信息的代码。

正如我告诉过你的,我对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 

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 
谁人与我共长歌 2024-12-22 13:35:34

我无法帮助您编码,但我在 上一篇文章。您可以尝试使用这种格式来完成您的任务。左侧值始终是静态的。希望你能用这种方式。

仅供参考,我发现谷歌搜索...(支持所有 3 个移动平台)

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

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

不寐倦长更 2024-12-22 13:35:34

func saveContactsfromVCard(vCard : Data)
    {            
            let contactStore = CNContactStore()

            do {

                let saveRequest = CNSaveRequest() // create saveRequests

                let fetchedContacts = try CNContactVCardSerialization.contacts(with: vCard)

                for person in fetchedContacts{

                   let mutableContact = person.mutableCopy() as! CNMutableContact

                    saveRequest.add(mutableContact, toContainerWithIdentifier: nil)    
                   // saveRequest.add(person, toContainerWithIdentifier: nil) // add contacts to saveRequest

                }

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

                let alert1 = UIAlertController(title: "Successful", message: "Contacts Added Successfully!", preferredStyle: UIAlertController.Style.alert)

                alert1.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler:
                    {(alertAction) in
                }))
                self.present(alert1, animated: true, completion: nil)


            } catch  {

                print("Unable to show the new contact")
        }else{

            print("Contacts not added")

        }
    }

func saveContactsfromVCard(vCard : Data)
    {            
            let contactStore = CNContactStore()

            do {

                let saveRequest = CNSaveRequest() // create saveRequests

                let fetchedContacts = try CNContactVCardSerialization.contacts(with: vCard)

                for person in fetchedContacts{

                   let mutableContact = person.mutableCopy() as! CNMutableContact

                    saveRequest.add(mutableContact, toContainerWithIdentifier: nil)    
                   // saveRequest.add(person, toContainerWithIdentifier: nil) // add contacts to saveRequest

                }

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

                let alert1 = UIAlertController(title: "Successful", message: "Contacts Added Successfully!", preferredStyle: UIAlertController.Style.alert)

                alert1.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler:
                    {(alertAction) in
                }))
                self.present(alert1, animated: true, completion: nil)


            } catch  {

                print("Unable to show the new contact")
        }else{

            print("Contacts not added")

        }
    }
川水往事 2024-12-22 13:35:34
    ABMutableMultiValueRef date = ABRecordCopyValue(newPerson, kABPersonDateProperty);
    ABMultiValueAddValueAndLabel(date, dateTextField.text, kABPersonAnniversaryLabel, NULL);            
    ABRecordSetValue(newPerson, kABPersonDateProperty, date,nil);
    ABMutableMultiValueRef date = ABRecordCopyValue(newPerson, kABPersonDateProperty);
    ABMultiValueAddValueAndLabel(date, dateTextField.text, kABPersonAnniversaryLabel, NULL);            
    ABRecordSetValue(newPerson, kABPersonDateProperty, date,nil);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文