iosswifturlcastingnssecurecoding

Cast NSSecureCoding to URL in Swift


I have a ShareExtension in which I get the URL and I would like to use it but for that I need to cast it as a String:

@objc func actionButtonTapped(){
        print("yeet")
        var html: String?

        if let item = extensionContext?.inputItems.first as? NSExtensionItem,
            let itemProvider = item.attachments?.first,
            itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
                if (url as? URL) != nil {

                    let urlString = String(contentsOf: url)

                    OpenGraphDataDownloader.shared.fetchOGData(urlString: urlString) { result in
                        switch result {
                        case let .success(data, _):
                            // do something
                            print(data.pageTitle!)
                        case let .failure(error, _):
                            // do something
                            print(error.localizedDescription)
                        }

                    }
                }
            }
        }
}

The code above is throwing this error:

Cannot convert value of type 'NSSecureCoding?' to expected argument type 'URL'

Question:

How can I cast NSSecureCoding to URL so I can then cast it as String ?? Can't find anything on this anywhere... Happy for every help!


Solution

  • I solved it by first casting it as NSURL and then get its absoluteString like this:

    let directoryURL = url as! NSURL
    
    let urlString: String = directoryURL.absoluteString!