Sal*_*mua 4 vcf abaddressbook ios swift ios9
我想在通讯录应用程序中选择iPhone通讯录,生成.vcf文件,在该文件中写入所选的通讯录并发送到服务器。
正如我在iOS 9中所知道的,通讯录的许多功能已被弃用,因此任何人都可以帮助我以正确的方式编写此代码。
您将需要的一般部件是:
CNContact数据编码为VCard格式。下面是一个示例,回答了将联系人保存为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])
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1750 次 |
| 最近记录: |