'Using kUTTypePDF in a Share Extension only provides local URL, not web URL?
My app saves the URL from Safari using a Share Extension. However, when the site is a pdf type file I notice that using the kUTTypePDF identifyer the resulting URL is a local URL instead of the web URL. Does anyone know how to obtain the web URL file from a pdf file type? Note: using kUTTypeURL or "public URL" works for the standard html file, but will not work if it is a pdf file. Code is below. The resulting linkURL = String(describing: shareURL) is a local URL (file:// ..., not https://www...)
private func handleSharedFile() {
// extracting the path to the URL that is being shared
recTypeS = "xxx"
let contentType1 = kUTTypeImage as String
let contentType2 = kUTTypeURL as String
let contentType3 = kUTTypePDF as String
guard
let items = extensionContext?.inputItems as? [NSExtensionItem],
let item = items.first,
let attachments = item.attachments
else {
print("no items found")
return}
let semaphore = DispatchSemaphore(value: 2) // allows 2 threads at a time
print("** Attachements Found: ",attachments.count)
if attachment.hasItemConformingToTypeIdentifier(contentType3) {
attachment.loadItem(forTypeIdentifier: contentType3,
options: nil) { [unowned self] (url, error) in
// Handle the error here if you want
guard error == nil else {
//
return }
if let shareURL = url as? NSURL{
linkURL = String(describing: shareURL) // This results in local URL (file:// ..., not https://www...)
let sufType = getSuffix(fileName: linkURL)
print("shareURL=",shareURL)
print("suffix=",sufType)
guard let nurl = URL(string: String(describing: shareURL)) else {return}
let session = URLSession.shared
session.dataTask(with: nurl) { (data, response, err) in
if let response = response {
print(response)
}
if let data = data{
//print(data)
let datastring = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
//datF = datastring as String? ?? ""
recTypeS = ".pdf"
let document = PDFDocument(data: data)
pdfView.document = document
NotificationCenter.default.post(name: .nVersion, object: self) // updates VC
}
} .resume()
} else {
print("Could not load URL")
// Handle this situation as you prefer
fatalError("Impossible to load image")
}
print("url found")
semaphore.signal()
} // closure
} // end if type URL
} // loop
_ = semaphore.wait(timeout: .now() + 1.0)
//print(String(describing: image))
//print(String(describing: url))
if recTypeS == "xxx" {
print("no valid type")
//extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
} else { print("valid type found")}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|