iOS: File upload with SFTP

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.

Installing CocoaPod

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

Install MNSSH

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

Library import

Start the project by double-clicking the generated ** [project name] .xcworkspace **.

Library import in the target file

ViewController.swift


imprt NMSSH

Create dummy file to upload

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)
        }

upload

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()

Sample source code

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

iOS: File upload with SFTP
File upload with Spring Boot
How to achieve file upload with Feign
CarPlay starting with iOS14
I tried to implement file upload with Spring MVC
File upload with Spring Boot (do not use Multipart File)
csv file output with opencsv
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
Overwrite upload of file with the same name with BOX SDK (java)
Automatic file upload with old Ruby gem What to do with Watir
How to realize huge file upload with Rest Template of Spring
Upload a file using Java HttpURLConnection
Minimal Rails with reduced file generation
Read dump file with Docker MySQL
Create an excel file with poi
Implement file download with Spring MVC
EXCEL file update sample with JAVA