Kodeco Forums

Creating a Framework for iOS

In this tutorial, you'll learn how to build an iOS framework so you can share code between apps, modularize your code, or distribute it as a third-party library.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5109-creating-a-framework-for-ios

Are you sure that it’s needed to turn off “Inherit from target” and setting the module name manually in the Interface Builder? I am also developing on a framework with its own UI and don’t need to do that.

Hi @epileptickoala! Thanks for your question.

About the first question, the override is needed just if you see warning on the console. This in order to prevent problems with CocoaPods installation.

About the second, since the framework has been created after the showcase one, setting the module manually is necessary otherwise the custom control will not be available / visible in the showcase project. Are you able to elaborate on this or provide a sample project?

Thank you!

Hi, Thank you for the tutorial.

I’m confused on the the Clone the Repository section.

Why do we create a new directory at the root of KnobShowcase ?
This new directory named repo has a copy of the KnobControl framework.

This is confusing for a few reasons, we only updated
s.source = { :git => "URL", :tag => "1.0.0" }
in the copied KnobControl repo.

Do we not update the other version’s s.source ?

I hope my questions are clear, if not I would just love an overview of what is going on here.
How is the KnobShowcase project getting access to the KnobControl framework, this seems to be the main point of confusion for me.

Hi @ivanlares and sorry for the delayed response.

The Clone the Repository section allows you to move the KnobControl source into GitHub.

In fact, following the steps provided in Create a Repository, you will just create a repository that is cloned in your local machine.

Since the repository you’ve just cloned is empty (and your main goal is to make the framework accessible to everyone), you copy and paste the contents of the KnobControl directory into the new directory (called repo).

Updating the .podspec is required since if other devs want to use your framework, CocoaPods will resolve the update against that url.

It’s important to note that all the instructions in order to make the repo accessible through GitHub / CocoaPods could have been made in the root KnobControl source directory (avoiding to copy the source in the new repo directory). For example:

cd KnobControl
git init
git remote add origin "url"
git add .
git push -u origin master

As you can see, this procedure requires more steps than the previous ones.

Hope my explanation is clear.

Lorenzo

Hello. I keep getting errors when trying to pod install.

Analyzing dependencies
Fetching podspec for KnobControl from ../KnobControl
[!] No podspec found for KnobControl in ../KnobControl

I have checked and there is a podspec file in the directory.

Hi @thejackburton,

Could you tell me in which step of the tutorial you’ve encountered that problem?

Thanks,
Lorenzo

Hi @lorenzoboaro,

I am able to create the podspec file, make the edits, then I am able to run pod init. I edit the Podfile and then when I run pod install, that is when I receive the error.

Hi @thejackburton, can you provide me some additional info? Could you copy and paste your Podfile? Thanks, Lorenzo

This tutorial is a little wonky. I would suggest in the future that you keep it simple.

For example, for this tutorial, there is really no need to diverge into UI development (IBInspectable, etc.) or CocoaPods. The title doesn’t indicate that is actually three tutorials combined into one, which obfuscates what is needed to simply create a framework, archive it, and share the X.framework.

Hi @dnedrow! Your feedback it’s really appreciated. And I’m sorry that you’ve found the tutorial a little wonky. Right now what I can do is to take into account your critique for future tutorials. Thank you, Lorenzo

Regarding the tutorial instructions for access control changes, there seems to be an error at:
Note for value, the property will be public while the setter continues to be private.

However, the setter is called from KnobShowcase/ViewController, and needs to be public.

The solution code for Knob.swift seems to confirm this:

  public private (set) var value: Float = 0
  public func setValue(_ newValue: Float, animated: Bool = false) 

It seems to me the private (set) does nothing in this case. Am I missing something?

1 Like

Hi @ronzo99! Thanks for your feedback.

You should think of value as a property that can read but that cannot change (since it’s marked as private (set)). setValue is the method that allows to set the value property. The reason of this separation is due to the fact you can decide to apply an animation behavior when value changes.

Hope it makes more sense.

For any additional clarifications, please let me know.

Lorenzo

Hi, thank you for the nice and clear tutorial! What is the best way to distribute my framework hiding the source code?

Hi @andrea.gorrieri. Sorry for the delay.

You can find a useful approach at Hide implementation of swift framework when distributing | by Zhihui Tang | Medium.

Thanks, Lorenzo

Hi, Can you help me??

I like very much Swift, and this is the first time that I created a framework…

I have a project in Objective-C, and I created the framework in Swift, when I implement my framework on project and build on my phone, that’s Ok, but when I Archive project, I can’t upload to App Store, the button “Upload to App Store…” Not stayed active for me…

I just can export to Build Products or Archive, but not create IPA to Store or Fabric

When I remove my framework from project, the Archive action is right, I can send it to the Store and still export to adHoc among others.

Sorry for my bad english. :confused:

@lorenzoboaro Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @ruango2, sorry for the delay I’m writing to you.

Are you able to provide a screenshot of the problem you are encountering?

In addition, are you using CocoaPods to use the framework in your app?

Thanks, Lorenzo

image
I had faced this build failed problem whenever i try to build and run my project.

I found out what was wrong with my project. My Version of Deployment Target in KnobShowcase target was 12.0 while version of Deployment Target in KnobControl Framework was 12.1
So i reset the version in KnobShowcase, and it works!

I hope the reader who will read this tutorial should not make a same mistake that i had done while i was in this tutorial. I know it is a tiny thing but it kept me for 4 hours…

It was really a helpful tutorial!
Thank you, Lorenzo!

Considering how long and how messy and error prone this is, even with Swift and no “.h” header files, not sure it is legit to say “Easy” and “Framework” in the same tutorial. Creating object libraries under Mac OS Classic with C++ or C was trivial compared to this.