LoginSignup
11
10

More than 5 years have passed since last update.

自作FrameworkをCocoaPodsで管理する

Posted at

アプリケーション固有では無いものの、接続するHWだったりに依存するものはFrameworkとして切り出したかったりします。
しかし、ただFrameworkとして切り出しておいただけだと、利用する側の取り込みや構成管理が煩雑になってしまいます。

そこで、CocoaPodsのライブラリ(Pod)としてFrameworkを作成し、利用する側はPodfileに依存関係を追加することで取り込めるようにしてみます。

pod lib create LIBRARY_NAME とコマンドを実行すると雛形が作成されます。
今回はSwiftで、デモアプリを含む構成で作成しました。

$ pod lib create PodLibExample
Cloning `https://github.com/CocoaPods/pod-template.git` into `PodLibExample`.
Configuring PodLibExample template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide:
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )


What language do you want to use?? [ Swift / ObjC ]
 >
swift
Would you like to include a demo application with your library? [ Yes / No ]
 >
yes
Which testing frameworks will you use? [ Quick / None ]
 > None



Would you like to do view based testing? [ Yes / No ]
 > No

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `PodLibExample` from `../`
Downloading dependencies
Installing PodLibExample (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `PodLibExample.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'PodLibExample/Example/PodLibExample.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

作成されたディレクトリ構造は以下の通りです。

.
├── Example
│   ├── PodLibExample
│   │   ├── AppDelegate.swift
│   │   ├── Base.lproj
│   │   │   ├── LaunchScreen.xib
│   │   │   └── Main.storyboard
│   │   ├── Images.xcassets
│   │   │   └── AppIcon.appiconset
│   │   │       └── Contents.json
│   │   ├── Info.plist
│   │   └── ViewController.swift
│   ├── PodLibExample.xcodeproj
│   │   ├── project.pbxproj
│   │   ├── project.xcworkspace
│   │   │   └── contents.xcworkspacedata
│   │   ├── xcshareddata
│   │   │   └── xcschemes
│   │   │       └── PodLibExample-Example.xcscheme
│   │   └── xcuserdata
│   │       └── tis.xcuserdatad
│   │           └── xcschemes
│   │               └── xcschememanagement.plist
│   ├── PodLibExample.xcworkspace
│   │   ├── contents.xcworkspacedata
│   │   └── xcuserdata
│   │       └── tis.xcuserdatad
│   │           └── UserInterfaceState.xcuserstate
│   ├── Podfile
│   ├── Podfile.lock
│   ├── Pods
│   │   ├── Headers
│   │   ├── Local\ Podspecs
│   │   │   └── PodLibExample.podspec.json
│   │   ├── Manifest.lock
│   │   ├── Pods.xcodeproj
│   │   │   ├── project.pbxproj
│   │   │   └── xcuserdata
│   │   │       └── tis.xcuserdatad
│   │   │           └── xcschemes
│   │   │               ├── PodLibExample.xcscheme
│   │   │               ├── Pods-PodLibExample_Example.xcscheme
│   │   │               ├── Pods-PodLibExample_Tests.xcscheme
│   │   │               └── xcschememanagement.plist
│   │   └── Target\ Support\ Files
│   │       ├── PodLibExample
│   │       │   ├── Info.plist
│   │       │   ├── PodLibExample-dummy.m
│   │       │   ├── PodLibExample-prefix.pch
│   │       │   ├── PodLibExample-umbrella.h
│   │       │   ├── PodLibExample.modulemap
│   │       │   └── PodLibExample.xcconfig
│   │       ├── Pods-PodLibExample_Example
│   │       │   ├── Info.plist
│   │       │   ├── Pods-PodLibExample_Example-acknowledgements.markdown
│   │       │   ├── Pods-PodLibExample_Example-acknowledgements.plist
│   │       │   ├── Pods-PodLibExample_Example-dummy.m
│   │       │   ├── Pods-PodLibExample_Example-frameworks.sh
│   │       │   ├── Pods-PodLibExample_Example-resources.sh
│   │       │   ├── Pods-PodLibExample_Example-umbrella.h
│   │       │   ├── Pods-PodLibExample_Example.debug.xcconfig
│   │       │   ├── Pods-PodLibExample_Example.modulemap
│   │       │   └── Pods-PodLibExample_Example.release.xcconfig
│   │       └── Pods-PodLibExample_Tests
│   │           ├── Info.plist
│   │           ├── Pods-PodLibExample_Tests-acknowledgements.markdown
│   │           ├── Pods-PodLibExample_Tests-acknowledgements.plist
│   │           ├── Pods-PodLibExample_Tests-dummy.m
│   │           ├── Pods-PodLibExample_Tests-frameworks.sh
│   │           ├── Pods-PodLibExample_Tests-resources.sh
│   │           ├── Pods-PodLibExample_Tests-umbrella.h
│   │           ├── Pods-PodLibExample_Tests.debug.xcconfig
│   │           ├── Pods-PodLibExample_Tests.modulemap
│   │           └── Pods-PodLibExample_Tests.release.xcconfig
│   └── Tests
│       ├── Info.plist
│       └── Tests.swift
├── LICENSE
├── PodLibExample
│   ├── Assets
│   └── Classes
│       └── ReplaceMe.swift
├── PodLibExample.podspec
├── README.md
└── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj

PodLibExample.podspecというファイルに、Podの定義を記載していきます。
初期状態は概ね以下の通りです。(コメント行などは削除しています)

Pod::Spec.new do |s|
  s.name             = 'PodLibExample'
  s.version          = '0.1.0'
  s.summary          = 'A short description of PodLibExample.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC
  s.homepage         = 'https://github.com/tenten0213/PodLibExample'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'tenten0213' => 'takehito.0213@gmail.com' }
  s.source           = { :git => 'https://github.com/tenten0213/PodLibExample.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  s.ios.deployment_target = '8.0'
  s.source_files = 'PodLibExample/Classes/**/*'
end

テンプレートから雛形を作成せず、後からpodspecを追加したい場合は以下のコマンドを実行します。

$ pod spec create LIBRARY_NAME

s.sourceで、参照しているリポジトリは、仕事で利用しているプライベートなリポジトリなどにも変更可能です。

  s.source           = { :git => 'https://github.com/tenten0213/PodLibExample.git', :tag => s.version.to_s }

利用方法

デモ用のExampleから呼び出せるようにPodfileに依存関係を追加します。(※)
https://cocoapods.org/ に公開していないので、gitのリポジトリを指定します。

※ 修正前の状態は、Exampleアプリケーションの1階層上のディレクトリにpodspecが置いてあるので、そこを参照するようになっています

@@ -1,7 +1,7 @@
 use_frameworks!

 target 'PodLibExample_Example' do
-  pod 'PodLibExample', :path => '../'
+  pod 'PodLibExample', :git => 'https://github.com/tenten0213/PodLibExample.git'

   target 'PodLibExample_Tests' do
     inherit! :search_paths

pod installすると、作成したPodがインストールされます。

$ cd Example
$ pod install
Analyzing dependencies
Pre-downloading: `PodLibExample` from `https://github.com/tenten0213/PodLibExample.git`
Downloading dependencies
Installing PodLibExample 0.1.0 (was 0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

ブランチを指定したい場合はpodの指定に:branch => 'develop'のように追加すれば良いです。
また、ライブラリを開発中で、ローカルでアプリケーション側の動作確認をしながら進めたいようなシーンでは、
pod 'PodLibExample', :path => '~/develop/PodLibExample/'
のようにローカルのライブラリを参照するようにしておくと効率が良さそうです。

ちなみに、CocoaPodsの公開リポジトリに公開するには、以下の手順で登録すれば公開できます。

参考

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10