Skip to content

Repository files navigation

SMBClient

Test

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.

Usage

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
letclient=SMBClient(host:"198.51.100.50")tryawait client.login(username:"alice", password:"secret")tryawait client.connectShare("Public")letfiles=tryawait client.listDirectory("")print(files.map{ $0.fileName })tryawait client.disconnectShare()tryawait 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
letsession=Session(host:"198.51.100.50")tryawait session.connect()tryawait session.negotiate()tryawait session.sessionSetup(username:"alice", password:"secret")tryawait session.treeConnect(path:"Public")letfiles=tryawait session.queryDirectory(path:"", pattern:"*")print(files.map{ $0.fileName })tryawait session.treeDisconnect()tryawait session.logoff()

Log in to the remote SMB server

letclient=SMBClient(host:"198.51.100.50")tryawait client.login(username:"alice", password:"secret")

List share drives

letshares=tryawait client.listShares()print(shares.map{ $0.name })

Connect to a share drive

tryawait client.connectShare("Public")

List files in a directory

letfiles=tryawait client.listDirectory("Documents/Presentations")print(files.map{ $0.fileName })

Download a file

letdata=tryawait client.download(path:"Pictures/IMG_0001.jpg")

Upload a file/directory

tryawait client.upload(data: data, path:"Documents/Presentations/Keynote.key")

Example Applications

Network File Browser for macOS

macOS File Browser App

Network File Browser for iOS

iOS File Browser AppiOS File Browser App

Installation

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"))]

Supported Platforms

  • macOS 10.15 or later
  • iOS 13.0 or later

Supported SMB Messages

  • 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

FAQ

How to connedct to macOS file sharing?

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

Supporters & Sponsors

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! ❤️

About

Swift SMB client library and iOS/macOS file browser applications.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages