I am trying to create an iOS share extension to share contacts using the following code:
我正在尝试使用以下代码创建iOS共享扩展以共享联系人:
let contactType = "public.vcard"
override func viewDidLoad() {
let items = extensionContext?.inputItems
var itemProvider: NSItemProvider?
if items != nil && items!.isEmpty == false {
let item = items![0] as! NSExtensionItem
if let attachments = item.attachments {
if !attachments.isEmpty {
itemProvider = attachments[0] as? NSItemProvider
}
}
}
if itemProvider?.hasItemConformingToTypeIdentifier(contactType) == true {
itemProvider?.loadItemForTypeIdentifier(contactType, options: nil) { item, error in
if error == nil {
print("item: \(item)")
}
}
}
}
I am getting the following output:
我得到以下输出:
item: Optional(<42454749 4e3a5643 4152440d 0a564552 53494f4e 3a332e30 0d0a5052 4f444944 3a2d2f2f 4170706c 6520496e 632e2f2f 6950686f 6e65204f 5320392e 322f2f45 4e0d0a4e 3a42656e 6e657474 3b436872 69733b3b 3b0d0a46 4e3a2043 68726973 20204265 6e6e6574 74200d0a 54454c3b 74797065 3d484f4d 453b7479 70653d56 4f494345 3b747970 653d7072 65663a28 36313529 c2a03432 352d3637 31390d0a 454e443a 56434152 440d0a>)
If i set a breakpoint I see that item has a type of NSSecureCoding. My question is how do I turn this into a CNContact?
如果我设置断点,我看到该项具有NSSecureCoding类型。我的问题是如何将其转换为CNContact?
3 个解决方案
#1
4
The item you are getting is of type NSData
. You have to transform it to String
to get the vCard String:
您获得的项目是NSData类型。您必须将其转换为String以获取vCard字符串:
if itemProvider?.hasItemConformingToTypeIdentifier(contactType) == true {
itemProvider?.loadItemForTypeIdentifier(contactType, options: nil) { item, error in
if error == nil {
if let data = item as? NSData, let vCardString = String(data: data, encoding: NSUTF8StringEncoding) {
print(vCardString)
}
}
}
}
#2
2
When you are sharing any contacts, itemProvider
contains vCard format of CNContact. To access that contact, you need to convert that data (type NSSecureCoding) to CNContact
.
当您共享任何联系人时,itemProvider包含CNContact的vCard格式。要访问该联系人,您需要将该数据(类型为NSSecureCoding)转换为CNContact。
let uttypeContact = kUTTypeContact as String
if itemProvider.hasItemConformingToTypeIdentifier(uttypeContact) {
itemProvider.loadItemForTypeIdentifier(uttypeContact, options: nil, completionHandler: { (data, error) in
if error == nil {
do {
if let data = data as? NSData {
let contact = try CNContactVCardSerialization.contactsWithData(data)
print(contact)
}
} catch {}
}
})
}
[Update] For more details https://forums.developer.apple.com/message/92115#92115
[更新]有关详细信息,请访问https://forums.developer.apple.com/message/92115#92115
#3
0
Updated answer of @joern to Swift 3:
更新了@joern到Swift 3的答案:
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeContact as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeContact as String, options: nil, completionHandler: { (contact, error) in
if let contactData = contact as? Data, let vCardString = String(data: contactData, encoding: .utf8) {
print(vCardString)
}
})
}
Also for an Action Extension you need to add the OperationQueue
:
对于Action Extension,您还需要添加OperationQueue:
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeContact as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeContact as String, options: nil, completionHandler: { (contact, error) in
OperationQueue.main.addOperation {
if let contactData = contact as? Data, let vCardString = String(data: contactData, encoding: .utf8) {
print(vCardString)
}
}
})
}
#1
4
The item you are getting is of type NSData
. You have to transform it to String
to get the vCard String:
您获得的项目是NSData类型。您必须将其转换为String以获取vCard字符串:
if itemProvider?.hasItemConformingToTypeIdentifier(contactType) == true {
itemProvider?.loadItemForTypeIdentifier(contactType, options: nil) { item, error in
if error == nil {
if let data = item as? NSData, let vCardString = String(data: data, encoding: NSUTF8StringEncoding) {
print(vCardString)
}
}
}
}
#2
2
When you are sharing any contacts, itemProvider
contains vCard format of CNContact. To access that contact, you need to convert that data (type NSSecureCoding) to CNContact
.
当您共享任何联系人时,itemProvider包含CNContact的vCard格式。要访问该联系人,您需要将该数据(类型为NSSecureCoding)转换为CNContact。
let uttypeContact = kUTTypeContact as String
if itemProvider.hasItemConformingToTypeIdentifier(uttypeContact) {
itemProvider.loadItemForTypeIdentifier(uttypeContact, options: nil, completionHandler: { (data, error) in
if error == nil {
do {
if let data = data as? NSData {
let contact = try CNContactVCardSerialization.contactsWithData(data)
print(contact)
}
} catch {}
}
})
}
[Update] For more details https://forums.developer.apple.com/message/92115#92115
[更新]有关详细信息,请访问https://forums.developer.apple.com/message/92115#92115
#3
0
Updated answer of @joern to Swift 3:
更新了@joern到Swift 3的答案:
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeContact as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeContact as String, options: nil, completionHandler: { (contact, error) in
if let contactData = contact as? Data, let vCardString = String(data: contactData, encoding: .utf8) {
print(vCardString)
}
})
}
Also for an Action Extension you need to add the OperationQueue
:
对于Action Extension,您还需要添加OperationQueue:
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeContact as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeContact as String, options: nil, completionHandler: { (contact, error) in
OperationQueue.main.addOperation {
if let contactData = contact as? Data, let vCardString = String(data: contactData, encoding: .utf8) {
print(vCardString)
}
}
})
}