如何为联系人创建 iOS 共享扩展

How to create an iOS share extension for contacts

我正在尝试使用以下代码创建 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)")
            }
        }
    }
}

我得到以下输出:

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>)

如果我设置断点,我会看到该项目具有 NSSecureCoding 类型。我的问题是如何将其转换为 CNContact?

您得到的项目是 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)
            }
        }
    }
}

更新了@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)
            }
    })
}

对于动作扩展,您还需要添加 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)
            }
        }
    })
}

当您共享任何联系人时,itemProvider 包含 vCard format 个 CNContact。要访问该联系人,您需要将该数据(类型 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 {}
    }
  })
}

[更新] 更多详情 https://forums.developer.apple.com/message/92115#92115