以编程方式将 iPhone 个联系人导出到 .vcf 文件
Export iPhone contacts to .vcf file programmatically
我想在联系人应用程序中选择iPhone个联系人,生成.vcf文件,将选择的联系人写入此文件并发送到服务器。
据我所知,在iOS9中,通讯录的许多功能都被贬低了,所以谁能帮我正确地写这段代码。
您需要的一般部件是:
- 用于访问 phone 联系人的 Contacts 框架。
- 使用内置视图控制器访问联系人的 ContactsUI 框架。
- 使用 CNContactVCardSerialization.dataWithContacts 将
CNContact
数据编码为 VCard 格式。
- 使用data.writeToURL将数据写入文件。
- 使用NSURLSession上传数据到服务器。
下面的示例回答了将联系人保存为 VCard 格式的问题。
import Contacts
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
contact.imageData = NSData() // The profile picture as a NSData object
contact.givenName = "John"
contact.familyName = "Appleseed"
let homeEmail = CNLabeledValue(label:CNLabelHome, value:"john@example.com")
let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com")
contact.emailAddresses = [homeEmail, workEmail]
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Cupertino"
homeAddress.state = "CA"
homeAddress.postalCode = "95014"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]
let birthday = NSDateComponents()
birthday.day = 1
birthday.month = 4
birthday.year = 1988 // You can omit the year value for a yearless birthday
contact.birthday = birthday
let data = try CNContactVCardSerialization.dataWithContacts([contact])
let s = String(data: data, encoding: NSUTF8StringEncoding)
if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first {
let fileURL = directoryURL.URLByAppendingPathComponent("john.appleseed").URLByAppendingPathExtension("vcf")
try data.writeToURL(fileURL, options: [.AtomicWrite])
}
我想在联系人应用程序中选择iPhone个联系人,生成.vcf文件,将选择的联系人写入此文件并发送到服务器。
据我所知,在iOS9中,通讯录的许多功能都被贬低了,所以谁能帮我正确地写这段代码。
您需要的一般部件是:
- 用于访问 phone 联系人的 Contacts 框架。
- 使用内置视图控制器访问联系人的 ContactsUI 框架。
- 使用 CNContactVCardSerialization.dataWithContacts 将
CNContact
数据编码为 VCard 格式。 - 使用data.writeToURL将数据写入文件。
- 使用NSURLSession上传数据到服务器。
下面的示例回答了将联系人保存为 VCard 格式的问题。
import Contacts
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
contact.imageData = NSData() // The profile picture as a NSData object
contact.givenName = "John"
contact.familyName = "Appleseed"
let homeEmail = CNLabeledValue(label:CNLabelHome, value:"john@example.com")
let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com")
contact.emailAddresses = [homeEmail, workEmail]
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Cupertino"
homeAddress.state = "CA"
homeAddress.postalCode = "95014"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]
let birthday = NSDateComponents()
birthday.day = 1
birthday.month = 4
birthday.year = 1988 // You can omit the year value for a yearless birthday
contact.birthday = birthday
let data = try CNContactVCardSerialization.dataWithContacts([contact])
let s = String(data: data, encoding: NSUTF8StringEncoding)
if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first {
let fileURL = directoryURL.URLByAppendingPathComponent("john.appleseed").URLByAppendingPathExtension("vcf")
try data.writeToURL(fileURL, options: [.AtomicWrite])
}