I can't provide you with a full, completely working solution, but this doesn't cause any errors:
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
super.loadView()
if let inputItem = extensionContext!.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first {
itemProvider.loadItem(forTypeIdentifier: UTType.url.identifier as String) { [unowned self] (item, error) in
let contentView = ShareView(extensionContext: extensionContext, url: item as! URL)
DispatchQueue.main.async {
let hostingView = UIHostingController(rootView: contentView)
hostingView.view.frame = self.view.frame
self.view.addSubview(hostingView.view)
}
}
}
}
}
}
struct ShareView: View {
var extensionContext: NSExtensionContext?
var url: URL
var body: some View {
VStack{}
.task{
await extractItems()
}
}
func extractItems() async {
try await downloadAndSaveMedia(reelURL: url.absoluteString)
extensionContext?.completeRequest(returningItems: [])
}
}
In your code you get all the attachments here: if let itemProviders = (extensionContext?.inputItems.first as? NSExtensionItem)?.attachments { and you send that array of NSItemProvider to ShareView(), but you then get just the first attachment here (first line of extractItems()): guard let itemProvider = itemProviders.first else { return }, so in my code I'm just using the first attachment.
The distinction is that I do all that stuff in the loadView() method rather than in ShareView().
Might work for you, might not, but I don't think you've tried this. You can probably tidy it up a little.
Topic:
Programming Languages
SubTopic:
Swift
Tags: