【问题标题】:SwiftUI: How to drag and drop a contact from Contacts on macOSSwiftUI:如何从 macOS 上的联系人中拖放联系人
【发布时间】:2021-02-11 17:56:21
【问题描述】:

我正在尝试将联系人从联系人拖到我的应用程序中。

这是我更新的代码:

import SwiftUI
import UniformTypeIdentifiers
let uttypes = [UTType.contact, UTType.emailMessage]

struct ContentView: View
{
    let dropDelegate = ContactDropDelegate()

    var body: some View
    {
        VStack
        {
            Text("Drag your contact here!")
            .padding(20)
        }
        .onDrop(of: uttypes, delegate: dropDelegate)
    }
}

struct ContactDropDelegate: DropDelegate
{

    func validateDrop(info: DropInfo) -> Bool
    {
        return true
    }
    
    func dropEntered(info: DropInfo)
    {
        print ("Drop Entered")
    }
    
    func performDrop(info: DropInfo) -> Bool
    {
        print ("performDrop")
        NSSound(named: "Submarine")?.play()
        
        print (info)
        print ("Has UniformTypeIdentifiers:", info.hasItemsConforming(to: uttypes))
        
        let items = info.itemProviders(for: uttypes)
        print ("Number of items:",items.count)
        print (items)
        for item in items
        {
            print ("item:", item)
                item.loadDataRepresentation(forTypeIdentifier: kUTTypeVCard as String, completionHandler: { (data, error) in
                if let data = data
                {
                    print ("contact")
                }
            })
         }
        return true
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

控制台输出如下:

Drop Entered
performDrop
2020-11-07 10:00:37.150359+0000 DropContact[9653:434367] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600003fd9b80> F8BB1C28-BAE8-11D6-9C31-00039315CD46
DropInfo(platformDropInfo: SwiftUI.(unknown context at $7fff423616a8).DropInfo_Mac(location: (85.0618896484375, 22.804290771484375), pasteboard: <NSPasteboard: 0x600001ba6ca0>))
Has UniformTypeIdentifiers: true
Number of items: 1
[<NSItemProvider: 0x6000014a73a0> {types = (
)}]
item: <NSItemProvider: 0x6000014a73a0> {types = (
)}
2020-11-07 10:00:37.427867+0000 DropContact[9653:434367] Cannot find representation conforming to type public.vcard

所以它知道 drop 中有一个联系人,但我如何访问它?当我尝试将其取出时,它似乎为零。

【问题讨论】:

  • 这是在 macOS 上吗?
  • 是的,它在 macOS 上。
  • 我已经更新了代码以包含查看问题所需的所有内容并提供完整代码。我现在也在 macOS 11 中使用 UTI。

标签: macos swiftui drag-and-drop contacts


【解决方案1】:

订阅和加载数据类型应该相同,所以这里是一个工作解决方案的演示。使用 Xcode 12.1 / macOS 10.15.7 测试

let uttypes = [String(kUTTypeVCard), String(kUTTypeEmailMessage)]

struct ContentView: View
{
    let dropDelegate = ContactDropDelegate()

    var body: some View
    {
        VStack
        {
            Text("Drag your contact here!")
            .padding(20)
        }
        .onDrop(of: uttypes, delegate: dropDelegate) // << kUTTypeVCard !!
    }
}

import Contacts

struct ContactDropDelegate: DropDelegate
{

    func validateDrop(info: DropInfo) -> Bool
    {
        return true
    }
    
    func dropEntered(info: DropInfo)
    {
        print ("Drop Entered")
    }
    
    func performDrop(info: DropInfo) -> Bool
    {
        print ("performDrop")
        NSSound(named: "Submarine")?.play()
        
        print (info)
        print ("Has UniformTypeIdentifiers:", info.hasItemsConforming(to: uttypes))
        
        let items = info.itemProviders(for: uttypes)
        print ("Number of items:",items.count)
        print (items)

        for item in items
        {
            item.loadDataRepresentation(forTypeIdentifier: kUTTypeVCard as String, completionHandler: { (data, error) in
                if let data = data, 
                   let contact = try? CNContactVCardSerialization.contacts(with: data).first
                {
                    print("Contact: \(contact.givenName)")
                }
            })
         }
        return true
    }
}

【讨论】:

  • 效果很好。如果您可以扩展该答案以包括从 Apple Mail 拖放电子邮件,我将很乐意再支付一笔赏金!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多