Swift连接到网络smb驱动器

时间:2016-04-14 18:58:48

标签: xcode swift

我一直在尝试通过swift 2和Xcode 7找到连接到我的网络驱动器的方法。我可以连接到我的桌面或文档目录,但不能连接到我的服务器。我玩过NSString / NSURL等,但无济于事。以下是我的参考代码:

//Target file
let myFile = "myTextFile.txt"

// Write this text.
let myText = "Dante, The Divine Comedy"

//Target path
if let myDir : NSString = ("smb://path/to/text/file/on/network/server/myTextFile.txt") {
let myPath = myDir.stringByAppendingPathComponent(myFile);

// Writting
do {
try myText.writeToFile(myPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
print("Failed writing to URL: \(myPath), Error: " + error.localizedDescription)
}

//Reading
do {
let myText = try? NSString(contentsOfFile: myPath, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
print("Failed writing to URL: \(myPath), Error: " + error.localizedDescription)
}
} //closes if

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

看起来您要将文件名附加到已包含文件名的路径(" smb://path/to/text/file/on/network/server/myTextFile.txt" +" myTextFile.txt" =" smb://path/to/text/file/on/network/server/myTextFile.txtmyTextFile.txt")