首页 > 解决方案 > 在 SwiftUI 中使用 DropDelegate 获取项目的 URL

问题描述

我有一个符合DropDelegate并正常工作的 SwiftUI 视图,以便从macOS和中的其他应用程序接收图像iPadOS,但是两个操作系统中的行为完全不同:

struct ContentView: View { 
    var body: some View {
        MyView { ... }
            .onDrop(of: [.fileURL, .image], delegate: self)
    }
}

实现DropDelegate是:

extension ContentView: DropDelegate { 
   func performDrop(info: DropInfo) -> Bool {
      // Debug implementation
      if info.hasItemsConforming(to: [.image]) { 
         print("CONTAINS AN IMAGE")
      } else if info.hasItemsConforming(to: [.fileURL]) { 
         print("CONTAINS AN URL")
      }
   }
}

如果我在macOS输出是CONTAINS AN URL,如果我在iPadOS输出是CONTAINS AN IMAGE

问题是我需要访问creationDate源 URL 的一些元数据(如 ),creationDate如果输出是UIImage. 如何通过 DropDelegate 访问 iPadOS/iOS 上的源 URL?或者......我怎样才能获得由UIImage提供的创建日期DropDelegate

Xcode 13 | macOS 12 | iPadOS 15 | 斯威夫特 5.5 | SwiftUI 3.0

标签: iosswiftxcodeswiftuiipados

解决方案


推荐阅读