|
| 1 | +// |
| 2 | +// ActionViewController.swift |
| 3 | +// Action Assistant |
| 4 | +// |
| 5 | +// Created by Marino Faggiana on 14/05/2026. |
| 6 | +// Copyright © 2026 Marino Faggiana. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import NextcloudKit |
| 11 | +import UniformTypeIdentifiers |
| 12 | + |
| 13 | +final class ActionViewController: UIViewController { |
| 14 | + override func viewDidLoad() { |
| 15 | + super.viewDidLoad() |
| 16 | + |
| 17 | + view.isHidden = true |
| 18 | + view.alpha = 0 |
| 19 | + view.backgroundColor = .clear |
| 20 | + preferredContentSize = .zero |
| 21 | + |
| 22 | + Task { |
| 23 | + await handleAction() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private func handleAction() async { |
| 28 | + guard let text = await loadText() else { |
| 29 | + extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + NCAssistantSharedTextStore.save(text) |
| 34 | + openMainAppForAssistantSharedText() |
| 35 | + } |
| 36 | + |
| 37 | + private func loadText() async -> String? { |
| 38 | + guard let extensionItems = extensionContext?.inputItems as? [NSExtensionItem] else { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + for extensionItem in extensionItems { |
| 43 | + guard let attachments = extensionItem.attachments else { |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + for provider in attachments { |
| 48 | + if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) { |
| 49 | + return await loadText(from: provider, typeIdentifier: UTType.plainText.identifier) |
| 50 | + } |
| 51 | + |
| 52 | + if provider.hasItemConformingToTypeIdentifier(UTType.utf8PlainText.identifier) { |
| 53 | + return await loadText(from: provider, typeIdentifier: UTType.utf8PlainText.identifier) |
| 54 | + } |
| 55 | + |
| 56 | + if provider.hasItemConformingToTypeIdentifier(UTType.text.identifier) { |
| 57 | + return await loadText(from: provider, typeIdentifier: UTType.text.identifier) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + private func loadText(from provider: NSItemProvider, typeIdentifier: String) async -> String? { |
| 66 | + await withCheckedContinuation { continuation in |
| 67 | + provider.loadItem(forTypeIdentifier: typeIdentifier, options: nil) { item, _ in |
| 68 | + let text: String? |
| 69 | + |
| 70 | + if let string = item as? String { |
| 71 | + text = string |
| 72 | + } else if let attributedString = item as? NSAttributedString { |
| 73 | + text = attributedString.string |
| 74 | + } else if let data = item as? Data { |
| 75 | + text = String(data: data, encoding: .utf8) |
| 76 | + } else if let url = item as? URL { |
| 77 | + text = try? String(contentsOf: url, encoding: .utf8) |
| 78 | + } else { |
| 79 | + text = nil |
| 80 | + } |
| 81 | + |
| 82 | + guard let text, !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { |
| 83 | + continuation.resume(returning: nil) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + continuation.resume(returning: text) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private func openMainAppForAssistantSharedText() { |
| 93 | + guard let url = URL(string: "nextcloud://assistant/shared-text") else { |
| 94 | + extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + openAssistantSharedTextURLThroughResponderChain(url) |
| 99 | + |
| 100 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in |
| 101 | + self?.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Opens the Assistant shared-text deep link from the Share extension. |
| 106 | + /// |
| 107 | + /// Share extensions cannot use `UIApplication.shared` directly because it is not |
| 108 | + /// extension-safe. This method walks the responder chain until it finds the hidden |
| 109 | + /// `UIApplication` responder and invokes the modern `open(_:options:completionHandler:)` |
| 110 | + /// Objective-C selector dynamically. |
| 111 | + /// |
| 112 | + /// This is intentionally isolated because it relies on Objective-C runtime dispatch. |
| 113 | + /// |
| 114 | + /// - Parameter url: Deep link URL to open in the containing application. |
| 115 | + private func openAssistantSharedTextURLThroughResponderChain(_ url: URL) { |
| 116 | + let selector = NSSelectorFromString("openURL:options:completionHandler:") |
| 117 | + let applicationClass: AnyClass? = NSClassFromString("UIApplication") |
| 118 | + var responder: UIResponder? = self |
| 119 | + |
| 120 | + while let currentResponder = responder { |
| 121 | + guard let applicationClass, |
| 122 | + currentResponder.isKind(of: applicationClass), |
| 123 | + currentResponder.responds(to: selector), |
| 124 | + let implementation = currentResponder.method(for: selector) else { |
| 125 | + responder = currentResponder.next |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + typealias CompletionBlock = @convention(block) (Bool) -> Void |
| 130 | + typealias OpenURLFunction = @convention(c) (AnyObject, Selector, NSURL, NSDictionary, CompletionBlock?) -> Void |
| 131 | + |
| 132 | + let openURL = unsafeBitCast(implementation, to: OpenURLFunction.self) |
| 133 | + |
| 134 | + let completion: CompletionBlock = { success in |
| 135 | + if success { |
| 136 | + nkLog(debug: "Assistant shared text deep link performed through modern responder chain") |
| 137 | + } else { |
| 138 | + nkLog(error: "Assistant shared text deep link modern responder chain returned false") |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + openURL(currentResponder, selector, url as NSURL, NSDictionary(), completion) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + nkLog(error: "Assistant shared text deep link failed because no UIApplication responder can open URL") |
| 147 | + } |
| 148 | +} |
0 commit comments