NSItemProvider
概要
code:swift
import MobileCoreServices
class MyViewController: UIViewController {
func checkItem() {
guard let item = self.extensionContext?.inputItems.first as? NSExtensionItem else {
fatalError()
}
guard let attachment = item.attachments?.first else {
fatalError()
}
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { item, _ in
guard let url = item as? URL else { return }
// 何か処理
}
}
if attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) { data, _ in
switch data {
case let image as UIImage:
// 何か処理
case let data as Data:
// 何か処理
default:
// 何か処理
}
}
}
}
}
などを定義できる。
code:swift
class MyModel: NSObject {
var name: String
var thumbnail: UIImage?
}
extension MyModel: NSItemProviderWriting {
// MARK: - NSItemProviderWriting
static var writableTypeIdentifiersForItemProvider: String = [ kUTTypeVCard as String,
kUTTypeUTF8PlainText as String
]
func loadData(withTypeIdentifier typeIdentifier: String,
forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
if typeIdentifier == kUTTypeVCard as String {
completionHandler(createVCard(), nil)
} else if typeIdentifier == kUTTypeUTF8PlainText as String {
completionHandler(name.data(using: .utf8), nil)
} else {
completionHandler(nil, ContactCardError.invalidTypeIdentifier)
}
return nil
}
}
プレビュー画像
複数フォーマットのサポート