Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Boost Your Hardware Skills with This $15 ESP32 Handheld

    April 20, 2026

    Getting Started with Zero-Shot Text Classification

    April 20, 2026

    Singer loses life savings to fake wallet downloaded from the Apple App Store

    April 20, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»swift – Bundle.main.paths(forResourcesOfType:inDirectory) is not working on iOS
    iOS Development

    swift – Bundle.main.paths(forResourcesOfType:inDirectory) is not working on iOS

    big tee tech hubBy big tee tech hubApril 4, 2026004 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    swift – Bundle.main.paths(forResourcesOfType:inDirectory) is not working on iOS
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    I’m creating a Multiplatform SwiftUI app.

    After some initial struggles, I was able to create a directory of files and add them to my app’s bundle. At launch time, I try to use the Bundle function Bundle.main.paths(forResourcesOfType:) to find those files and open them.

    The Mac version of the app finds the files just fine. When I run the same code on the iOS version, it returns an empty array of file paths.

    I can navigate to the app bundle for the simulator version of the app in the finder, open it with “show package contents”, and see my folder in the bundle plain as day, with the files I’ve written to it. However, Bundle.main.paths(forResourcesOfType:) returns an empty list.

    I initially tried getting the bundle resources path appending my folder name to that, and using the file manager to fetch the list of files, but that failed, so I switched to using the Bundle method.

    Both approaches work on macOS, and both fail on iOS.

    Is there some sandboxing issue I’m not aware of?

    Here is my code:

        static var scopeTemplates: [ScopeTemplate] = {
            let fileManager = FileManager.default
            let folderName = "Kaleidoscope_Templates"
            do {
                if let bundlePath = Bundle.main.resourcePath {
                    print("bundlePath = \(bundlePath)")
                } else {
                    print("Can't resolve bundle path")
                }
                // old path-based code.
                //let kaleidoscopeTemplateFileNames = Bundle.main.paths(forResourcesOfType: "json", inDirectory: folderName)
                //let kaleidoscopeTemplateURLs = kaleidoscopeTemplateFileNames.map { URL(filePath: $0) }
                guard let kaleidoscopeTemplateURLs = Bundle.main.urls(forResourcesWithExtension: "json", subdirectory: folderName) else {
                    fatalError("Can't find folder \(folderName)")
                }
    
                
                
                let decoder = JSONDecoder()
                var scopeTemplates = [ScopeTemplate]()
                for aFileURL in kaleidoscopeTemplateURLs {
                    print("filename = \(aFileURL.path)")
                    let data = try Data(contentsOf: aFileURL)
                    let aScopeTemplate = try decoder.decode(ScopeTemplate.self, from: data)
                    scopeTemplates.append(aScopeTemplate)
                }
                return scopeTemplates.sorted() { lhv, rhv in
                    return lhv.index < rhv.index
    
                }
            } catch {
                fatalError("Error \(error) reading template files")
            }
        }()
    

    (This version of the code doesn’t crash, and doesn’t log any errors, but it returns an empty array. The FileManager version threw an exception on iOS when I tried to fetch the files in my directory, but that version also worked on macOS.)

    Also note that the bundle structure looks identical in iOS and MacOS. I use the finder “Go to Folder” menu command to open the directory in the bundle in the sim and it looks exactly as expected.

    I just created a minimum repeatable project and published it on Github:

    That code looks like this:

        static var scopeTemplates: [ScopeTemplate] = {
            
            let fileManager = FileManager.default
            let folderName = "Kaleidoscope_Templates"
            do {
                if let bundlePath = Bundle.main.resourcePath {
                    print("bundlePath = \(bundlePath)")
                } else {
                    print("Can't resolve bundle path")
                }
                guard let kaleidoscopeTemplateURLs = Bundle.main.urls(forResourcesWithExtension: "json", subdirectory: folderName) else {
                    fatalError("Can't find folder \(folderName)")
                }
                
                print("Directory \(folderName) in bundle resources contains \(kaleidoscopeTemplateURLs.count) files.")
                
                let decoder = JSONDecoder()
                var scopeTemplates = [ScopeTemplate]()
                for aFileURL in kaleidoscopeTemplateURLs {
                    print("filename = \(aFileURL.path)")
                    let data = try Data(contentsOf: aFileURL)
                    let aScopeTemplate = try decoder.decode(ScopeTemplate.self, from: data)
                    scopeTemplates.append(aScopeTemplate)
                }
                return scopeTemplates.sorted() { lhv, rhv in
                    return lhv.index < rhv.index
                    
                }
            } catch {
                fatalError("Error \(error) reading template files")
            }
        }()
    

    (The project on Github has the Xcode project, the folder-full of files, etc. It’ is set up as a SwiftUI Multiplatform project just like the real app I’m struggling with.)

    When run in the iOS simulator it logs the bundle path as expected:

    bundlePath = /Users/duncan/Library/Developer/CoreSimulator/Devices/EABDE8A8-0CAC-4D5F-81EF-FB03DC809EE5/data/Containers/Bundle/Application/8EA76044-8F14-4783-A45B-423A35F1EB4F/BundleFiles.app

    And then logs

    Directory Kaleidoscope_Templates in bundle resources contains 0 files.

    The same code run on my Mac loads and decodes the 4 files from the bundle as it should.

    When run on a Mac, the bundle path ends with Contents/Resources, but on iOS, it is the top-level app bundle directory. (I believe that is expected behavior.)



    Source link

    Bundle.main.pathsforResourcesOfTypeinDirectory iOS Swift working
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    DTCoreText 2.0 | Cocoanetics

    April 20, 2026

    ios – Xcode can’t find headers file from added frameworks

    April 19, 2026

    How I use FlowDeck to let my AI agent build and run my apps – Donny Wals

    April 18, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Boost Your Hardware Skills with This $15 ESP32 Handheld

    April 20, 2026

    Getting Started with Zero-Shot Text Classification

    April 20, 2026

    Singer loses life savings to fake wallet downloaded from the Apple App Store

    April 20, 2026

    Chinese tech workers are starting to train their AI doubles–and pushing back

    April 20, 2026
    Timer Code
    15 Second Timer for Articles
    20
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Boost Your Hardware Skills with This $15 ESP32 Handheld

    April 20, 2026

    Getting Started with Zero-Shot Text Classification

    April 20, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.