iOS app to access files in local mac shared folder

I am trying to develop a SwiftUI iOS app that requires me to read and write text files kept in a shared folder on a Mac Mini in my local network. I have shared the folder with full read&write access and I have no problems using the Finder on my development iMac to access the files in that folder. The Mac Mini has a static IP Address of 192.168.1.28 (although I would prefer to use its Shared name but cannot get that to work either). I am developing on a 2019 iMac running macOS Big Sur 11.5.2 and using Xcode 13.2.1.

It may be that I have to connect to (or “mount”) the Shared folder using the iOS app before accessing the shared folder but all my efforts to Google how to do that have left me totally confused. This seems like something that should be easy to do and my problems are probably due to bad syntax or some other trivial mistake. Any and all help will be greatly appreciated. (I am new to Swift and to App development but have been programming for 50 years - mostly in real-time “C” applications before GUIs came along. Most of my problems stem from not understanding the syntax and the scope of the countless built-in functions available in Swift and the capabilities of the Xcode environment.)

My app will use the modification date of some text files to know when they have been changed and to trigger a response. The code below is just to test the function of reading the modificiation date of one shared file. If I change the file string to “file:///Users/pete/Desktop/Music.txt” then everything works fine, and I can open and save the Music.txt file on my development computer’s Desktop and the App prints the update. Switching to the local Mac Mini produces this error: Got caught by error:

Error Domain=NSCocoaErrorDomain Code=260 “The file “Music.txt” couldn’t be opened because there is no such file.” UserInfo={NSFilePath=/Users/Shared/Music.txt, NSUnderlyingError=0x600002eb5d40 {Error Domain=NSPOSIXErrorDomain Code=2 “No such file or directory”}}.

//
//  ContentView.swift
//  TestURLStuff
//
//  Created by Pete Stonebridge on 22/01/22.
//

import SwiftUI

var lastDate = Date(timeIntervalSince1970: 0)
let fileString = "file://192.168.1.28/Users/Shared/Music.txt"

struct ContentView: View {
    @State private var resultString = "Hello, world!"
    
    var body: some View {
        Text(resultString)
            .padding()
            .task {
                let formatter = DateFormatter()
                formatter.dateFormat = "HH:mm:ss E, d MMM y"
                print(formatter.string(from: lastDate))
                repeat {
                    sleep(1)
                    let thisDate = getFileModDate(fileString: fileString)!
                    
                    if thisDate > lastDate {
                        lastDate = thisDate
                        print(formatter.string(from: lastDate))
                    }
                } while true
            }
    }
    
    func getFileModDate(fileString: String) -> Date? {
        do {
            let urlString = fileString.replacingOccurrences(of: " ", with: "%20")
            guard let url = URL(string: urlString) else {
                print("Invalid URL")
                return Date()
            }
            
            let attr = try FileManager.default.attributesOfItem(atPath: url.path)
            guard let modDate = attr[FileAttributeKey.modificationDate] as? Date else {
                print("Couldn't get modification date")
                return Date()
            }
            // Success - return the modification date
            return modDate
        } catch {
            print("Got caught by error: \(error).")
            return Date()
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thanks in advance, Pete.

Hi @pstonebridge welcome to the forum. This is an interesting post! Thank you! To tell you honestly, I don’t have idea this is possible but I would love to search the web and share anything I found.

Have you consider CloudKit as an alternative?

CloudKit? Never heard of it but will check it out. Thanks for the heads-up!

One of the problems with XCode and Swift is that there are just too many “kits”. The syntax for each one is always challenging to enter correctly and the guidelines are always a bit esoteric. The documentation should come with some real-world examples using actual names, strings, etc. and not just references to another web of complicated terms. Another problem is the constant revisions, changes, and “upgrades”. I started learning Swift over 4 years ago and got a couple of small projects working only to find that they no longer worked with the next release of Swift code. Progress is good, but they should pay more attention to backwards compatibility.

The introduction of SwiftUI is amazingly powerful. But then it makes one wonder why it wasn’t done that way in the first place. So much simpler than the UIKit manual layout methods. The new introduction of Async/Await also makes one wonder why nobody thought of that sooner instead of the old cumbersome completion handler method. More time spent in planning future releases and introducing them more slowly might lead to a less turbulent life for the wannabe developers.

Final thought: (To put it in perspective I took the very first computer engineering class offered in the UK in 1970, worked in the telecommunications industry in Silicon Valley after graduating, managed a team of 75 software engineers in real-time applications, and have programmed for personal projects ever since (I love Applescript - it does everything I want and they don’t keep changing it)). The Swift language and its associated “kits”, along with the XCode development environment, are themselves developed by professional software engineers who claim to be offering “simple” solutions to enable the whole world to write applications easily. From personal experience, I have not met many software engineers that would deliberately weaken their job security so easily. IMHO, they could make everything so much simpler to get started doing the essential things. Accessing files on a local network computer seems like a fundamental thing to do for me. It should be one of the first lessons.

OK. Rant over. Back to work…

1 Like

I hope I learn more things from you! I’ll look forward to seeing you in the forum. Learning from experience developers is like having a shortcut in life as a developer. I hope you don’t mind sharing more of your insight into this community. :blush:

Unfortunately, I am a total novice when it comes to Swift. I looked into CloudKit and discovered that it is for using iCloud storage for sharing files between devices. That seems a little bit of overkill when all I want to do is share files between devices on a local network. I’m sure that it is easy enough once you break through the initial barrier but I’m about to give up on that approach. FileManager seemed like the obvious choice and I found a way to list all the “mounted” shared folders, but nothing on how to actually mount one.

Oh well, I think I’ll go back to exploring using Bonjour to communicate between devices and give up on the idea of shared files being easier. I found sample code a few years ago that allowed me to send text messages using Bonjour between a MacOS server app and an iOS client and got it working but then they released a newer version of Swift a few weeks later and it stopped working.

Onwards and upwards… Good luck with your code.

To be devils advocate to this specific statement.

You started Swift relatively early! It’s a young language! It was only 4 years old when you had first used it and now it’s only 8 years old. (Released in 2014)

As opposed to languages like C that have been around since the 70s, and have had decades to stabilize.

Coming from your background with C languages and a backend, I can see how you think this, but my experience as an iOS developer I’ve never wanted or needed to do this, with any of my app ideas. I don’t think it’s a fundamental thing to do.

But your app your making I’d suggest looking at how to access the Files app which already handles syncing files between a Mac and iPhone and your app can access those files

1 Like