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")
![macOS File Browser App](https://private-user-images.githubusercontent.com/40610/350689922-5573ab34-645a-404e-b28f-182935b0badd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjYxNzAsIm5iZiI6MTcyNTU2NTg3MCwicGF0aCI6Ii80MDYxMC8zNTA2ODk5MjItNTU3M2FiMzQtNjQ1YS00MDRlLWIyOGYtMTgyOTM1YjBiYWRkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5NTExMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQxNzg3MjNhNzc1YzBhYTM4MTExODQxZmYyYzhkZDJhZDNmZmNiNjExM2MzNTA5ZWM1NmU1ZjY2MGYwNGI1ZjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.w5aIfC37o8UI7SQKBg-la9ocl6zVkqzAsMGVAI92Pwk)
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.
![macOS File Sharing settings](https://private-user-images.githubusercontent.com/40610/351448698-9d521df6-b899-4f10-ac8e-0dbbe371e5c2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjYxNzAsIm5iZiI6MTcyNTU2NTg3MCwicGF0aCI6Ii80MDYxMC8zNTE0NDg2OTgtOWQ1MjFkZjYtYjg5OS00ZjEwLWFjOGUtMGRiYmUzNzFlNWMyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5NTExMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY3YmZlNGFlNDhiNmFlYjViYzZlMjhmMGIwOTU5NzQwNzMzYzViMDRiOWZjYzA3NmIzOTEyODYzZjI0YWJlZjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.dVwoDqk5G1r8pA4ADrVVlNQiLvTITmbiC0fXo9DL8VU)
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! ❤️