How to Use Cocoapods When Creating a Cocoa Touch Framework

Can I use CocoaPods when creating a Cocoa Touch Framework?

Unfortunately CocoaPods doesn't support use with Cocoa Touch Framework target. I found a few references to this while digging through their issues on GitHub:

We don't really support integrating Pods into framework targets...

-neonichu on Nov 4, 2015

and

...in order for this to "just work", CP would need to do a recursive analysis of dependencies in your Xcode project and also somehow ensure that you would never use the build product in another context.

-neonichu on Jul 7, 2015



So far I've found two ways to deal with the issue:

The right way is to create a new pod spec for your framework and bring it in to your main project via CocoaPods. This resolves all of the problems CococaPods has with the dependency graph and is the recommended solution from the CocoaPods developers.

The easy way is to include the pods from your framework in your main project. This seems to work, but frankly I don't know why. This is the Podfile from my test project:

platform :ios, '9.0'
use_frameworks!

def myfirstframework_pods
pod 'Alamofire', '~> 3.0'
end

target 'MyApp' do
pod 'SwiftKeychainWrapper', '~>1.0'
myfirstframework_pods
end

target 'MyFirstFramework' do
myfirstframework_pods
end

Creating a CocoaPod from Cocoa Touch Framework

The reason I was getting "Use of unresolved identifier" errors when I was trying to use my public classes is because my header file mylibrary-Swift.h was not getting exposed properly.

I was able to fix this issue by naming my header file explicitly in my podspec like this:

s.public_header_files = "mylibrary.framework/Headers/mylibary-Swift.h"

After I pushed this new podspec I am able to use my public classes and methods.

Using cocoapods in a framework

If you are using Cocoapods on the application project you can create a private pod for the iOS framework. To do so you have to create a .podspec file in order to add the conflicting dependency. For Restkit the .podspec file would be like this:

Pod::Spec.new do |s|

s.name = "MyFramework"
...
s.dependency 'RestKit', '~> 0.23'

end

You can read more about these files here: http://guides.cocoapods.org/making/specs-and-specs-repo.html

After that you just have to add a dependency of your framework on the application project Podfile. You can do so through a local path or through a version control system.

pod 'MyFramework', :path => './../my-framework'

pod 'MyFramework', :git => 'https://url/to/my-framework.git', :tag => '0.0.1'

How to use Cocoapods in a Swift framework?

I found the solution, so I will let it here in case someone else has the same issue.

So, Cocoapods supports iOS frameworks since version 0.36, and as it's said in their blog, to use it in a framework, you just have to add this line in your Podfile:

use_frameworks!

After that, in a Swift framework, you don't need to include the .h files in your umbrella header, but you do need to import the module wherever you need it, for instance:

import MBProgressHUD

This is working for me, hope it helps!



Related Topics



Leave a reply



Submit