SwiftUI:如何从 macOS 上的邮件拖放电子邮件 [英] SwiftUI: How to drag and drop an email from Mail on macOS

查看:45
本文介绍了SwiftUI:如何从 macOS 上的邮件拖放电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为@Asperi 对我关于如何拖放问题的回答的跟进

即,这里是完整的表示列表:

com.apple.mail.PasteboardTypeMessageTransfer,com.apple.mail.PasteboardTypeAutomator,com.apple.pasteboard.promised-file-url,dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k,NSPromiseContentsPboardType,com.apple.pasteboard.promised-file-content-type,dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu,苹果文件承诺粘贴板类型,公共网址,CorePasteboardFlavorType 0x75726C20,dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu,Apple URL 粘贴板类型,public.url-名称,CorePasteboardFlavorType 0x75726C6E,public.utf8-纯文本,NSStringPboardType

所以现在您可以从上面加载任何这些类型的数据(当然 Apple 自己的私有数据除外).而且,顺便说一下,该列表可能(而且将)取决于 macOS 版本.

As a follow up on @Asperi's answer of my question on how to drag and drop contacts and, I'd also like to be able to drag and drop email in the same way. Here is my code:

import SwiftUI
import UniformTypeIdentifiers
let uttypes = [String(kUTTypeEmailMessage)]

struct ContentView: View
{
    let dropDelegate = EmailDropDelegate()

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

struct EmailDropDelegate: DropDelegate
{

    func validateDrop(info: DropInfo) -> Bool
    {
        return true
    }
    
    func dropEntered(info: DropInfo)
    {
        print ("Drop Entered")
    }
    
    func performDrop(info: DropInfo) -> Bool
    {
        let items = info.itemProviders(for: uttypes)
        for item in items
        {
            print (item.registeredTypeIdentifiers) // prints []

            item.loadDataRepresentation(forTypeIdentifier: kUTTypeEmailMessage as String, completionHandler: { (data, error) in
                if let data = data
                {
                    print(data)
                }
            })
         }
        return true
    }
}

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

I'm not getting any data back that I can decode.

2020-11-08 09:34:54.877532+0000 DropContact[3856:124769] Cannot find representation conforming to type public.email-message

This feature has been eluding me forever so any help would be very much appreciated.

解决方案

Well... the approach is the same, the only thing is that Apple Mail does not provide kUTTypeEmailMessage UTI representation on drag (copy)

If we register self for generic kUTTypeContent UTI and investigate content of pasteboard on drop mail from Mail, we get:

Ie, here is a complete list of representations:

com.apple.mail.PasteboardTypeMessageTransfer,
com.apple.mail.PasteboardTypeAutomator,
com.apple.pasteboard.promised-file-url,
dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k,
NSPromiseContentsPboardType,
com.apple.pasteboard.promised-file-content-type,
dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu,
Apple files promise pasteboard type,
public.url,
CorePasteboardFlavorType 0x75726C20,
dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu,
Apple URL pasteboard type,
public.url-name,
CorePasteboardFlavorType 0x75726C6E,
public.utf8-plain-text,
NSStringPboardType

so now you can load data of any of those types from above (except of course Apple's own privates). And, by the way, that list might (and rather will) depend on macOS version.

这篇关于SwiftUI:如何从 macOS 上的邮件拖放电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆