Proper Way of Editing a Cocoapod Library

Proper way of editing a CocoaPod Library

If you want to edit behaviour of the code in TheLibrary, then you should fork it and use your own code which you can edit as you wish. In case you want to update with new code that is released in the meantime you will have to merge from TheLibrary to your fork.

Doing it this way you will have full control over the code from the library.

However, if you want to add additional stuff to TheLibrary, you can inherit class you want to alter and use official Pod for that library. You will just add the functions you want to the subclass you created.

For the former you can create extension as well for the particular class where you will add your functionality, e.g. function.

One caveat the editing pod file - it will work on your machine until you run pod install or pod update. Then you will have original code without your edits. Also, possible team members won't have these changes, so avoid it at all costs.

Cocoapods: what is the best way to modify the resources

What I usually do is forking the 3rd party project (probably on GitHub) and push the changes to my fork. Then, on Podfile you just point to your repo:

platform :ios, '6.0'

pod 'MyAwesomeLib', :git => 'https://github.com/myuser/MyAwesomeLib'

Changing an imported library using cocoapods

@pgb and wattson provided me good information but ultimately the problem was in a combination of things.

I don't know why but it seems that cocoapods 0.22 handles headers differently. I uninstalled cocoapods and installed the version 0.20.2.

To check the version of cocoapods I have used gem query and I have removed the cocoapods with gem uninstall cocoapods and installed the cocoapods with gem install cocoapods --version 0.20.2.

I have used my podfile like this:

'WhirlyGlobe', :podspec => 'https://raw.github.com/tiagoalmeida/WhirlyGlobe/master/WhirlyGlobe.podspec'

Where podspec points to my new podspec. I made like this because I need to remove the :tag from the original podfile (otherwise it always points to the same spot) and this way I have more control over the file.

In the podspec I have changed the source:

s.source = { :git => "https://github.com/tiagoalmeida/WhirlyGlobe.git"} 

To point into my fork and removed the tag.

Thanks @pgb and @wattson for the attempts to help me. Upvoted both because they were both usefull.

How to make changes to cocoapods file?

You should show the code you wrote to setup your layout. This would make it easier to help you.

Put the code related to collectionView in the didSet block of your collectionView property.

The following code snippet should work for you:

@IBOutlet weak var collectionView: UICollectionView! {  
didSet {
let cardLayout = CardsCollectionViewLayout()
cardLayout.itemSize = CGSize(width: 50, height: 50)
collectionView.collectionViewLayout = cardLayout
collectionView.dataSource = self
collectionView.delegate = self
collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.isHidden = true
}
}

Do never change a file in a pod! Your changes would be lost when updating the pod!

If you ever need to change the behavior of a pod, you have options:

  • write an extension, if that suits your needs
  • use subclassing
  • if you need to make bigger changes, use a fork as you already tried
  • or you may add the pods source files to your projects code base. But I wouldn't recommend this, because that would make it difficult to profit from updates published to the pods source.

Editing locked files from a CocoaPods framework

You can not make changes in Original Pod file. If you want to add some more features then you have to fork that particular repo.

Follow steps to do so :

  1. Find library you want to use on Git.
  2. Fork library.
  3. Update Podfile which refer to your forked version.
    Suppose I want to fork GPUImage Library. Then point your fork in Podfile as given.

pod 'GPUImage', :git => 'https://github.com/UserName/GPUImage.git'


  1. Make your changes.
  2. Commit/push your changes to your forked repo.
  3. Submit a pull request to the original author of Library.
  4. If pull request is accepted and the pod is updated, revert back your Podfile to use the public pod.

You can create Category (Objective C) or Extension (Swift) to extend or add some features to existing class.

Modify an open source library installed with Cocoapods

You should add the CocoaPods on under Source Control. That means that you should add everything from your project under Source Control and ignore the files you don't want under .gitignore file.

Creating a .gitignore file

cd yourProjectDirectory
touch .gitignore
open -e .gitignore

On GitHub you a list of things that are recommended to be put on .gitignore, depending on your programming language that you are using.

Adding everything under Source Control

cd yourProjectDirectory
git add . //This line will add every recognized file under Repo

Now you are free on your customizations

You can make your own customizations and push the changes on repo. And if someone will add a new pod under podfile, then installing it, will not affect the current version of your file, CocoaPods automatically on the first run generates a podfile.lock, which keeps track of your Pods

More info: podfile.lock

How to edit locally a Cocoapods dependencies?

It may be a lot of work but forking is the best way forward. If you create a bugfix, then do the right thing and submit a Pull Request on GitHub.

If the project already has a PodSpec in the root, you don't have to create a new one to use your fork.

In your Podfile, include

pod 'YourLibrary', :git => 'http://github.com/yourname/yourlibrary', :commit => 'shahash'

Otherwise if you have a local installation you may just point at that

pod 'YourLibrary', :path => '~/git/yourlibrary'

Do Cocoa Pods Re-Install When Uploading To App Store?

Check that all steps are done from the following list:

  1. Change the source files from your forked pod project.
  2. Commit it & push to GitHub (if pod is hosted here).
  3. Update the Podfile within your main project to use forked pod.
  4. Run in terminal pod update PodToInstall.

Also CocoaPods and GitHub forks might be useful.

Right way to extend or customize cocoapods

As far as I know, CocoaPods are not intended to make any changes to frameworks and contribution to frameworks. They are a good way to copy something to your project only.

You can extend classes using categories for adding new methods and associated references for adding new variables. This will not affect the modularity of a pod/framework and you will not loose your changes.

You can also use composition or inheritance. It is a good practice to use composition rather than inheritance.

If you want to make a contribution to a third party library, probably, you should get a repository of that third party library separately, make changes and then make a pull request. Or you can use git submodules to add a third party library and bind it to it's own separate git repository, for instance in GitHub.

How can I open Podfile for editing

Open the Podfile with TextEdit
use terminal

 $ cd "your_project_location"
$ open -a TextEdit Podfile


Related Topics



Leave a reply



Submit