How to include a Library into my Swift Project?
Asked Answered
N

2

8

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.

Nertie answered 16/3, 2017 at 19:19 Comment(1)
You can refer to this tutorial to successfully include (i.e import) library (i.e framework) into a project. Obviously you will be able to have the updated framework every time you run carthage update command in terminalUpstream
E
10

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.

Essex answered 16/3, 2017 at 19:36 Comment(22)
bu wheres the advantage against download "it" and include it in the swift project?Nertie
@Nertie tons of advantages: 1. Periodical improvements/bugfixes from creator and community 2. Language changes and if you migrate to a new version and the lib will not be updated you will have to download it again, drag to the project etc. 3. Once you'll get used to pods and terminal you'll be adding frameworks to your project in a few minutes and this will exclude lots of error that may occur while you're adding the library manually. 4. Via pod file you can easily manage versions of all your libraries, remove unwanted etc. There's more but I advise you to discover it on your own ;]Essex
but I have a few questions: 1. what is a pod file? 2. should i write all the code you have written in the terminal?Nertie
1. Basically a text file named Podfile, generated after you do the "pod init" command in terminal. 2. Almost, I've updated the answer a bit so that it's more simple to understand. It's just a few lines of code in terminal ;)Essex
but when i type sudo gem install cocoapods in terminal it wants a passwortNertie
and on the page of cocoapods cocoapods.org is written you need Ruby, I don't know what this isNertie
@Nertie yes, "sudo" means super user do, it needs your administrator rights to perform action. guides.cocoapods.org/using/getting-started.html, apple.stackexchange.com/questions/14421/…Essex
@Nertie I bet you have ruby on your machine, you just didn't realize it ;] en.wikipedia.org/wiki/Ruby_(programming_language)Essex
I am an Administrator, I was only so dumb that I don't realised, I must give in my administrator passwortNertie
Should the link to the project be the .xcodeproj or the folder?Nertie
@Nertie the folderEssex
but when I make cd /Then the folder/ and enter and in the next line pod init nothig happens, and there is no new data in the folderNertie
@Nertie you need to type in existing directory. And if there's no project directory you should create it via finder or terminal (with mkdir command). Please also setup pods, I've updated answer a little, it's more detailed now.Essex
I have made all as you have said, and than I had a file named Podfile. Then I have written it in this, what you have said. And I have written in terminal pod install but I became the error "[!] Unable to add a source with url https://github.com/CocoaPods/Specs.git named master. You can try adding it manually in ~/.cocoapods/repos or via pod repo add.". And now I have an folde Pods with 3 empty folders.Nertie
@Nertie you did not setup pods properly or something went wrong. Go to ~/.cocoapods/repos and run "git clone github.com/CocoaPods/Specs.git master" in terminal.Essex
I think I have the error. I have repeated al, but I have forgotten to change the Podfile, and the .xcworkspace came. But when I uncomment the one line and write the others he is loading big mass of datas from github.com/CocoaPods/Specs.git and no .xcworkspace is coming.Nertie
The error comes because of the text I should write in te podfile data. I made all again, only I didn't wrote this text (but I have uncommented that one line) and it worked well and there stood this "[!] Please close any current Xcode sessions and use TestLöschen.xcworkspace for this project from now on. Pod installation complete! There are 0 dependencies from the Podfile and 0 total pods installed." and I have the right data, but I don't have the library (0 pods). ||||||||| Could it be you have written a little false text to me I should write?Nertie
Every one of these relies on some third-party utility, and doesn't say how to do it natively.Miracle
@Miracle The best option is an automated option, why should you update something manually? Cocoapods is one of the best options because it's highly supported (large community) and very simple. While the project doesn't suffer from build-time issues it's a perfect tool. "doesn't say how to do it natively" SPM is native, but using it for iOS requires workarounds, it's good for Server-Side Swift development though. If you're talking about manual integration then it's a bad idea, think of all the libraries you need to update manually and its highly error prone.Essex
Several reasons. First, I may want to ensure that I'm linking against a frozen codebase that's not going to change. Second, I need to understand exactly what's going on during the project build. Relying on automation before you know how to do something manually ensures that you're going to be helpless when that automation isn't available.Miracle
@Miracle You can freeze framework's code by defining a version number in PodFile (example: pod 'Alamofire', '4.7.0'). If you want to know what's going on during the project build look into Xcode Build Phases and CocoaPods documentation. I mean you can use only text editor for writing code but why not use IDE? You can make builds manually, but why not use CI tools? That's my point. On the other hand I agree it's way better to know how things workEssex
Thanks. I have included the external projects in my Xcode project and set them as dependencies, and the whole thing's building now.Miracle
B
0

You can drag the CSVImporter.xcodeproj and drop it into your project, also include CSVImporter in the embedded binaries.

Brazell answered 16/3, 2017 at 20:6 Comment(3)
What do Zoe mean with drop it in the project? I don't know where to do this!Nertie
1. *you 2. what do you mean with include CSVImporter in the embedded binaries? Could you explain it for me?Nertie
Are you using XCode?Brazell

© 2022 - 2024 — McMap. All rights reserved.