Fix opening URLs via share sheet

This commit is contained in:
Arkadiusz Fal 2022-11-25 19:42:50 +01:00
parent e8515d63e3
commit 779bf190f4

View File

@ -5,19 +5,48 @@ final class ShareViewController: SLComposeServiceViewController {
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) 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 { if let attachments = item.attachments {
for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.url") { tryToOpenItemForPlainTextTypeIdentifier(attachments)
itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { item, _ in tryToOpenItemForUrlTypeIdentifier(attachments)
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)
} private func tryToOpenItemForPlainTextTypeIdentifier(_ attachments: [NSItemProvider]) {
} for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.plain-text") {
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil) 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() { override func didSelectPost() {
openExtensionContextURLs()
extensionContext!.completeRequest(returningItems: [], completionHandler: nil) extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
} }