How to use libssh2 in a SwiftUI app?

I want to use sftp functionality in my SwiftUI app and I have decided to try and do it using plain libssh2 (ie. without an initial wrapper or anything). libssh2 is written in C.

What are the steps I need to take in order to start calling the libssh2 C functions in my SwiftUI app?

Answered by DTS Engineer in 795939022

Swift can, in general, call C APIs directly. However, there are numerous challenges:

  • If the C library doesn’t already have a module map, you have to create one.

  • You have to build the C code into some sort of library. This is especially tricky if you’re targeting, say, iOS, where you have to convince the C code’s build system, running on the Mac, to output a library for a different platform.

  • Indeed, if you’re targeting a non-Mac platform you have to build the C code multiple times, once for the target platform and once for the simulator.

  • You then have to package everything up so that Xcode can use that library.

There are lots of different ways to achieve this. And, as the best approach very much depends on the library you’re trying to use, there are lots of different discussions about this from lots of different folks. For example, I was having a similar conversation to this with someone last week.

If all this sounds like too much, I recommend that you search for an existing Swift package that has the functionality you need. If I were in your shoes I’d reach for SwiftNIO SSH but there are alternatives.

Even if you decide not to use someone else’s package, you can still look at how they’ve handled the various challenges I’ve outlined above.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift can, in general, call C APIs directly. However, there are numerous challenges:

  • If the C library doesn’t already have a module map, you have to create one.

  • You have to build the C code into some sort of library. This is especially tricky if you’re targeting, say, iOS, where you have to convince the C code’s build system, running on the Mac, to output a library for a different platform.

  • Indeed, if you’re targeting a non-Mac platform you have to build the C code multiple times, once for the target platform and once for the simulator.

  • You then have to package everything up so that Xcode can use that library.

There are lots of different ways to achieve this. And, as the best approach very much depends on the library you’re trying to use, there are lots of different discussions about this from lots of different folks. For example, I was having a similar conversation to this with someone last week.

If all this sounds like too much, I recommend that you search for an existing Swift package that has the functionality you need. If I were in your shoes I’d reach for SwiftNIO SSH but there are alternatives.

Even if you decide not to use someone else’s package, you can still look at how they’ve handled the various challenges I’ve outlined above.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to use libssh2 in a SwiftUI app?
 
 
Q