iOS11如何处理在自定义视图中删除的自定义文件

时间:2017-12-05 17:51:36

标签: ios swift

我一直在搞乱iOS11的掉落功能。我发现很多标准UIImage等的例子,但没有自定义文件。

drop部分工作得很好,我用它来删除自定义文件并将其导入我的应用程序。

问题是我只获取此文件的数据,我想获得其名称和扩展名。

我不知道我处理自定义文件的方式是否应该如何获取此信息。如果是这样,您如何从文件中获取此信息?

我有一个符合DropFile协议的NSItemProviderReading类。

class DropFile : NSObject, NSItemProviderReading {
  let fileData:Data?

  required init(data:Data, typeIdentifier:String) {
      fileData = data
  }

  static var readableTypeIdentifiersForItemProvider: [String] {
      var documentTypeArray: [String] = []

      for ext in FileImportProcessHelper.sharedInstance.getImportDocumentType() {
          let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext as CFString, nil)
          documentTypeArray.append((UTI?.takeRetainedValue() as String?)!)
      }
      return documentTypeArray

  }

  static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
      return self.init(data: data, typeIdentifier: typeIdentifier)
  }
}

这是符合ViewController

UIDropInteractionDelegate
// MARK: Handling Drop

@available(iOS 11.0, *)
// We refuse the dropped items based on their UTI
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {

    var documentTypeArray: [String] = []

    for ext in FileImportProcessHelper.sharedInstance.getImportDocumentType() {
        let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext as CFString, nil)
        documentTypeArray.append(UTI?.takeRetainedValue() as! String)
    }
    return session.hasItemsConforming(toTypeIdentifiers: documentTypeArray) && session.items.count == 1
}

@available(iOS 11.0, *)
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
    // Copy file from source app
    return UIDropProposal(operation: .copy)
}

@available(iOS 11.0, *)
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
    // Consume drag items
    session.loadObjects(ofClass: DropFile.self) { items in
        if let fileItems = items as? [DropFile] {
            DragnDropManager.sharedManager.createDropObject(fileItems: fileItems)
        }
    }
}

然后我如何写我的文件。

func createDropObject(fileItems: [DropFile]) {
        let documentsPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let inbox = URL(fileURLWithPath: documentsPathString!).appendingPathComponent("UserDropInbox/")

        do {
            try FileManager.default.createDirectory(atPath: inbox.path, withIntermediateDirectories: true, attributes: nil)
        } catch let error as NSError {
            NSLog("Unable to create directory \(error.debugDescription)")
        }

        for file in fileItems {
            do {
                let dropFilePath = inbox.appendingPathComponent("File").appendingPathExtension("pdf")
                try file.fileData?.write(to:dropFilePath)
            } catch {
                NSLog(error as! String)
            }
        }

        FileImportInboxManager.shared.hasReceivedFiles = true;
 }

1 个答案:

答案 0 :(得分:3)

我承认,这不是直截了当的:

  • 您可以列出UIDragItem UIDropSession成员中用户拖动的所有items
  • 其中每个项目都有NSItemProvider itemProvider成员
  • 此商品提供者有一个可选的String? suggestedName成员,可能不是nil

打印所有会话项目的简单循环建议名称:

for item in session.items {
   if let name = item.itemProvider.suggestedName {
      print(name)
   }
}

实际上,当文件来自文件应用程序时,它将提供没有扩展名的文件名,但是如果您可以轻松访问其UTI,则可以附加一个与原始文件相近的扩展名({{1例如,而不是jpeg