如何创建要在共享表中使用的vCard / vcf文件? [英] How to create vCard/vcf file to use in share sheet?
问题描述
我是swift的新手,我发现的方法因我的问题而被弃用。我正在构建一个目录应用程序,我从API中提取联系人数据,而不是从手机的地址簿中提取。
I'm new to swift and methods I am finding are deprecated regarding my issue. I'm building a directory app and I'm pulling contact data from an API, not from the phone's address book.
在iOS中,如果你去你的地址簿,您可以选择一个联系人,然后选择共享联系人,这会打开一个共享表。我想在我的应用程序中使用这个功能。
In iOS, if you go to your address book, you can select a contact and choose 'Share Contact' which brings up a share sheet. I want this exact functionality in my app.
我想想我找到了Share Sheets,这是我的代码:
I think I've got Share Sheets figured out, and here's my code for that:
@IBAction func actShare(sender: AnyObject) {
let activityViewController = UIActivityViewController(activityItems: ["text" as NSString], applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: {})
}
我想将text更改为NSString
作为vCard,因为这是iOS从该地址共享的对象书,对吗?假设我是对的,我想从我自己的应用程序的联系对象创建一个vCard,以便将它分享到适当的应用程序(电子邮件,短信等)。
I want to to change "text" as NSString
to be a vCard, as that is the object that iOS shares from the address book, right? Assuming I'm right, I want to create a vCard from my own app's contact object in order to share it to appropriate apps (email, sms, etc).
如何我可以在Swift中实现吗?如果我错了,请纠正我并告诉我我需要做什么。谢谢。
How can I achieve that in Swift? If I'm wrong, please correct me and show me what I need to do. Thanks.
编辑:好的,这是我的更改。
Okay, here's my changes.
@IBAction func actShare(sender: AnyObject) {
do {
var contactData = NSData()
try contactData = CNContactVCardSerialization.dataWithContacts([createContact()])
let activityViewController = UIActivityViewController(activityItems: [contactData as NSData], applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: {})
} catch {
print("CNContactVCardSerialization cannot save address")
}
和
func createContact() -> CNMutableContact {
let contactCard = CNMutableContact()
contactCard.givenName = "John"
contactCard.familyName = "Doe"
contactCard.emailAddresses = [
CNLabeledValue(label: CNLabelWork, value: "john.doe@email.com")
]
return contactCard
}
然而,当我点击分享按钮并显示我的共享表时,我选择了我想要共享的应用程序并且它不会添加/附加联系人数据如预期。我如何实现这一目标?
However, when I click the share button and it brings up my share sheet, I select the application I want to share to and it doesn't add/attach the contact data as intended. How do I accomplish this?
推荐答案
这里的技巧是使用<$将联系人保存到VCard(.vcf)文件c $ c> CNContactVCardSerialization.dataWithContacts ,然后将文件URL传递给 UIActivityViewController
。活动视图控制器从文件扩展名中检测VCard格式,并显示支持该格式的应用程序(例如消息,邮件,便笺,Airdrop等)
The trick here is to save the contact to a VCard (.vcf) file using CNContactVCardSerialization.dataWithContacts
, then pass the file URL to the UIActivityViewController
. The activity view controller detects the VCard format from the file extension, and shows the apps where the format is supported (e.g. Messages, Mail, Notes, Airdrop, etc)
示例:
@IBAction func buttonTapped(button: UIButton) {
let contact = createContact()
do {
try shareContacts([contact])
}
catch {
// Handle error
}
}
func shareContacts(contacts: [CNContact]) throws {
guard let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first else {
return
}
var filename = NSUUID().UUIDString
// Create a human friendly file name if sharing a single contact.
if let contact = contacts.first where contacts.count == 1 {
if let fullname = CNContactFormatter().stringFromContact(contact) {
filename = fullname.componentsSeparatedByString(" ").joinWithSeparator("")
}
}
let fileURL = directoryURL
.URLByAppendingPathComponent(filename)
.URLByAppendingPathExtension("vcf")
let data = try CNContactVCardSerialization.dataWithContacts(contacts)
print("filename: \(filename)")
print("contact: \(String(data: data, encoding: NSUTF8StringEncoding))")
try data.writeToURL(fileURL, options: [.AtomicWrite])
let activityViewController = UIActivityViewController(
activityItems: [fileURL],
applicationActivities: nil
)
presentViewController(activityViewController, animated: true, completion: {})
}
func createContact() -> CNContact {
// 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"))]
return contact
}
这篇关于如何创建要在共享表中使用的vCard / vcf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!