文件选择用遇到的坑

2018-08-10  本文已影响0人  凨弥

简介

iOS8.0+之后有开放了对共享目录的文件浏览编辑等操作

UIDocumentPickerViewController

//
init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)

import //复制导入沙盒中
open //共享目录打开
exportToService
moveToService

官方给的文档说直接首前两个。

 let vc = UIDocumentPickerViewController(documentTypes: ["public.content","public.text"], in: .open)
         vc.delegate = self
            vc.modalPresentationStyle = .fullScreen
            vc.allowsMultipleSelection = true//ios11之后 加入多选功能
            self.present(vc, animated: true, completion: nil)

* 此处必须是present方法 如果navigationController?.pushViewController(vc, animated: true) 选中文件不会出发代理事件,不知原因,这是我遇到的一个坑。

  • documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
    ios8.0 ios11废弃
  • documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
    iOS11.0+ 选中文件后,文件的地址。

*需要注意的是,URL的权限不能直接访问共享文件的内容,需要申请权限,操作完成释放权限

  • url.tartAccessingSecurityScopedResource() 申请权限,BOOL
  • url.stopAccessingSecurityScopedResource() 释放

UIDocumentBrowserViewController

实例化的方法基本一样

 let vc = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: ["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"])
            
            vc.modalPresentationStyle = .fullScreen
            vc.delegate = self
            navigationController?.pushViewController(vc, animated: true)

代理相对简单 就不多做说明了

UIDocumentPickerViewController遇到了一个坑,目前还没有解决:同样的代码,Dome中显示正常,项目中,导航栏的返回图标于返回文字重叠。

上一篇下一篇

猜你喜欢

热点阅读