Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing swift code and maintaining the code in one place [framework/library]

I would like to create a Swift framework and import it into my other projects. Obviously, I would like to share my code by using the framework in question. I was only able to find one related question here

Create and import swift framework

and some tutorials on the internet which did not seem to produce anything useful for me. So here is what I have:

  1. An XCode project (with a .xcworkspace workspace generated by CocoaPods) that is the actual application that should use the framework.
  2. A "Cocoa Touch Framework" XCode project that is the framework.

What are the steps to include the framework (2) into the application (1)?

I tried using the method described in the linked question above, but upon building, it says it does not find the actual source files.

< unknown >:0: error: no such file or directory: '/path/to/project/MyFrameWork/SomeClass.swift'

Where /path/to/project/ is obviously just a placeholder...

Bounty goal:

Propose a viable option how I could share a set of classes in an efficient way. I need to be able to reuse code from one project easily and be able to maintain this code in one place. It also needs to be compatible with iOS7, so dynamic libraries probably won't do it for me. Any workflow that would allow me to do what I described above will be a winner. Thanks

like image 913
the_critic Avatar asked Apr 30 '15 18:04

the_critic


1 Answers

For the sake of helping others, here is a follow up on what I did:

Due to the fact that -- at the time of this writing -- iOS8 is used by about 81% of Apple users with mobile devices, I figured I would go the CocoaPods route. (I mention this because iOS8 is required to use dynamic Swift libraries) And because I start over with my project anyway, I figured, why not just start at iOS8...

I basically created a new library by doing something along those lines (notice: CocoaPods needs to be installed on your system!)

pod lib create <YourLibraryName>

And changing the contents accordingly of the .podspec file that was created in the process.

Add your source code to the folder that was created for your library (Or the source_folder you specified in the .podspec file) and create a git repo from it. Now you will be able to use this library in every project that also uses CocoaPods by adding

pod 'YourFrameWork', :git => 'https://path/to/your/repo.git'

Hope it helps.

like image 71
the_critic Avatar answered Nov 04 '22 16:11

the_critic