How to open native files app in ios?

10.4k Views Asked by At

How do i open the native files app of ios using url scheme or some other way ? I tried searching url scheme but had no luck.

There seems to be no answer to this question there is thread open for this question in apples forum but it is still unanswered. https://forums.developer.apple.com/message/257860#257860 Can it be done using the bundle identifier ?

4

There are 4 best solutions below

0
On

If you really want to open the iOS Files app -- and not a UIDocumentPicker, which is a different beast -- you can use this code

extension URL {
    func openParentDirectoryInFilesApp() {
        guard var components = URLComponents(url: self.deletingLastPathComponent(), resolvingAgainstBaseURL: false) else { return }

        components.scheme = "shareddocuments"

        guard let newURL = components.url else { return }

        if UIApplication.shared.canOpenURL(newURL) {
            UIApplication.shared.open(newURL)
        }
    }
}

I learned this from here.

0
On

There is the option to open the Files app at a specific location using the shareddocuments:// URL scheme, as described in this article.

If you want the user to select a document, you can use chriswillow's approach with the UIDocumentPickerViewController. If you want to present the documents or folders in your app, create a Document Browser App.

2
On

Swift 5

let docsTypes = ["public.text",
                     "com.apple.iwork.pages.pages",
                     "public.data",
                     "kUTTypeItem",
                     "kUTTypeContent",
                     "kUTTypeCompositeContent",
                     "kUTTypeData",
                     "public.database",
                     "public.calendar-event",
                     "public.message",
                     "public.presentation",
                     "public.contact",
                     "public.archive",
                     "public.disk-image",
                     "public.plain-text",
                     "public.utf8-plain-text",
                     "public.utf16-external-plain-​text",
                     "public.utf16-plain-text",
                     "com.apple.traditional-mac-​plain-text",
                     "public.rtf",
                     "com.apple.ink.inktext",
                     "public.html",
                     "public.xml",
                     "public.source-code",
                     "public.c-source",
                     "public.objective-c-source",
                     "public.c-plus-plus-source",
                     "public.objective-c-plus-​plus-source",
                     "public.c-header",
                     "public.c-plus-plus-header",
                     "com.sun.java-source",
                     "public.script",
                     "public.assembly-source",
                     "com.apple.rez-source",
                     "public.mig-source",
                     "com.apple.symbol-export",
                     "com.netscape.javascript-​source",
                     "public.shell-script",
                     "public.csh-script",
                     "public.perl-script",
                     "public.python-script",
                     "public.ruby-script",
                     "public.php-script",
                     "com.sun.java-web-start",
                     "com.apple.applescript.text",
                     "com.apple.applescript.​script",
                     "public.object-code",
                     "com.apple.mach-o-binary",
                     "com.apple.pef-binary",
                     "com.microsoft.windows-​executable",
                     "com.microsoft.windows-​dynamic-link-library",
                     "com.sun.java-class",
                     "com.sun.java-archive",
                     "com.apple.quartz-​composer-composition",
                     "org.gnu.gnu-tar-archive",
                     "public.tar-archive",
                     "org.gnu.gnu-zip-archive",
                     "org.gnu.gnu-zip-tar-archive",
                     "com.apple.binhex-archive",
                     "com.apple.macbinary-​archive",
                     "public.url",
                     "public.file-url",
                     "public.url-name",
                     "public.vcard",
                     "public.image",
                     "public.fax",
                     "public.jpeg",
                     "public.jpeg-2000",
                     "public.tiff",
                     "public.camera-raw-image",
                     "com.apple.pict",
                     "com.apple.macpaint-image",
                     "public.png",
                     "public.xbitmap-image",
                     "com.apple.quicktime-image",
                     "com.apple.icns",
                     "com.apple.txn.text-​multimedia-data",
                     "public.audiovisual-​content",
                     "public.movie",
                     "public.video",
                     "com.apple.quicktime-movie",
                     "public.avi",
                     "public.mpeg",
                     "public.mpeg-4",
                     "public.3gpp",
                     "public.3gpp2",
                     "public.audio",
                     "public.mp3",
                     "public.mpeg-4-audio",
                     "com.apple.protected-​mpeg-4-audio",
                     "public.ulaw-audio",
                     "public.aifc-audio",
                     "public.aiff-audio",
                     "com.apple.coreaudio-​format",
                     "public.directory",
                     "public.folder",
                     "public.volume",
                     "com.apple.package",
                     "com.apple.bundle",
                     "public.executable",
                     "com.apple.application",
                     "com.apple.application-​bundle",
                     "com.apple.application-file",
                     "com.apple.deprecated-​application-file",
                     "com.apple.plugin",
                     "com.apple.metadata-​importer",
                     "com.apple.dashboard-​widget",
                     "public.cpio-archive",
                     "com.pkware.zip-archive",
                     "com.apple.webarchive",
                     "com.apple.framework",
                     "com.apple.rtfd",
                     "com.apple.flat-rtfd",
                     "com.apple.resolvable",
                     "public.symlink",
                     "com.apple.mount-point",
                     "com.apple.alias-record",
                     "com.apple.alias-file",
                     "public.font",
                     "public.truetype-font",
                     "com.adobe.postscript-font",
                     "com.apple.truetype-​datafork-suitcase-font",
                     "public.opentype-font",
                     "public.truetype-ttf-font",
                     "public.truetype-collection-​font",
                     "com.apple.font-suitcase",
                     "com.adobe.postscript-lwfn​-font",
                     "com.adobe.postscript-pfb-​font",
                     "com.adobe.postscript.pfa-​font",
                     "com.apple.colorsync-profile",
                     "public.filename-extension",
                     "public.mime-type",
                     "com.apple.ostype",
                     "com.apple.nspboard-type",
                     "com.adobe.pdf",
                     "com.adobe.postscript",
                     "com.adobe.encapsulated-​postscript",
                     "com.adobe.photoshop-​image",
                     "com.adobe.illustrator.ai-​image",
                     "com.compuserve.gif",
                     "com.microsoft.bmp",
                     "com.microsoft.ico",
                     "com.microsoft.word.doc",
                     "com.microsoft.excel.xls",
                     "com.microsoft.powerpoint.​ppt",
                     "com.microsoft.waveform-​audio",
                     "com.microsoft.advanced-​systems-format",
                     "com.microsoft.windows-​media-wm",
                     "com.microsoft.windows-​media-wmv",
                     "com.microsoft.windows-​media-wmp",
                     "com.microsoft.windows-​media-wma",
                     "com.microsoft.advanced-​stream-redirector",
                     "com.microsoft.windows-​media-wmx",
                     "com.microsoft.windows-​media-wvx",
                     "com.microsoft.windows-​media-wax",
                     "com.apple.keynote.key",
                     "com.apple.keynote.kth",
                     "com.truevision.tga-image",
                     "com.sgi.sgi-image",
                     "com.ilm.openexr-image",
                     "com.kodak.flashpix.image",
                     "com.j2.jfx-fax",
                     "com.js.efx-fax",
                     "com.digidesign.sd2-audio",
                     "com.real.realmedia",
                     "com.real.realaudio",
                     "com.real.smil",
                     "com.allume.stuffit-archive",
                     "org.openxmlformats.wordprocessingml.document",
                     "com.microsoft.powerpoint.​ppt",
                     "org.openxmlformats.presentationml.presentation",
                     "com.microsoft.excel.xls",
                     "org.openxmlformats.spreadsheetml.sheet"]
    
    let documentPicker = UIDocumentPickerViewController(documentTypes: docsTypes, in: .import)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = true
    present(documentPicker, animated: true, completion: nil)
1
On

Please try to use UIDocumentPickerViewController for your use case

let controller = UIDocumentPickerViewController(
    documentTypes: ["public.text"], // choose your desired documents the user is allowed to select
    in: .import // choose your desired UIDocumentPickerMode
)
controller.delegate = self
if #available(iOS 11.0, *) {
    controller.allowsMultipleSelection = false
}
// e.g. present UIDocumentPickerViewController via your current UIViewController
present(
    controller,
    animated: true,
    completion: nil
)

UIDocumentPickerDelegate delegate methods to receive chosen documents URL as callback:

@available(iOS 11.0, *)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // do something with the selected documents
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    // do something with the selected document
}