Skip to content

Repository files navigation

AMSMB2

This is small Swift library for iOS, macOS and tvOS which wraps libsmb2 and allows to connect a SMB2/3 share and do file operation.

Swift VersionPlatformLicenseRelease version

Swift Version CompatibilityPlatform Compatibility

Getting Started

To use AMSMB2, add the following dependency to your Package.swift:

dependencies:[.package(url:"https://github.com/amosavian/AMSMB2",.upToNextMinor(from:"3.0.0"))]

You can then add the specific product dependency to your target:

dependencies:[.product(name:"AMSMB2",package:"AMSMB2"),]

Usage

Just read inline help to find what each function does. It's straightforward. It's thread safe.

To do listing files in directory and file operations you must use this template:

import AMSMB2
classSMBClient:@uncheckedSendable{
/// connect to: `smb://guest@XXX.XXX.XX.XX/share`
letserverURL=URL(string:"smb://XXX.XXX.XX.XX")!
letcredential=URLCredential(user:"guest", password:"", persistence:URLCredential.Persistence.forSession)letshare="share"
lazy privatevarclient=SMB2Manager(url:self.serverURL, credential:self.credential)!
privatefunc connect()asyncthrows->SMB2Manager{
// AMSMB2 can handle queueing connection requests
tryawait client.connectShare(name:self.share)returnself.client
}func listDirectory(path:String){Task{do{letclient=tryawaitconnect()letfiles=tryawait client.contentsOfDirectory(atPath: path)forentryin files {print("name:",entry[.nameKey]as!String,", path:",entry[.pathKey]as!String,", type:",entry[.fileResourceTypeKey]as!URLFileResourceType,", size:",entry[.fileSizeKey]as!Int64,", modified:",entry[.contentModificationDateKey]as!Date,", created:",entry[.creationDateKey]as!Date)}}catch{print(error)}}}func moveItem(path:String, to toPath:String){Task{do{letclient=tryawaitself.connect()tryawait client.moveItem(atPath: path, toPath: toPath)print("\(path) moved successfully.")
// Disconnecting is optional, it will be called eventually
// when `AMSMB2` object is freed.
// You may call it explicitly to detect errors.
tryawait client.disconnectShare()}catch{print(error)}}}}

License

While source code shipped with project is MIT licensed, but it has static link to libsmb2 which is LGPL v2.1, consequently the whole project becomes LGPL v2.1.

You must link this library dynamically to your app if you intend to distribute your app on App Store.

About

Swift framework to connect SMB2/3 shares

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages