From 779bf190f40a9b2d47ee2e139a962c6b8312e2b6 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 25 Nov 2022 19:42:50 +0100 Subject: [PATCH] Fix opening URLs via share sheet --- Open in Yattee/ShareViewController.swift | 50 +++++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/Open in Yattee/ShareViewController.swift b/Open in Yattee/ShareViewController.swift index c4039112..d44a9b1a 100644 --- a/Open in Yattee/ShareViewController.swift +++ b/Open in Yattee/ShareViewController.swift @@ -5,19 +5,48 @@ final class ShareViewController: SLComposeServiceViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - for item in extensionContext!.inputItems as! [NSExtensionItem] { + openExtensionContextURLs() + + extensionContext?.completeRequest(returningItems: [], completionHandler: nil) + } + + private func openExtensionContextURLs() { + for item in extensionContext?.inputItems as! [NSExtensionItem] { if let attachments = item.attachments { - for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.url") { - itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { item, _ in - if let url = (item as? NSURL), let absoluteURL = url.absoluteURL { - URLBookmarkModel.shared.saveBookmark(absoluteURL) - if let url = URL(string: "yattee://\(absoluteURL.absoluteString)") { - self.open(url: url) - } - } - self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil) + tryToOpenItemForPlainTextTypeIdentifier(attachments) + tryToOpenItemForUrlTypeIdentifier(attachments) + } + } + } + + private func tryToOpenItemForPlainTextTypeIdentifier(_ attachments: [NSItemProvider]) { + for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.plain-text") { + itemProvider.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { item, _ in + if let url = (item as? String), + let absoluteURL = URL(string: url)?.absoluteURL + { + URLBookmarkModel.shared.saveBookmark(absoluteURL) + if let url = URL(string: "yattee://\(absoluteURL.absoluteString)") { + self.open(url: url) } } + + self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) + } + } + } + + private func tryToOpenItemForUrlTypeIdentifier(_ attachments: [NSItemProvider]) { + for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.url") { + itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { item, _ in + if let url = (item as? NSURL), let absoluteURL = url.absoluteURL { + URLBookmarkModel.shared.saveBookmark(absoluteURL) + if let url = URL(string: "yattee://\(absoluteURL.absoluteString)") { + self.open(url: url) + } + } + + self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil) } } } @@ -45,6 +74,7 @@ final class ShareViewController: SLComposeServiceViewController { } override func didSelectPost() { + openExtensionContextURLs() extensionContext!.completeRequest(returningItems: [], completionHandler: nil) }