一、支持打開(kāi)
1、系統(tǒng)文件類(lèi)型
蘋(píng)果系統(tǒng)類(lèi)型的官方文檔:地址
以 PDF 為例,在 info.plist 里配置 Document types 如下:

image.png
- Document Type Name:文檔的類(lèi)型名稱(chēng)(PDF)。
- Handler rank:包含Owner、Alternate 、Default、None四個(gè)可選值,指定對(duì)于某種類(lèi)型的優(yōu)先權(quán)級(jí)別,而Launcher Service會(huì)根據(jù)這個(gè)優(yōu)先級(jí)別來(lái)排列顯示的App的順序。優(yōu)先級(jí)別從高到低依次是Owner、Alternate、Default、None表示不接受這種類(lèi)型。
- Document Content Type UTIs - Item0: 文件類(lèi)型的 identifier,系統(tǒng)文件可以從上面官方文檔查詢(xún)。
2、自定義類(lèi)型
添加自定義類(lèi)型
以 ofd 文件為例,在 info.plist 里配置 Exported Type Identifiers 如下:

Xcode11

Xcode12
- Identifier:唯一ID,需要保持唯一性。
- public.filename-extension(Extensions):文件類(lèi)型。
配置可打開(kāi)類(lèi)型
在 info.plist 里配置 Document types 如下:

image.png
二、支持導(dǎo)入
1、系統(tǒng)文件類(lèi)型
以 PDF 為例,使用 UIDocumentPickerViewController 讀取“文件”App內(nèi)的 PDF 文檔,代碼如下:
let documentTypes = ["com.adobe.pdf"]
let documentVC = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
documentVC.delegate = self
documentVC.transitioningDelegate = self
documentVC.modalPresentationStyle = .fullScreen
self.present(documentVC, animated: true, completion: nil)
2、自定義類(lèi)型
添加自定義類(lèi)型
以 ofd 文件為例,在 info.plist 里配置 Imported Type Identifiers 如下:

Xcode11

Xcode12
讀取文件
使用 UIDocumentPickerViewController 讀取“文件”App內(nèi)的 ofd 文檔,代碼如下:
let documentTypes = ["com.adobe.pdf", "com.inbasis.ofd"]
let documentVC = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
documentVC.delegate = self
documentVC.transitioningDelegate = self
documentVC.modalPresentationStyle = .fullScreen
self.present(documentVC, animated: true, completion: nil)
三、總結(jié)
- 系統(tǒng)文件讀取
- 無(wú)需配置,直接
UIDocumentPickerViewController代碼讀取
- 無(wú)需配置,直接
- 非系統(tǒng)文件讀取
- 配置
Imported Type UTIs -
UIDocumentPickerViewController代碼讀取
- 配置
- 支持系統(tǒng)文件打開(kāi)
- 配置
Document types
- 配置
- 支持非系統(tǒng)文件打開(kāi)
- 配置
Exported Type UTIs - 配置
Document types
- 配置