【问题标题】:How to save contact info from vCard into iPhone's Contacts App如何将 vCard 中的联系人信息保存到 iPhone 的联系人应用程序中
【发布时间】:2011-12-29 06:14:30
【问题描述】:

在我的 iPhone 应用程序中,当我点击我拥有的 vCard 时,我想将 vCard 保存到我 iPhone 的通讯录中。

我该怎么做?

我在 App Store 上看到过这样的应用:

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

谢谢

【问题讨论】:

  • 我想知道是否可以将其发送到 Gmail 帐户并以这种方式导入?哦,你可能会从apple.stackexchange.com的人那里得到更好的答案
  • @Yzmir Ramirez:这是一个从编程角度而不是 iPhone(硬件)功能角度的问题。所以我认为这是stackoverflow的正确问题。我想通过我的应用程序以编程方式执行此操作。
  • 我的错。我多希望我能帮忙。 vCard 格式并不难解析,但我也想知道是否有内置的东西可以通过编程方式解决这个问题。
  • @ParthBhatt:我不知道 vCard,但我可以帮你在 iPhone 的通讯录中插入信息。如果您需要,请告诉我。
  • 谢天谢地,iOS 7终于允许将 vCard 直接导入通讯录。

标签: iphone objective-c ios cocoa-touch vcf-vcard


【解决方案1】:

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

    }
}

【讨论】:

  • 感谢您对这篇文章的增值..!!
  • @yvzzztrk,仅适用于 9.0?我是 ios 应用程序开发的新手,但我是从 ios 8.0 开发的。什么是 CNContact 的 8.0 等价物?
  • 嗨@alex,你可以使用ABAddressBoook,看看这个answer
  • @alex,当然可以,但我建议你使用 iOS 9 的 CNContact 框架和 if #available(iOS 9.0, *) { /*code */ } 在我的回答中
【解决方案2】:

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

正如我告诉过你的,我对vCard 一无所知,但此代码posted by malinois in their answer here 可能有用:

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 

【讨论】:

  • 感谢 Devang..!!我会看看它..我会尽快回复你.. :)
【解决方案3】:

我不能帮你编码,但是我在previous post找到了vCard的通用格式。您可以尝试使用这种格式来完成您的任务。左侧值始终是静态的。希望你能用那种方式。

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

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

【讨论】:

    【解决方案4】:
    
    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")
    
            }
        }
    

    【讨论】:

      【解决方案5】:
          ABMutableMultiValueRef date = ABRecordCopyValue(newPerson, kABPersonDateProperty);
          ABMultiValueAddValueAndLabel(date, dateTextField.text, kABPersonAnniversaryLabel, NULL);            
          ABRecordSetValue(newPerson, kABPersonDateProperty, date,nil);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多