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

        //取消保存

    }

你可能感兴趣的:(iOS 下载文件到文件App(swift语言))