I'm inspired Daniel Steinberg, Jeff Hui, and Hiroki Kato's session on try! Swift,
and wanted to create library to apply MVVM architecture on UITableView.
But I haven't create OSS repository, so I started to learn how to create OSS repository and support Carthage/Cocoapods.
(The framework which I created is hosted here)
What I did at this post
- Create framework
- Import it from iOS Application
- Support Carthage
- Support CocoaPods
At first, I created Cocoa Touch framework.
That content is not important, so I created
HelloWorld.framework which include only
Hello class and
Import it from iOS Application
Secondly, build it and get HelloWorld.framework.
Then I added it on newly created iOS application project, and call it as below.
We can confirm it works as expected because "Hello world" is printed on console log.
add public modifier on each class and method what you want to allow access
Swift class/method default modifier is internal, so we can't access class/method from outside of module(in this case, framework is one module).
Thus we have to add public modifier or we get errors as below.
If class modifier is internal, we can't access Hello class from outside of framework. If class modifier is public and method modifier is internal, we can access Hello class, but can't access world method.
Build framework with device, and run iOS Application on device
When we build framework, it only supports real device architecture.
(For more details about following commands, refer this blog post) So it occurs error as below when we try to run it on iOS Simulator. We can solve this problem with Carthage / CocoaPods.
What we should do to support Carthage, make framework build scheme shared one, and run following command.
carthage build --no-skip-current
We can get file named LIBRARY_NAME.podspec with following command.
pod spec create LIBRARY_NAME
So, what we have to do is run
pod lib lint command and fix warnings/errors.
- Libraries written in Swift only supports iOS8 and later, we have to add it on podspec.
- source_files doesn't include .swift as default, we have to add it on podspec.
- You should be careful about access modifier when you create framework.
- To create framework is useful for understanding access modifier, and how compiler works with architecture.