美文网首页
iOS 下载文件到文件App(swift语言)

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

作者: 黑白色 | 来源:发表于2022-11-21 12:37 被阅读0次

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

        //取消保存

    }

相关文章

网友评论

      本文标题:iOS 下载文件到文件App(swift语言)

      本文链接:https://www.haomeiwen.com/subject/hlcwxdtx.html