讓App支持系統(tǒng)/自定義類(lèi)型文件打開(kāi)(自定義類(lèi)型:ofd)

一、支持打開(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é)

  1. 系統(tǒng)文件讀取
    • 無(wú)需配置,直接UIDocumentPickerViewController代碼讀取
  2. 非系統(tǒng)文件讀取
    • 配置 Imported Type UTIs
    • UIDocumentPickerViewController代碼讀取
  3. 支持系統(tǒng)文件打開(kāi)
    • 配置 Document types
  4. 支持非系統(tǒng)文件打開(kāi)
    • 配置 Exported Type UTIs
    • 配置 Document types
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容