Tusker/OpenInTusker/ActionViewController.swift

106 lines
3.6 KiB
Swift
Raw Permalink Normal View History

2021-05-24 23:07:47 +00:00
//
// ActionViewController.swift
// OpenInTusker
//
// Created by Shadowfacts on 5/23/21.
// Copyright © 2021 Shadowfacts. All rights reserved.
//
import UIKit
import MobileCoreServices
2023-10-20 02:50:20 +00:00
import UniformTypeIdentifiers
2021-05-24 23:07:47 +00:00
class ActionViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
findURLFromWebPage { (components) in
DispatchQueue.main.async {
if let components {
self.searchForURLInApp(components)
} else {
self.findURLItem { (components) in
if let components {
DispatchQueue.main.async {
self.searchForURLInApp(components)
}
}
2021-05-24 23:07:47 +00:00
}
}
}
}
}
private func findURLFromWebPage(completion: @escaping @Sendable (URLComponents?) -> Void) {
2021-05-24 23:07:47 +00:00
for item in extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
2023-10-20 02:50:20 +00:00
guard provider.hasItemConformingToTypeIdentifier(UTType.propertyList.identifier) else {
2021-05-24 23:07:47 +00:00
continue
}
2023-10-20 02:50:20 +00:00
provider.loadItem(forTypeIdentifier: UTType.propertyList.identifier, options: nil) { (result, error) in
2021-05-24 23:07:47 +00:00
guard let result = result as? [String: Any],
let jsResult = result[NSExtensionJavaScriptPreprocessingResultsKey] as? [String: Any],
let urlString = jsResult["activityPubURL"] as? String ?? jsResult["url"] as? String,
let components = URLComponents(string: urlString) else {
completion(nil)
return
}
completion(components)
}
return
}
}
completion(nil)
}
private func findURLItem(completion: @escaping @Sendable (URLComponents?) -> Void) {
2021-05-24 23:07:47 +00:00
for item in extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
2023-10-20 02:50:20 +00:00
guard provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) else {
2021-05-24 23:07:47 +00:00
continue
}
2023-10-20 02:50:20 +00:00
provider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { (result, error) in
2021-05-24 23:07:47 +00:00
guard let result = result as? URL,
let components = URLComponents(url: result, resolvingAgainstBaseURL: false) else {
completion(nil)
return
}
completion(components)
}
return
}
}
completion(nil)
}
private func searchForURLInApp(_ components: URLComponents) {
var components = components
components.scheme = "tusker"
self.openURL(components.url!)
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
@objc private func openURL(_ url: URL) {
var responder: UIResponder = self
while let parent = responder.next {
if let application = parent as? UIApplication {
application.perform(#selector(openURL(_:)), with: url)
break
} else {
responder = parent
}
}
}
@IBAction func done() {
extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
}