how to mount a network share in Swift

I’ve tried multiple variations of the below but nothing seem to work. The output is always

Error: sharePath blabla Not Valid

 

For some reason NetFSMountURLSync is not working in Swift 4.

 

import Cocoa

import NetFS

@NSApplicationMain

class 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)”)

        }

    }

}

Powered by WPeMatico

About

You may also like...

Comments are closed.