13
6

More than 3 years have passed since last update.

UIDocumentPickerViewControllerで選択ファイルを絞る & iOS14からのUTType

Last updated at Posted at 2020-08-04

ユーザーの選択ファイルを限定したい

最近のプロジェクトで端末内の.m4a, .mp3ファイルだけ選択させたい用件があったのでUIDocumentPickerViewControllerでdocumentTypesを指定して実装しました。

その時にMobileCoreServiceの定数を使って処理していたのですが、非推奨の扱いになっていたので合わせてiOS14以降のUTTypeについて調べました。

ちなみに非推奨の実装でベータのiOS14のシュミレータで実行してみましたが、ワーニングが出るだけで動作はしていました。

iOS13までの実装

Xcode 11.5(11E608c)

import UIKit
// 1. MobileCoreServicesをインポート
import MobileCoreServices

class FileSearchViewController: UIViewController {

    // ~~~ 省略 ~~~

    private func showFilePicker() {
        // 2. documentTypesでkUTType~で選択して欲しいファイル形式を指定する
        // iOS14だと 'init(documentTypes:in:)' was deprecated in iOS 14.0と怒られる
        // ここでは選択できるファイルを.m4a, .mp3に限定する
        let picker = UIDocumentPickerViewController(
            documentTypes: [
                String(kUTTypeMPEG4Audio),
                String(kUTTypeMP3)
            ],
            in: .import)
        picker.delegate = self
        self.navigationController?.present(picker, animated: true, completion: nil)
    }
}

extension FileSearchViewController: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        // ファイル選択後に呼ばれる
        // urls.first?.pathExtensionで選択した拡張子が取得できる

        if let filePath = urls.first?.description {
            print("ファイルパス:\(filePath)")
        }
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("キャンセル")
    }
}

選択できるファイルはこちらのUTTypeのContentsから選べます。
Apple Document: MobileCoreServices/UTType

iOS14以降の実装(ベータなので変更になるかもしれません)

主な変更は1と2のimportとUIDocumentPickerViewControllerの呼び出し方とUTTypeの指定です。

Xcode 12.0 beta 3 (12A8169g)

import UIKit
// 1. UniformTypeIdentifiersをインポート
import UniformTypeIdentifiers

class FileSearchViewController: UIViewController {

    // ~~~ 省略 ~~~

    private func showFilePicker() {
        // 2. forOpeningContentTypesでUTTypeで選択して欲しいファイル形式を指定する
        // ここでは選択できるファイルを.m4a, .mp3に限定する
        let picker = UIDocumentPickerViewController(
            forOpeningContentTypes: [
                UTType.mpeg4Audio,
                UTType.mp3
            ],
            asCopy: true)
        picker.delegate = self
        self.navigationController?.present(picker, animated: true, completion: nil)
    }
}

extension FileSearchViewController: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        // ファイル選択後に呼ばれる
        // urls.first?.pathExtensionで選択した拡張子が取得できる

        if let filePath = urls.first?.description {
            print("ファイルパス:\(filePath)")
        }
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("キャンセル")
    }
}

Apple Document: UniformTypeIdentifiers

13
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
6