In iOS application development, it was a specification to upload a file to the server by SFTP. The library used this time is NMSSH. Other than this, I could not find it.
First of all, I stumbled here. See) https://qiita.com/spring_i/items/181bc3c05142d1f80d93
$ sudo gem install -v1.8.4 cocoapods -n /usr/local/bin
$ pod setup
The Xcode project is already created.
$ cd [Project name]
$ pod init
A Podfile will be generated, so edit it.
Podfile
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'SampleProject' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!
  # Pods for GSApp
  pod 'NMSSH'
end
Install command
$ pod install
Start the project by double-clicking the generated ** [project name] .xcworkspace **.
Library import in the target file
ViewController.swift
imprt NMSSH
Creating a dummy file for uploading
ViewController.swift
        do {
            let fileManager = FileManager.default
            let doc = try fileManager.url(for: .documentDirectory,
                                          in: .userDomainMask,
                                          appropriateFor: nil, create: false)
            let path = doc.appendingPathComponent("muyFile.txt")
            let data = "Hello, world".data(using: .utf8)
            fileManager.createFile(atPath: path.path, contents: data, attributes: nil)
        } catch {
            print(error)
        }
ViewController.swift
            let host = "[IP address]"
            let username = "[username]"
            let password = "[password]"
            let remotePath = "[Destination file path]"
            let session = NMSSHSession.connect(toHost: host, withUsername: username)
            if (session.isConnected) {
                session.authenticate(byPassword: password)
                if (session.isAuthorized) {
                    session.channel.uploadFile(path.path, to: remotePath)
                }
            }
            session.disconnect()
I'm connecting onClick with a button.
ViewController.swift
//
//  ViewController.swift
//  SampleProject
//
//  Created by SankoSC on 2020/12/21.
//
import UIKit
import NMSSH
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBAction func onClick(_ sender: Any) {
        sftp()
    }
        
    func sftp() {
        do {
            let fileManager = FileManager.default
            let doc = try fileManager.url(for: .documentDirectory,
                                          in: .userDomainMask,
                                          appropriateFor: nil, create: false)
            let path = doc.appendingPathComponent("muyFile.txt")
            let data = "Hello, world".data(using: .utf8)
            fileManager.createFile(atPath: path.path, contents: data, attributes: nil)
        
            let host = "[IP address]"
            let username = "[username]"
            let password = "[password]"
            let remotePath = "[Destination file path]"
            let session = NMSSHSession.connect(toHost: host, withUsername: username)
            if (session.isConnected) {
                session.authenticate(byPassword: password)
                if (session.isAuthorized) {
                    session.channel.uploadFile(path.path, to: remotePath)
                }
            }
            session.disconnect()
        } catch {
            print(error)
        }
    }
}
        Recommended Posts