【发布时间】:2020-01-14 23:28:50
【问题描述】:
我正在尝试构建一个应用程序,我可以在其中快速访问(读/写)本地网络中的 windows/mac 共享文件夹。
有没有什么方法可以用 swift 做到这一点?
App Store 中有一个名为“FileExplorer”https://apps.apple.com/de/app/fe-file-explorer-file-manager/id510282524 的应用程序,您可以在其中访问这些共享文件夹,但我不知道他们如何编程以及使用哪种语言进行编程。 我还尝试通过这个应用程序访问我的共享文件夹,是的,我可以在我的手机上看到我的共享文件夹。
但需要有一种方法可以快速完成...
我已经尝试过不同的东西(代码如下)。 在下面的代码中,我尝试访问我的第二台 Mac 的共享文件夹,并将文本“使用 Swift 在 iOS 中将此文本作为文本写入 fileURL”写入名为“Test.txt”的文件中,然后我想阅读再次相同的文件。
@IBAction func Button(_ sender: UIButton)
{
var uc = URLComponents()
uc.scheme = "smb"
uc.user = "user"
uc.password = "password"
uc.host = "ip-adress"
uc.path = "document-directory"
// Save data to file
let fileName = "Test"
let url = uc.url
//let DocumentDirURL = URL(fileURLWithPath: "/Users/f/d/t/App/Assets/Apps/TestApp")
let DocumentDirURL = try! URL(resolvingAliasFileAt: url!)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
let writeString = "Write this text to the fileURL as text in iOS using Swift"
do {
// Write to the file
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
var fullString: String = "" // Used to store the file contents
do {
// Read the file contents
fullString = try String(contentsOf: fileURL, encoding: .utf8)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)")
}
如果我按所示运行代码,他总是给我错误 “不支持 smb 方案”,然后是一些额外的错误,他无法写入/读取文件,因为他无法访问它。
当我更改代码并仅在我正在编程的设备上搜索并运行模拟器来搜索此文件时,一切正常。所以我对“smb”有疑问。
感谢您提供的每一个有用的答案。
【问题讨论】:
-
iOS 本身不支持 SMB。你需要自己实现这个协议。那里可能有一些你可以使用的源代码,但我怀疑 Swift 中有什么。
-
Thx Paulw11,几天前我发现了这个:github.com/amosavian/AMSMB2 我想这就是我要找的东西,但我真的不知道如何使用它。我已经在我的项目中实现了它,但是如何使用第一个功能呢?当我调用该函数时,我应该在“处理程序”中写入什么
-
您尝试过使用 Samba 吗?这是一个用 C 编写的开源 SMB 解决方案。您可以包装此代码并在您的 Swift 项目中使用它。还有一个C基础的SMB方案叫YNQ,这个是商业授权方案,公司名称是Visuality Systems,可以联系他们询问。
标签: ios swift local shared smb