Swift SMB client library and iOS/macOS file browser applications. This library provides a high-level interface to the SMB protocol and allows you to access files on remote SMB servers. Written in Swift, no dependencies on external libraries.
SMBClient
class hides the low-layer SMB protocol and provides a higher-layer interface suitable for common use cases. The following example demonstrates how to list files in a share drive on a remote SMB server.
import SMBClient
let client = SMBClient(host: "198.51.100.50")
try await client.login(username: "alice", password: "secret")
try await client.connectShare("Public")
let files = try await client.listDirectory("")
print(files.map { $0.fileName })
try await client.disconnectShare()
try await client.logoff()
If you want to use the low-layer SMB protocol directly, you can use the Session
class. Session
class provides a set of functions that correspond to SMB messages. You can get more fine-grained control over the SMB protocol.
import SMBClient
let session = Session(host: "198.51.100.50")
try await session.connect()
try await session.negotiate()
try await session.sessionSetup(username: "alice", password: "secret")
try await session.treeConnect(path: "Public")
let files = try await session.queryDirectory(path: "", pattern: "*")
print(files.map { $0.fileName })
try await session.treeDisconnect()
try await session.logoff()
let client = SMBClient(host: "198.51.100.50")
try await client.login(username: "alice", password: "secret")
let shares = try await client.listShares()
print(shares.map { $0.name })
try await client.connectShare("Public")
let files = try await client.listDirectory("Documents/Presentations")
print(files.map { $0.fileName })
let data = try await client.download(path: "Pictures/IMG_0001.jpg")
try await client.upload(data: data, path: "Documents/Presentations/Keynote.key")
Add the following line to the dependencies in your Package.swift
file:
dependencies: [
.package(url: "https://github.com/kishikawakatsumi/SMBClient.git", .upToNextMajor(from: "0.1.0"))
]
- macOS 10.15 or later
- iOS 13.0 or later
- NEGOTIATE
- SESSION_SETUP
- LOGOFF
- TREE_CONNECT
- TREE_DISCONNECT
- CREATE
- CLOSE
- FLUSH
- READ
- WRITE
- LOCK
- ECHO
- CANCEL
- IOCTL
- QUERY_DIRECTORY
- CHANGE_NOTIFY
- QUERY_INFO
- SET_INFO
To connect to macOS file sharing, you need to enable "Windows File Sharing" in the "Sharing" sytem settings in "File Sharing" section. This will enable NTLM v2 authentication on your macOS.
Open source projects thrive on the generosity and support of people like you. If you find this project valuable, please consider extending your support. Contributing to the project not only sustains its growth, but also helps drive innovation and improve its features.
To support this project, you can become a sponsor through GitHub Sponsors. Your contribution will be greatly appreciated and will help keep the project alive and thriving. Thanks for your consideration! ❤️