Once my share extension is loaded, not everytime and not everything is visible for user immediately. The most common is that there you can see image, number of images, and content text. However there are cases where there is a lot more informations.
I know that within SLComposeServiceViewController
there is extensionContext
and its inputItems
property.
Ok, so I stopped the debugger at time, and print out on console some things with following command:
po (extensionContext!.inputItems[0] as! NSExtensionItem).userInfo![NSExtensionItemAttachmentsKey]
there was two NSItemProvider
objects as attachments to first NSExtensionItem
Ok, then I print out the first of attachments:
NSItemProvider
and url from the next one? Can you deliver some code?I suppose we will use
loadItemForTypeIdentifier(_:options:completionHandler:)
but do not know how.
import MobileCoreServices
There is a simple function you can apply to your code:
private func fetchAndSetContentFromContext() { guard let extensionItems = extensionContext?.inputItems as? [NSExtensionItem] else { return } for extensionItem in extensionItems { if let itemProviders = extensionItem.attachments as? [NSItemProvider] { for itemProvider in itemProviders { if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeText as String) { itemProvider.loadItemForTypeIdentifier(kUTTypeText as String, options: nil, completionHandler: { text, error in }) } } } } }
So now you know to use the loadItemForTypeIdentifier(_:options:completionHandler:)
method to load your aspired data.
In your snapshots you wan to get image and url objects.
Let's begin.
guard
let items = extensionContext?.inputItems as? [NSExtensionItem],
let item = items.first,
let attachments = item.attachments
else { return }
var image: UIImage?
var url: URL?
let semaphore = DispatchSemaphore(value: 2)
for attachment in attachments {
if attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String) { item, _ in
image = item as? UIImage
semaphore.signal()
}
}
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String) { item, _ in
url = item as? URL
semaphore.signal()
}
}
}
_ = semaphore.wait(timeout: .now() + 1.0)
print(String(describing: image))
print(String(describing: url))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.