How to include a Library into my Swift Project?

Blub picture Blub · Mar 16, 2017 · Viewed 11.3k times · Source

I want to read a .csv data in Swift, so I have informed me how to make this. Finally I got into this: https://github.com/Flinesoft/CSVImporter The CSVImporter.

But it says: "You can of course also just include this framework manually into your project by downloading it".

That would be OK, but there are several folders and as I have never importer a library into Swift before, I don't know what to download and where I should include it into my project.

I hope, anybody can help me. Thanks.

Answer

Oleh Zayats picture Oleh Zayats · Mar 16, 2017

The best way to import third-party dependencies is via dependency managers: CocoaPods/Carthage/SPM, you will be able to update a lib without headache.

1. CocoaPods [Offical Guide]

This is by far the easiest way to go...

Open terminal

Install CocoaPods (type in terminal):

sudo gem install cocoapods

Sudo means "super user do", it will require your password. Enter when requested.

Next, you need to setup the cocoapods master repo. Type in terminal:

pod setup --verbose // verbose option logs the setup progress

then create a pod file in your project directory (you can type "cd " and drag the project folder to the terminal window if your not comfortable with writing the path):

cd User/Projects/YourProject // make way to your project dir

pod init 

then inside the Podfile (find it in project directory) insert:

platform :ios, '8.0'
use_frameworks!

target 'YouAppTarget' do
    pod 'CSVImporter', '~> 1.7'
end

(Uncomment platform :ios, '8.0' Uncomment user_frameworks! if you're using Swift)

Run (while in your project dir):

pod install

Xcode .xcworkspace file will be generated, thats it, you can open it and use the framework ;]

later on you can update lib with:

pod update

2. Carthage [Offical Guide]

Steps to install a framework via Carthage are pretty similar to CocoaPods, follow the 'Offical Guide' link to see what's the differences are. A downside about this dependency manager is that not all libs are available. Some are only available for CocoaPods, but majority of new ones are supporting Carthage. P.S. Here's a good article on the differences.

3. Swift Package Manager [Offical Overview/Guide]

SPM is a native dependency manager, it's cross-platform, officaly-supported by Apple, decentralized and open-source.

It was planned as a replacement for CocoaPods and Carthage so I'd give it a try too.