你好卡尔文,
请添加代表UIDocumentMenuDelegate,UIDocumentPickerDelegate
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let docurl = url as URL
print("The Url is : \(docurl)")
let urlRequest = NSURLRequest(url: docurl)
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: url.path)
let theData = try NSURLConnection.sendSynchronousRequest(urlRequest as URLRequest, returning: nil)
let fileExtension = urlRequest.url?.lastPathComponent
var strFileNAME = ""
if (fileExtension?.contains("pdf"))! {
print("pdf")
} else {
print("doc")
}
var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last as NSURL?
docURL = docURL?.appendingPathComponent("\(String(describing: fileExtension))") as NSURL?
try theData.write(to: docURL! as URL)
self.DocumentData = theData
print("downloaded")
}
} catch (let writeError) {
print("error : \(writeError)")
}
let docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last)
do {
let contents = try (FileManager.default.contentsOfDirectory(at: docURL!, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles))
print(contents)
}
catch (let error) {
print("error contents \(error)")
}
}
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}