一共2步
第一步:
let docmPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
letfileName = (model.url!asNSString).lastPathComponent
?letfilePath = docmPath +"/accessory/\(model.name!).\(model.type!)"
self.saveFileToPhone(url: filePath)
第二步:
//MARK: 下載文件保存到手機文件指定目錄
? ? func saveFileToPhone(url:String) {
? ? ? ? letdocumentPicker =UIDocumentPickerViewController.init(url:URL(fileURLWithPath: url),in: .exportToService)
? ? ? ? documentPicker.delegate=self
? ? ? ? documentPicker.modalPresentationStyle = .formSheet
? ? ? ? currentViewController().present(documentPicker,animated:true)
? ? }
? ? //MARK:? - UIDocumentInteractionControllerDelegate
? ? public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
? ? ? ? return currentViewController()
? ? }
? ? public func documentInteractionControllerViewForPreview(_ controller: UIDocumentInteractionController) -> UIView? {
? ? ? ? return currentViewController().view
? ? }
? ? public func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
? ? ? ? return view.frame
? ? }
? ? // MARK: - UIDocumentPickerDelegate
? ? publicfuncdocumentPicker(_controller:UIDocumentPickerViewController,didPickDocumentsAturls: [URL]) { ? ? ? ?
//保存成功
? ? }
? ? public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
? ? ? ? //取消保存
? ? }