Post

Replies

Boosts

Views

Activity

how to mount a network share in Swift
I've tried multiple variations of the below but nothing seem to work. The output is alwaysError: sharePath blabla Not ValidFor some reason NetFSMountURLSync is not working in Swift 4.import Cocoaimport NetFS@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { var fileSystemProtocol: String = "smb" var logServer: String = "smb://myservername" var logShareName: String = "/Scripts" var userName: String = "" var password: String = "" func applicationDidFinishLaunching(_ aNotification: Notification) { mountShare(serverAddress: logServer, shareName: logShareName, userName: userName, password: password) } func applicationWillTerminate(_ aNotification: Notification) { } func mountShare( serverAddress: String, shareName: String, userName: String, password: String) { let fm = FileManager.default let mountPoint = "/Volumes/".appending(shareName) var isDir : ObjCBool = false if fm.fileExists(atPath: mountPoint, isDirectory:&isDir) { if isDir.boolValue { unmount(mountPoint, 0) print("unmount \(mountPoint)") } } let sharePath = NSURL(string: "\(serverAddress)\(shareName)")! let mounted: Int32 = NetFSMountURLSync(sharePath, nil, nil, nil, nil, nil, nil) if mounted > 0 { print("Error: sharePath: \(sharePath) Not Valid") } else { print("Mounted: \(sharePath)") } }}
16
0
8k
6h
how to mount a network share in Swift
I've tried multiple variations of the below but nothing seem to work. The output is alwaysError: sharePath blabla Not ValidFor some reason NetFSMountURLSync is not working in Swift 4.import Cocoaimport NetFS@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { var fileSystemProtocol: String = "smb" var logServer: String = "smb://myservername" var logShareName: String = "/Scripts" var userName: String = "" var password: String = "" func applicationDidFinishLaunching(_ aNotification: Notification) { mountShare(serverAddress: logServer, shareName: logShareName, userName: userName, password: password) } func applicationWillTerminate(_ aNotification: Notification) { } func mountShare( serverAddress: String, shareName: String, userName: String, password: String) { let fm = FileManager.default let mountPoint = "/Volumes/".appending(shareName) var isDir : ObjCBool = false if fm.fileExists(atPath: mountPoint, isDirectory:&isDir) { if isDir.boolValue { unmount(mountPoint, 0) print("unmount \(mountPoint)") } } let sharePath = NSURL(string: "\(serverAddress)\(shareName)")! let mounted: Int32 = NetFSMountURLSync(sharePath, nil, nil, nil, nil, nil, nil) if mounted > 0 { print("Error: sharePath: \(sharePath) Not Valid") } else { print("Mounted: \(sharePath)") } }}
Replies
16
Boosts
0
Views
8k
Activity
6h