iOS,从“文件”选择文件

iOS,从“文件”选择文件_第1张图片
iOS10.jpeg

由于项目需求,需要从iOS 11后系统自带的“文件”App中选择文件进行上传,刚拿到这个需求,以为很复杂,于是思维方向就错了,掉进了坑了,跌的老疼。好了废话不多说,进入正题。

需求讲解:
附件上传,通常我们的附件上传,就是上传本地的照片,或者是拍照上传,这两个很简单。如果需要上传文件,例如word文档excel文档或者是pdf等文档,此时我们就应该考虑文档的来源,来自sandBox还是来自系统自带的“文件”app中(这里对这个系统自带的“文件”app,实际就是访问iCould云盘)。

1.如果来源是sandBox:
如果是从本地的沙盒中选择文件上传, 这个相对比较简单,用NSFileManage类以及相关API可以解决

2.如果来源是“文件”(iOS 11系统自带的App,一下简称“文件”):
如果是选择“文件”中的文档上传,这个就相对较复杂一点,其实也很简单,因为之前走了很多的弯路,所以自认为相对较复杂。

爬坑说明:
第一次拿到这个需求的时候,思维走了弯路, 以为是需要调用iCouldKit相关API,于是查阅了官方文档,结果没有找到方向,于是又继续百度,又想到了一个关键字iCould Drive, 又去查阅了iCould Drive相关官方文档,然并卵,于是陷入了僵局。后来无意中在网上查到了一篇内容很少的博客,一试中第,果然就是这个东西,到此问题得以初步解决。

干货:
1.首先要调用”文件“,我们需要对项目进行配置,就是配置iCould,其实很简单,点击【工程】 -> 选择【targets】 -> 选择【Capabilities】 -> 打开【iCloud】 -> 勾选【iCould Documents】, 搞定, 我的Xcode版本是9.3,所以证书和配置文件的修改和配置,Xcode自动完成。

2.要调用“文件”,其实很简单,就是一个一个类UIDocumentPickerViewController,这个类的具体interface我还没有去查阅,这里就不给大家乱说了,我只介绍怎么用它。上代码:


private func selectUploadFileFromICouldDrive()  {
    letdocumentTypes = ["public.content",
                         "public.text",
                         "public.source-code",
                         "public.image",
                         "public.audiovisual-content",
                         "com.adobe.pdf",
                         "com.apple.keynote.key",
                         "com.microsoft.word.doc",
                         "com.microsoft.excel.xls",
                         "com.microsoft.powerpoint.ppt"]

     letdocument = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .open)
     document.delegate = self  //UIDocumentPickerDelegate
     self.present(document, animated:true, completion:nil)
}

就是这么简单,就这么我们就成功了访问了到了“文件”。接下来,我就需要对选择文件进行操作。
UIDocumentPickerDelegate有两个协议方法,如下:

func documentPicker(_ controller: UIDocumentPickerViewController,didPickDocumentAt url: URL) 
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) 

我们在第一个代理方法中,拿到我们选择的文件地址,进行处理:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    let fileName = url.lastPathComponent
    if ICouldManager.iCouldEnable() {
        ICouldManager.downloadFile(WithDocumentUrl: url) { (fileData) in
            //fileData:就是我们选择的文件,这里你可以做你想要的操作,我这里是上传到服务
            self.uploadFile(WithFileName: fileName, fileData: fileData)
        }
    } 
    controller.dismiss(animated: true, completion: nil)
}

这里的ICouldManager是我自定义的类,以及ICouldManager类里面用到的SxsDocument类,这两个类我也是在网上找的源码,具体的含义我也就不做过多的解释了,可以自行百度下。特此说明下,UIDocument这个类,大家可以去看看官方文档的介绍,很有用。
源码如下:

class ICouldManager {
    public static func iCouldEnable() -> Bool {
        let url = FileManager.default.url(forUbiquityContainerIdentifier: nil)
        return (url != nil)
    }
    
    public static func downloadFile(WithDocumentUrl url: URL, completion: ((Data) -> Void)? = nil) {
        let document = SxsDocument.init(fileURL: url)
        document.open { (success) in
            if success {
                document.close(completionHandler: nil)
            }
            if let callback = completion {
                callback(document.data)
            }
        }
    }
}
class SxsDocument: UIDocument { 
    public var data = Data.init()
    
    override func load(fromContents contents: Any, ofType typeName: String?) throws {
        self.data = contents as! Data
    }
}

总结:
OK完毕,就这么简单,比较菜的我,走了很多的弯路, 最终还是解决了这个问题,所以特此记录,以便往后查阅。

你可能感兴趣的:(iOS,从“文件”选择文件)