Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS: Accessing contents directly from a directory in Swift

I am new to mac OS dev so pardon me for any basic questions, but trust me, I have done a lot of research and still am kind of stuck.

So I am trying to access contents of desktop of current user.

In playground it's easy, just use

let homeURL = FileManager.default.homeDirectoryForCurrentUser
let desktopURL = homeURL.appendingPathComponent("Desktop/")
let contents = try fileManager.contentsOfDirectory(atPath:desktopURL.path)

It shows all the contents on desktop. But when I run similar code in a project.

let homeURL = FileManager.default.homeDirectoryForCurrentUser

it returns me container of my App.

file:///Users/myUserName/Library/Containers/AppIdentifier/Data/

and the let contents = try fileManager.contentsOfDirectory(atPath: desktopURL.path) doesnt reuturn any content.

Well, I then I use

deletingLastPathComponent

while getting home path to get my URL till my username

let homeURL = FileManager.default.homeDirectoryForCurrentUser.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()

and when I debug it, it returns

file:///Users/myUserName

as i wanted. But when I run the code, the contents variable is still nil.

Next attempt:

I searched internet to check alternate way to get path of a directory. So I came across

NSOpenPanel

I get URL of folder from Panel and use that url to check contents in it. It work fine

here's the code for that

func buttonClicked(_ sender : Any){
guard let window = view.window else {return}

        let panel = NSOpenPanel()
        panel.canChooseFiles = false
        panel.canChooseDirectories = true
        panel.allowsMultipleSelection = false

        panel.beginSheetModal(for: window) { (result) in
            if result.rawValue == 1{
                let folderURL = panel.urls[0]
                print(folderURL)

                let contents = self.contentsOf(folder: folderURL)
                contents.forEach{
                    print($0.absoluteString)
                }
            }
        }
        }
 
     func contentsOf(folder: URL) -> [URL] {
        
        print("folder path = \(folder.path)")
        let fileManager = FileManager.default
        do {
            let contents = try fileManager.contentsOfDirectory(atPath: folder.path)
            let urls = contents
                .filter { return $0.first != "." }
                .map { return folder.appendingPathComponent($0) }
            return urls
        } catch {
            return []
        }
    }

but I don't understand why i can't access the contents directly. I have tried a ton of times. In a project I can'enter code heret get contents directly from a (sort of) hard coded URL.

Let me know if any other information is needed.

like image 627
Talha Ahmad Khan Avatar asked Nov 26 '25 13:11

Talha Ahmad Khan


1 Answers

Obviously the app is sandboxed which is enabled by default.

If App Sandbox is enabled you can only access the directories in the application container programmatically. This is an intended restriction. The only way to access one of the standard directories in the usual home folder is the com.apple.security.files.user-selected.read-write entitlement and NSOpen-/SavePanel.

Please read App Sandbox Design Guide

If you are not planning to publish the app in the App Store and you don't need the sandbox disable it.

like image 154
vadian Avatar answered Nov 28 '25 02:11

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!