IOS Drag&;Drop获取已删除文件的文件名

IOS Drag&;Drop获取已删除文件的文件名,ios,swift,Ios,Swift,我正在使用IOS的Drop功能将pdf文件导入我的应用程序。有没有办法获取被删除文件的文件名 我在UIDropSession或UIDropInteraction中找不到任何内容 谢谢 如果是UITableView,请签出UITableViewDropItem.dragItem.itemProvider.suggestedName。它包含不带扩展名的文件名 然后使用UITableViewDropCoordinator.session.loadObjects()异步加载文件并重建其文件名和扩展名。可

我正在使用IOS的Drop功能将pdf文件导入我的应用程序。有没有办法获取被删除文件的文件名

我在
UIDropSession
UIDropInteraction
中找不到任何内容


谢谢

如果是
UITableView
,请签出
UITableViewDropItem.dragItem.itemProvider.suggestedName
。它包含不带扩展名的文件名

然后使用
UITableViewDropCoordinator.session.loadObjects()
异步加载文件并重建其文件名和扩展名。可以通过将UTI从
nsistemProviderReading.object()
映射到文件扩展名来重构扩展名

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
    guard let indexPath = coordinator.destinationIndexPath else { return }

    for item in coordinator.items {
        guard let fileName = item.dragItem.itemProvider.suggestedName else { continue }

        let placeHolder = UITableViewDropPlaceholder(insertionIndexPath: indexPath, reuseIdentifier: "Placeholder", rowHeight: 44)
        placeHolder.cellUpdateHandler = { (cell) in
            // Configure cell
        }
        let context = coordinator.drop(item.dragItem, to: placeHolder)

        // Load file contents
        item.dragItem.itemProvider.loadObject(ofClass: PlaylistItemReader.self) { (reader, error) in

            DispatchQueue.main.async {
                guard let reader = reader as? PlaylistItemReader else {
                    context.deletePlaceholder()
                    return
                }

                // Convert file type to actual extension
                guard let fileExtension = PlaylistItem.typeToExtension[reader.fileType] else {
                    context.deletePlaceholder()
                    return
                }
                // fileExtension is something like "m4a" or "mp3"

                print("File '\(fileName).\(fileExtension)'")

                context.commitInsertion(dataSourceUpdates: { (insertionIndexPath) in
                    // update model
                })
            } // async

        } // load
    } // items
}

注意:
playlitemreader
是我的自定义类,它实现了
NSItemProviderReading
协议。

如果是
UITableView
,请签出
UITableViewDropItem.dragItem.itemProvider.suggestedName
。它包含不带扩展名的文件名

然后使用
UITableViewDropCoordinator.session.loadObjects()
异步加载文件并重建其文件名和扩展名。可以通过将UTI从
nsistemProviderReading.object()
映射到文件扩展名来重构扩展名

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
    guard let indexPath = coordinator.destinationIndexPath else { return }

    for item in coordinator.items {
        guard let fileName = item.dragItem.itemProvider.suggestedName else { continue }

        let placeHolder = UITableViewDropPlaceholder(insertionIndexPath: indexPath, reuseIdentifier: "Placeholder", rowHeight: 44)
        placeHolder.cellUpdateHandler = { (cell) in
            // Configure cell
        }
        let context = coordinator.drop(item.dragItem, to: placeHolder)

        // Load file contents
        item.dragItem.itemProvider.loadObject(ofClass: PlaylistItemReader.self) { (reader, error) in

            DispatchQueue.main.async {
                guard let reader = reader as? PlaylistItemReader else {
                    context.deletePlaceholder()
                    return
                }

                // Convert file type to actual extension
                guard let fileExtension = PlaylistItem.typeToExtension[reader.fileType] else {
                    context.deletePlaceholder()
                    return
                }
                // fileExtension is something like "m4a" or "mp3"

                print("File '\(fileName).\(fileExtension)'")

                context.commitInsertion(dataSourceUpdates: { (insertionIndexPath) in
                    // update model
                })
            } // async

        } // load
    } // items
}

注意:
playlitemreader
是我的自定义类,它实现了
NSItemProviderReading
协议。

您可以从
UIDropSession
获取名称,例如
session.items.first?.itemProvider.suggestedName

您可以从
UIDropSession
获取名称,例如,
会话.items.first?.itemProvider.suggestedName

我假设它是一个外部拖放?你要扔的东西到底是什么?文件的URL?或者别的什么?它是一个外部pdf文件我想它是一个外部文件?你要扔的东西到底是什么?文件的URL?或者别的什么?这是一个外部pdf文件。我使用了
UTTypeCopyPreferredTagWithClass
(类型标识符为CFString,kUTTagClassFilenameExtension)从UTI.Works中获取文件扩展名。我使用
UTTypeCopyPreferredTagWithClass
(typeIdentifier为CFString,kUTTagClassFilenameExtension)从UTI获取文件扩展名。