How to open native files app in ios?
Asked Answered
S

4

9

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 ?

Soliz answered 18/2, 2018 at 11:41 Comment(9)
why do you need to open the app? what are you trying to achieve?Due
let user select files andndocuments he want to share !Soliz
did my answer solve your problem?Due
nope it didn't if you look at existing apps all they do is use file app to let users select documents I want to do the same so could you help please !Soliz
IMHO everybody accesses the data in the Files app via 'UIDocumentPickerViewController'. The code I posted in my answer allows you to open a ViewController which shows you the same files as in the Files app. Did you try my code?Due
i did ! it worked but delegate methods arent working !Soliz
Is your delegate who registers here controller.delegate = self conforming to the protocol UIDocumentPickerDelegate?Due
yes it confirms to the protocalSoliz
what happens after you select a file and confirm your selection and the controller is dismissed? any errors?Due
D
13

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
}
Due answered 18/2, 2018 at 12:11 Comment(1)
For documentTypes, here's a list of "System-Declared Uniform Type Identifiers", hope it helps for others.Viscardi
N
6

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.

Nitwit answered 14/2, 2020 at 9:20 Comment(0)
B
3

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.

Bristle answered 3/8, 2023 at 18:44 Comment(0)
H
2

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)
Herwin answered 29/1, 2022 at 15:51 Comment(2)
Is there a future-proof way to specify the list of all possible UTIs? Something like "*" perhaps?Menispermaceous
@Menispermaceous Go to developer.apple.com/library/archive/documentation/Miscellaneous/… and see the few UTIs that don't conform to another (such as public.item and public.database). Those few would cover all others.Carding

© 2022 - 2024 — McMap. All rights reserved.