SwiftUI: How to drag and drop a contact from Contacts on macOS

﹥>﹥吖頭↗ 提交于 2021-01-03 12:27:35

问题


I'm trying to drag a contact from Contacts into my application.

Here is my updated code:

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

Output in the console is as follows:

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

So it knows there is a contact in the drop, but how do I access it? It seems to be nil when I try to get it out.


回答1:


The subscription & load data types should be the same, so here is a demo of worked solution. Tested with 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
    }
}


来源:https://stackoverflow.com/questions/64588583/swiftui-how-to-drag-and-drop-a-contact-from-contacts-on-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!