7

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

the_critic
  • 12,720
  • 19
  • 67
  • 115

3 Answers3

2

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.

the_critic
  • 12,720
  • 19
  • 67
  • 115
0

I don't know if this is the answer but I will tell it anyway. Create framework project in Xcode and put it on github. Then you can create framework with Carthage

mustafa
  • 15,254
  • 10
  • 48
  • 57
  • Thanks for your answer mustafa, I really considered doing this... until I found out that it only supports iOS8 upwards ... – the_critic May 03 '15 at 01:03
  • Then there is no option for you. Because you can't create static swift library. – mustafa May 03 '15 at 07:30
  • Are you sure ? I just need to be able to share code between my projects, why in the name of God is it so difficult/impossible :( ? – the_critic May 03 '15 at 09:50
0

Here is a good tutorial I found.

What you have to do ?

[sudo] gem install cocoapods --pre

The .podspec file

Pod::Spec.new do |s|
  s.name = 'MotionKit'
  s.version = '0.6'
  s.license = 'MIT'
  s.summary = 'CoreMotion Made insanely simple'
  s.homepage = 'https://github.com/MHaroonBaig/MotionKit'
  s.social_media_url = 'https://twitter.com/PyBaig'
  s.authors = { 'Haroon Baig' => 'haroon.prog@gmail.com' }
  s.source = { :git => 'https://github.com/MHaroonBaig/MotionKit.git', :tag => s.version }

  s.ios.deployment_target = '8.0'

  s.source_files = 'MotionKit/*.swift'

  s.requires_arc = true
end

Now Test your newly created Pod

Now is the time to test your pod, to make sure it works fine. Quickly create the pod file into your test project and add the following line to it.

pod '[s.name]', :git => '[s.source]'

in your case:

pod 'MotionKit', :git => 'https://github.com/MHaroonBaig/MotionKit.git'
Michael
  • 32,527
  • 49
  • 210
  • 370