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,8 +5,38 @@ 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 {
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") { for itemProvider in attachments where itemProvider.hasItemConformingToTypeIdentifier("public.url") {
itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { item, _ in itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { item, _ in
if let url = (item as? NSURL), let absoluteURL = url.absoluteURL { if let url = (item as? NSURL), let absoluteURL = url.absoluteURL {
@ -15,12 +45,11 @@ final class ShareViewController: SLComposeServiceViewController {
self.open(url: url) self.open(url: url)
} }
} }
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil) self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
} }
} }
} }
}
}
private func open(url: URL) { private func open(url: URL) {
var responder: UIResponder? = self as UIResponder var responder: UIResponder? = self as UIResponder
@ -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)
} }