Group Group Group Group Group Group Group Group Group

Creating a Framework for iOS


#21

@cerniuk Thanks for your comment.

Please let me know what don’t you understand exactly or what you find complex when you get a chance. Thank you!


#22

Using this tutorial as a basis for a custom project and framework, I’m encountering an issue when trying to archive a project with an embedded binary .framework. The project works great when testing in the simulator, but once I switch to Generic iOS Device and select Product > Archive, all the classes within the .framework binary become opaque, triggering “MyClass is unavailable” compile errors. I need to archive this project for distribution via TestFlight.

To clarify, I have two projects. One is the pure Cocoa Touch Framework project that produces a .framework product. The second project is completely isolated. I am just linking in a copy of the framework file to the second project using the option outlined in the tutorial note (“you could just add the KnobControl.framework output.” approach).

This has all the smell of the fat framework problem that Internet folks complain about with Xcode framework output for iOS, but I’m not sure of the appropriate fix as most tutorials are heavily outdated, including the one here (the provided build run script does not work for me) - https://www.raywenderlich.com/2430-how-to-create-a-framework-for-ios

This is a great tutorial though! More like this are needed, and Apple sorely needs to make framework binaries and package management first class citizens in iOS.


#23

Thanks for your feedback @plasticbrain.

Maybe I haven’t understood your question correctly, but here you can find some useful info about stripping out simulator slices from your archive: Deep dive into Swift frameworks.

Hope it helps.


#24

[UPDATE] Solved! See my post below.

original ranty post:

@lorenzoboaro Thank you for this link. There’s some great information there. Unfortunately it only goes so far, as the script there (and in the aforementioned article on raywenderlich) is very specific and fails to work for crafting a universal framework file. I’m encountering build error after build error.

I even tried using just the source from this article, creating a .framework file and importing it in the use project. I cannot figure out the build magic needed.

An updated article focused just on creating and utilizing a .framework file for independent distribution is needed. Problems to solve in such an article:

  • Build an iOS .framework for independent distribution (e.g. on github).
  • Make a .framework that works only on iOS 11 or later targets (64-bit only).
  • Embedding and utilizing a .framework in an archived project, published in App Store or via TestFlight.

#25

@plasticbrain Are you able to provide a sample (e.g. GitHub)?


#26

@lorenzoboaro Here’s the project page for my Cocoa Touch Framework project: https://github.com/plasticbraindotcom/SnapNavigation

After several days of work on making a universal framework target, I finally managed to solve all my problems. I’ll try to outline it here for those suffering a similar fate.

Use this “iOS universal framework” article as the starting guideline for crafting your framework project: http://arsenkin.com/ios-universal-framework.html

For structural/organization, I followed some of Eric Cerney’s tips in his Building Reusable Frameworks talk, particularly as it relates to Info.plist: https://www.raywenderlich.com/4319-building-reusable-frameworks

To make it all work, I had to tweak the run script from the “iOS universal framework” article in a couple of ways. Since I’m only offering support for iOS 10.0+, which enforces 64-bit compliance, I had to remove older 32-bit architecture references: armv7, armv7s, i386. I was also encountering an aggravating build error which required that I prefix the build commands with “env -i” (problem, along with solution outlined in commend by Werner Altewischer on stackoverflow: https://stackoverflow.com/questions/51558933/error-unable-to-load-standard-library-for-target-arm64-apple-ios10-0-simulator?noredirect=1). Finally, I had to also sprinkle in the bitcode directive to bulletproof the binary for inclusion in TestFlight distributions.

Key edits to the runtime script:
env -i xcodebuild -target “${FRAMEWORK_NAME}” ENABLE_BITCODE=YES BITCODE_GENERATION_MODE=bitcode -configuration Release -arch arm64 only_active_arch=no defines_module=yes -sdk “iphoneos”

env -i xcodebuild -target “${FRAMEWORK_NAME}” ENABLE_BITCODE=YES BITCODE_GENERATION_MODE=bitcode -configuration Release -arch x86_64 only_active_arch=no defines_module=yes -sdk “iphonesimulator”

The result is that I now have a framework that [ostensibly] works if added manually to a project, or is added with CocoaPods or Carthage. Carthage in particular will utilize any pre-compiled .framework.zip added to a release version target on github, which was the start of the initial challenge for me. This seems to be good, but I’m still testing it. I think I have to utilize a finishing build run script for projects utilizing the .framework file when distributed via App Store / TestFlight (as outlined in the addendum in the “iOS universal framework” article).

This is a lot of song and dance just for packaging my code for distribution and sharing it with the world! Thanks for the nudging help, and I apologize for any ranting tone I had :). Apple really needs to provide support Swift Package Manager for iOS, watchOS, tvOS.


#27

Hi @plasticbrain! Thanks for sharing this info. It’s valuable and (for sure) it could help other people.

Lorenzo


#28

@lorenzoboaro Hello I have build a framework which uses storyboard… Now the client has our storyboard and he edited design in that file…How can he override the storyboard which is in our project into his new, edited one… I have tried many things and it is still now working… I have tried either building a compiled framework or not compiled… framework is distributed via cocoaPods… I feel desperate please help


#29

@wellbranding Sorry for the delay. Are you able to provide more info on this? What dow you mean with “the client has edited the Storyboard file”? Thanks, Lorenzo


#30

Hi, I have one doubt

Is it possible to use the external dependencies[like Alamofire] inside the Framework. How should we use locally and release the framework with external dependencies.


#31

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


#32

Hello, thanks for the article.

I have spent the last few days trying to get a framework project to work, in order to share code. However, i wanted to have some third-party dependencies in there, and also a prefix header file.

I have gotten it to work, but one real drawback of this approach, if i have understood it, is that every time i make a code change to my framework project, it will not be reflected in any other projects until i do a “pod update”., and then build? It’s kind of slow and error prone compared to just having all the code in one workspace. Or am i missing something?


#33

Hi, nice tutorial.

Do you know if it is possible use an image from a framework into the actual application in Storyboard? I haven’t managed to do it, only programatically with UIImage(named: String, in: Bundle?, compatibleWith: UITraitCollection?).