1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30

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) {
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 {
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 {
@ -15,12 +45,11 @@ final class ShareViewController: SLComposeServiceViewController {
self.open(url: url)
}
}
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
}
}
}
}
private func open(url: URL) {
var responder: UIResponder? = self as UIResponder
@ -45,6 +74,7 @@ final class ShareViewController: SLComposeServiceViewController {
}
override func didSelectPost() {
openExtensionContextURLs()
extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}