// // ActionViewController.swift // URLDisplayApp // // Created by Eli Slothower on 3/12/25. // import UIKit import UniformTypeIdentifiers class ActionViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Optionally hide the view if you want no visible UI view.isHidden = true processInput() } private func processInput() { print("Action Extension: processInput started") // Get the first input item guard let inputItem = extensionContext?.inputItems.first as? NSExtensionItem else { completeRequest() return } // Try to extract a URL from the item let urlType = UTType.url.identifier if let attachment = inputItem.attachments?.first, attachment.hasItemConformingToTypeIdentifier(urlType) { attachment.loadItem(forTypeIdentifier: urlType, options: nil) { [weak self] data, error in guard let self = self else { return } if let url = data as? URL { self.handleURL(url) } else { print("Action Extension: Failed to load URL from attachment") self.completeRequest() } } } else { print("Action Extension: No valid attachment found") completeRequest() } } private func handleURL(_ url: URL) { // Encode the URL to form a proper deep link guard let encodedURL = url.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let deepLink = URL(string: "myapp://share?url=\(encodedURL)") else { print("Action Extension: Error forming deep link") completeRequest() return } print("Action Extension: Deep Link formed: \(deepLink)") // Attempt to open the main app with the deep link extensionContext?.open(deepLink) { success in if success { print("Action Extension: Opened main app successfully") } else { print("Action Extension: Failed to open main app") } self.completeRequest() } } private func completeRequest() { // Dismiss the extension extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) } }