How to Add Openssl to a Swift Project

How to add OpenSSL to an Xcode project

You will need to compile and link it yourself, and your app needs to ship it. If the license of your app and OpenSSL's license are compatible, you may use static linking. Otherwise you will need to dynamically link it.

There are a few documents describing the process and build scripts that you can find with Google searches. For iOS, there's even a Github project. I didn't copy the contents of those documents here since it's too much and it's a moving target.

You can also install OpenSSL with Homebrew. If you just want to have your app run on your Mac and you don't want to distribute it, this is the easiest way: you just need to link it. But if you want to distribute your app, you would need to copy the library/libraries to your app bundle and make sure the the linker finds it there. This also has the disadvantage that there's a possible "disconnect" between your app and the OpenSSL version: if in one year, you update OpenSSL with Homebrew and want to compile/link an older version of your app against the very same OpenSSL version as you've used at that time, you have a problem.

Installing OpenSSL library for Xcode

OK, how to build and install it....

It might be easier to use a pre-built version of OpenSSL for iOS. You can find one at this Github account. The OpenSSL from that Github are multi-arch. They have ARMv7, ARMv7s, ARM64, and i386. That means they work with devices and simulators.

Download either OpenSSL 1.0.1e or 1.0.1f. Install it in a location like /usr/local/ssl/ios.

Then, add the headers to your Xcode project. They are located in /usr/local/ssl/ios/include:

Sample Image

Finally, add the multi-arch libs (libcrypto.a and libssl.a) to your Xcode project. They are located in /usr/local/ssl/ios/lib:

Sample Image

Adding openSSL to Swift OS X project for receipt validation

I have finally figured out the source of the compiler errors. XCode is very picky about where the openssl headers are. If I link directly to the header directory entitled "openssl" I get the compiler errors. If I link to the parent folder entitled "include", no errors!

I do not understand this behavior, but at least now I have a solution. Thanks to the instructions on Receigen for adding OpenSSL as a static library, which I finally followed precisely, I was able to discover the source of the error.

How to include OpenSSL on an iOS project in a way that works

The easiest solution would be to use CocoaPods - there is an OpenSSL pod...

OpenSSL fails to compile for swift framework

I fixed the compilation issue and was able to access the OpenSSL API's.

I had to make a few changes to the module map file.

//
// module.modulemap
// NiksWay
//
// Created by Suraj Gaikwad on 14/03/22.
//

framework module NiksWay {
umbrella header "NiksWay.h"

export *

module * { export * }

explicit module OpenSSL {

header "pkcs12.h"
header "pem.h"
header "opensslv.h"
header "err.h"

link "libcrypto"
link "openssl"

export *
}
}

Using OpenSSL cocoa pod in iOS project

1) I have created sample project

2) Added pod 'OpenSSL', '~> 1.0'
3) Closed project and then opened Project_Name.xcworkspace

4) used #import <OpenSSL/bio.h>
5) Command + Shift + K (for cleaning project)

6) Command + Shift + B (for building it)

It worked for me on Xcode7.2. Try this.

Non-modular headers of OpenSSL library when using modulemap for Swift framework

In the end I couldn't find any better solution than listing all necessary headers into module map and rewriting include macros of the SSL library files from <> syntax to just plain include. I used this little shell script to help me with that:

sed -E -i '' 's/#[[:space:]]*include <openssl\/(.*).h>/#include \"\1\.h"/' $SCRIPT_DIR/../Libraries/openssl/include/openssl/*.h


Related Topics



Leave a reply



Submit