In Java, for instance, importing a library only requiere to put a .jar file in our project. In (my loved) ActionScript, we just put a SWC in the correct place, but in Swift that issue is a little more complicated, and now I want to show how I made it after a lot of battles with Xcode, because I'm a noob on it.
First of all: What do we want?
We want to write a custom library that can be used in any project with Swift in Xcode.
What do we need to use?
Xcode and a little bit of Swift just for this tutorial.
Ok, let's do it...
I like describing first a checklist of all the process...just for being organized:
- We are going to create a Single View Application (SVA) for importing our future custom library (from now, Framework).
- Next, we are going to create a Cocoa Touch Framework project. This is the final Framework that we are going to need and use in the project created before.
- Add a Swift file to write our code.
- Modify Build Settings.
- Build the Framework project.
- Drag and drop the file created before into the SVA project.
- Configure the SVA to use the Framework properly.
- Use the Framework
- Bonus: Some problems in the middle of the process
1. We are going to create a Single View Application (SVA) for importing our future custom library (from now, Framework)
Define a folder where to save it and that's all for now

2. Next, we are going to create a Cocoa Touch Framework project. This is the final Framework that we are going to need and use in the project created before

I use another random (and original) name in this project: MyCustomFramework

We can save it wherever we want

Now, we are going to write our custom class (or classes, if we need it)
3. Add a Swift file to write our code

Select Swift type

I gave a custom name

Finally, write the Class...obviously, this case is for testing purposes

4. Modify Build Settings
As we can see, everything made by us so far it's normal and in fact, there's anything to code left, but, there are some little things to do before we can use the Framework.
For now, in the Framework project, we need to set a path.
Click on the project name, and select "Build Settings" tab. Then localized through the search tool a property named "Framework Search Paths".
Double click just on the second column and a simple window it's shown up. Here, we must click the + button (bottom left corner), and write $(PROJECT_DIR). Then, click outside the window.
Now it must be seen like this.
5. Build the Framework project
That's it. CMD+B and we get the .framework file necessary to import from any other project.
6. Drag and drop the file created before into the SVA project
We first need localize the .framework file, 'cause it's necessary to drag and drop it.
It looks like this
Now, we need to drag and drop that file to our first created project, I mean, RandomApp. I drop it over the project name and then, another window is shown up, and I set it right as below
7. Configure the SVA to use the Framework properly
Now, this part it's about configuring our RandomApp to access to Framework.
First, click on the project name and select General tab. So, localize the "Embedded Binaries" section, and clic the + icon.
Add MyCustomFramework.framework on the popup.
Now, it looks like this:
If we want, we can remove the leftover reference on "Linked Frameworks and Libraries" section.
At this moment, we get all necessary things to work.
8. Use the Framework
This is my favorite part :) ...now we are ready to use our custom Framework.
First, we import (from the file we created) the Framework, just like any other library.
Then, we write a variable just for testing, like I show below:
That's all, there's no more settings to do or to code.
Bonus: Some problems that can make it difficult
It looks like easy, but it's possible that in the middle of the process we get one or more fails, and I get 3 different errors:
Error 1: 'MyCustomClass' initializer is inaccessible due to 'internal' protection level
This happened when we don't write the init function inside the class.
Error 2: Use of undeclared type 'MyCustomFramework'
This error particularly makes me nuts, 'cause when we compile de Framework, the target device must be the same as the target device of our RandomApp.
So, below it's the wrong way, 'cause the device target doesn't match:
Both must be the same target. But it won't go to work neither if we select Generic iOS Device, unless we get a developer certificate from Apple. That's another story.
Error 3: dyld: Library not loaded: @rpath/MyCustomFramework.framework/MyCustomFramework
Solution it's more simple, we just need to ensure that in "Embedded Binaries" section, the Framework has been added.
I hope this tutorial to be useful to anybody that require to import a Framework into his own project.
















