[SwiftUI] SwiftUI Drag&Drop implementation(Vol. 3:use own UTI in Drag&Drop)

SwiftUI

Last 2 posts, implemented drag & drop for String type and my own class with using public.utf8-plain-text. In this post, let’s implement drag and drop with using my own UTI.

Environment

This post is checked with following environment

  • macOS Big Sur 11.2.3
  • Xcode 12.4
SwiftUI[SwiftUI] SwiftUI で実装する Drag&Drop (その1: String を Drag&Drop) SwiftUI[SwiftUI] SwiftUI で実装する Drag&Drop (その2:クラスオブジェクト を Drag&Drop)

define own UTI type

First let’s define own UTI type with using extension.

example

Usually reverese DNS is recommended for new UTIs.

There are two options for defining UTI. one is “ExportedAs”, another is “ImportedAs”.
For your own UTI, need to use “ExportedAs”. this means you are the owner of the UTI.

“ImportedAs” will be used for “public.text” for example. that means another company/person owns the UTI, but your app can handle the type.

for detail, please refer to the apple doc here.

Define UTI type again (in Info.plist)

Because OS also needs to understand the UTIs which will be handled by application.

You need to put UTI information in Info.plist.

Conveniently Xcode provide the GUI for maintaining the UTI.

UTI definition in Xcode
UTI definition in Xcode
  1. Click target (for the application)
  2. Click Info
  3. define identifier (com.mycompany.myowntype in this case)
  4. need to conforms to public.item (or subclass of it) for Drag and drop (public.item in this case)

Followings are XML expression, so you can edit Info.plist manually.

コード

Note1: Xcode will check the definition at compile time

In case you define your own UTI only in code, Xcode will give the warning.

Note2: UTI needs to conform physical hierarchy for Drag&Drop

If want to use in Drag and Drop, new UTI needs to conform to one of the physical hierarchy type (like public.item).

Note3: conformation information should be defined in Info.plist

Above conformance information should be defined in Info.plist (for Drag&Drop). Otherwise does not work.

Use own UTI type

Just use it as one of UTType.

Use new own UTI type in MyDropData class

Here is the code.

MyDropData

just use own UTI instead of kUTTypeUTF8PlainText.

Note: In some cases, may need to use own type as String. we can use identifier static method of UTType for such cases.

Use new own UTI in SwiftUI

As you see, basically we don’t need to change the code. because MyDropData class handles type information as well.

ContentView

code

whole code

Summary:how to handle my own UTI in Drag&Drop

how to handle my own UTI in Drag&Drop
  • need to define UTI in code. use “ExportedAs” in case you’re the owner of the UTI. if not, use “ImportedAs” instead.
  • need to define UTI in Info.plist too.
  • for using in Drag&Drop, the UTI needs to conform one of the physical type (i.e. descendants of public.item)

Thank you for reading. Hope you enjoy this post.
please feel free to contact to twitter.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です