読者です 読者をやめる 読者になる 読者になる

How to start OSS(create Sample framework, support CocoaPods and Carthage)

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

create framework

At first, I created Cocoa Touch framework.
That content is not important, so I created HelloWorld.framework which include only Hello class and world method. f:id:nerd0geek1:20160320231157p:plain

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. f:id:nerd0geek1:20160320231209p:plain

Caution

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. f:id:nerd0geek1:20160320231227p:plain If class modifier is public and method modifier is internal, we can access Hello class, but can't access world method. f:id:nerd0geek1:20160320231237p:plain

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) f:id:nerd0geek1:20160320231415p:plain So it occurs error as below when we try to run it on iOS Simulator. f:id:nerd0geek1:20160320231245p:plain We can solve this problem with Carthage / CocoaPods.

Support Carthage

What we should do to support Carthage, make framework build scheme shared one, and run following command. f:id:nerd0geek1:20160321005426p:plain

carthage build --no-skip-current

Carthage solves problem related to iOS Simulator architecture with above command, so framework generated by it includes i386, x86_64 architecture. f:id:nerd0geek1:20160320231609p:plain

CocoaPods

We can get file named LIBRARY_NAME.podspec with following command.

pod spec create LIBRARY_NAME

f:id:nerd0geek1:20160321011507p:plain So, what we have to do is run pod lib lint command and fix warnings/errors.

Caution

  • 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. f:id:nerd0geek1:20160321012015p:plain

Recap

  • 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.

Reference