Latest 1.7.0
Homepage https://github.com/amosavian/AMSMB2
License LGPL-2.1
Platforms ios 9.0, osx 10.11, tvos 9.0
Frameworks Foundation
Authors

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 Version
Platform
License

Build Status
Release version
CocoaPods version
Carthage compatible

Install

Cocoapods / Carthage

Add this line to your pods file:

pod "AMSMB2"

Or add this to Cartfile:

github "amosavian/AMSMB2"

Manually

To have latest updates with ease, use this command on terminal to get a clone:

git clone https://github.com/amosavian/AMSMB2

You can update your library using this command in AMSMB2 folder:

git pull

if you have a git based project, use this command in your project’s directory to add this project as a submodule to your project:

git submodule add https://github.com/amosavian/AMSMB2

Then drop AMSMB2.xcodeproj to you Xcode workspace and add the framework to your Embeded Binaries in target.

Usage

Just read inline help to find what each function does. It’s straightforward. It’s thread safe and any queue.

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

import AMSMB2

class SMBClient {
    /// connect to: `smb://[email protected]/share`

    let serverURL = URL(string: "smb://XXX.XXX.XX.XX")!
    let credential = URLCredential(user: "guest", password: "", persistence: URLCredential.Persistence.forSession)
    let share = "share"

    func connect(handler: @escaping (_ client: AMSMB2?, _ error: Error?) -> Void) {
        let client = AMSMB2(url: self.serverURL, credential: self.credential)!
        client.connectShare(name: self.share) { error in
            handler(client, error)
        }
    }

    func listDirectory(path: String) {
        connect { (client, error) in
            if let error = error {
                print(error)
                return
            }

            client?.contentOfDirectory(atPath: path,
                                       completionHandler: { (files, error) in
                if let error = error {
                    print(error)
                    return
                }

                for entry in files {
                    print("name: ", entry[.nameKey] as! String,
                        ", path: ", entry[.pathKey],
                        ", type: ", entry[.fileResourceTypeKey] as? URLFileResourceType,
                        ", size: ", entry[.fileSizeKey] as? Int64,
                        ", modified: ", entry[.contentModificationDateKey],
                        ", created: ", entry[.creationDateKey]
                    )
                }
            })
        }
    }

    func moveItem(path: String, to toPath: String) {
        self.connect { (client, error) in
            if let error = error {
                print(error)
                return
            }

            client?.moveItem(atPath: path, toPath: toPath) { error in
                if let error = error {
                    print(error)
                } else {
                    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.
                client?.disconnectShare(completionHandler: { (error) in
                    if let error = error {
                        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.

Latest podspec

{
    "name": "AMSMB2",
    "version": "1.7.0",
    "summary": "Swift framework to connect SMB2/3 shares",
    "description": "This is small Swift library for iOS which wraps libsmb2 and allows to connect a SMB2/3 share and do file operation.",
    "homepage": "https://github.com/amosavian/AMSMB2",
    "license": {
        "type": "LGPL-2.1",
        "file": "LICENSE"
    },
    "authors": {
        "Amir Abbas Mousavian": "[email protected]"
    },
    "social_media_url": "https://twitter.com/amosavian",
    "swift_version": "4.1",
    "platforms": {
        "ios": "9.0",
        "osx": "10.11",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/amosavian/AMSMB2.git",
        "tag": "1.7.0"
    },
    "ios": {
        "vendored_libraries": "libsmb2/lib/libsmb2-ios.a"
    },
    "osx": {
        "vendored_libraries": "libsmb2/lib/libsmb2-macos.a"
    },
    "tvos": {
        "vendored_libraries": "libsmb2/lib/libsmb2-tvos.a"
    },
    "watchos": {
        "vendored_libraries": "libsmb2/lib/libsmb2-watchos.a"
    },
    "source_files": "AMSMB2/**/*.swift",
    "xcconfig": {
        "LIBRARY_SEARCH_PATHS": "$(PODS_ROOT)/AMSMB2/libsmb2/lib",
        "SWIFT_INCLUDE_PATHS": "${PODS_ROOT}/AMSMB2/libsmb2/**"
    },
    "preserve_paths": [
        "libsmb2/include/module.modulemap",
        "libsmb2/include/libsmb2-private.h",
        "libsmb2/include/smb2/libsmb2-raw.h",
        "libsmb2/include/smb2/libsmb2.h",
        "libsmb2/include/smb2/smb2-errors.h",
        "libsmb2/include/smb2/smb2.h"
    ],
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This