iOS 下载文件到文件App(swift语言)

一共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) {

        //取消保存

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容