我正在使用以下操作在邮件应用程序和我的应用程序之间使用拖放:
class DropFileTypes: NSObject, NSItemProviderReading {
let fileData: Data?
let extensionName: String?
required init(data: Data, typeIdentifier: String) {
fileData = data
if typeIdentifier == (kUTTypePNG as String) {
extensionName = "png"
} else if typeIdentifier == (kUTTypeJPEG as String) {
extensionName = "jpg"
} else if typeIdentifier == (kUTTypeHTML as String) {
extensionName = "html"
} else if typeIdentifier == (kUTTypePDF as String) {
extensionName = "pdf"
} else if typeIdentifier == (kUTTypeRTF as String) {
extensionName = "rtf"
} else if typeIdentifier == (kUTTypeXML as String) {
extensionName = "xml"
} else if typeIdentifier == (kUTTypeMessage as String) {
extensionName = "eml"
} else if typeIdentifier == ("com.microsoft.word.doc" as String) {
extensionName = "doc"
} else if typeIdentifier == ("org.openxmlformats.wordprocessingml.document" as String) {
extensionName = "docx"
} else if typeIdentifier == ("com.microsoft.excel.xls" as String) {
extensionName = "xls"
} else if typeIdentifier == ("com.microsoft.powerpoint.ppt" as String) {
extensionName = "ppt"
} else if typeIdentifier == ("org.openxmlformats.spreadsheetml.sheet" as String) {
extensionName = "xls"
} else if typeIdentifier == ("org.openxmlformats.presentationml.presentation" as String) {
extensionName = "ppt"
} else {
extensionName = "txt"
}
}
static var readableTypeIdentifiersForItemProvider: [String] {
return [kUTTypePNG as String,
kUTTypeJPEG as String,
kUTTypeHTML as String,
kUTTypePDF as String,
kUTTypeRTF as String,
kUTTypeXML as String,
kUTTypeMessage as String,
"com.microsoft.word.doc" as String,
"org.openxmlformats.wordprocessingml.document" as String,
"com.microsoft.excel.xls" as String,
"com.microsoft.powerpoint.ppt" as String,
"org.openxmlformats.spreadsheetml.sheet" as String,
"org.openxmlformats.presentationml.presentation" as String]
}
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
return self.init(data: data, typeIdentifier: typeIdentifier)
}
}
和
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
for item in session.items {
item.itemProvider.loadObject(ofClass: DropFileTypes.self, completionHandler: { (obj, error) in
if let error = error {
print(error.localizedDescription)
return
}
guard let draggedItem = obj as? DropFileTypes else { return }
var attachementMessage: String = "File Attached By App User"
if draggedItem.extensionName == "eml" {
attachementMessage = "Email Attached By App User"
}
DataManager().postAttachment(sStream: draggedItem.fileData!, sExt: draggedItem.extensionName!, fileName: item.itemProvider.suggestedName!, fileDesc: attachementMessage, completion: { (success, int, data, error) in
if let error = error {
print("File uploaded failed \(error.localizedDescription)")
return
}
print("File upload complete")
...
return
}
})
})
})
}
}
这很好用,并且在另一端重构时(桌面应用程序)可以识别每种文件类型。但是,我想知道如果类型为kUTTypeMessage是否可以获取更多信息?如发件人和收件人?
item.itemProvider.suggestedName!
给了我电子邮件的主题行,但是我看不到任何可用的更多信息。我想做到这一点的方法是将数据转换为消息类型并进行询问,但不知道我该怎么做?