初めて触るCocoaPodsのpodインストールでめちゃくちゃハマったので、同じことで悩む人がいなくなりますように。
経緯
他人のコードを写経しようとして、普通にCocoaPodsを使ってpodをインストールしようとしたら下記のようなエラーでハマった。
[!] The following Swift pods cannot yet be integrated as static libraries:
The Swift pod `AWSAppSync` depends upon `AWSCore`, which do not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.
何が悩ましいかって、他人のプロジェクトでは普通にpod installできたのに、自分の方ではエラーが出ていたこと。
ググってもなかなかそれらしい答えが出てこず。
最終的にここに行き当たった。
https://teratail.com/questions/157925
解決方法
Podfileにuse_frameworks!
の記述を追加するだけでOK。
target 'HogeApp' do
use_frameworks! <--こいつを追加するだけ
platform :ios, '10.0'
pod 'Firebase/Core'
...
target 'HogeAppTests' do
inherit! :search_paths
# Pods for testing
end
target 'HogeAppUITests' do
inherit! :search_paths
# Pods for testing
end
end
なんでuse_frameworksが必要なのか
ちょっと気になったので調べた。
当然気になる人はいるようで。
use_frameworks tells CocoaPods that you want to use Frameworks instead of Static Libraries. Since Swift does not support Static Libraries you have to use frameworks.
In another answer, I explained the differences between Static Libraries and Frameworks:
Cocoa Touch Frameworks
They are always open-source and will be built just like your app. (So Xcode will sometimes compile it, when you run your app and always after you cleaned the project.) Frameworks only support iOS 8 and newer, but you can use Swift and Objective-C in the framework.Cocoa Touch Static Libraries
As the name says, they are static. So they are already compiled, when you import them to your project. You can share them with others without showing them your code. Note that Static Libraries currently don't support Swift. You will have to use Objective-C within the library. The app itself can still be written in Swift.
抄訳:
use_frameworks
は、CocoaPodsに対して、Static Librariesの代わりにFrameworksを利用したいということを伝えます。
SwiftはStatic Librariesをサポートしていないため、Frameworksを使用する必要があります。
(※訳注: このQAは2016年12月時点のものであり、その後のXCode9系以降ではサポートされています)
Static LibrariesとFrameworksの違いは下記の通り。
Cocoa Touch Frameworks
すべてオープンソースであり、あなたが作成するアプリと同様に作られたものです。そのためXCodeは、あなたがアプリをRunする際、及びプロジェクトをcleanした後に毎回Frameworksをコンパイルします。
FrameworksはiOS8以降のみをサポートしていますが、内部的にはSwiftとObjective-Cのいずれも利用することができます。
Cocoa Touch Static Libraries
名前の通り、これらは「静的(= Static)」です。つまりこれらは、あなたがプロジェクトにインポートした際には既にコンパイル済みです。そのため、自分のコードを他人に見せることなく配布することができます。気をつけるべきなのは、Static Librariesは現状Swiftをサポートしていない、ということです(※訳注: このQAは2016年12月時点の(以下略))。Static Libraries内ではObjective-Cを使用する必要があります。アプリそのものについては、Swiftで問題ありません。
エラー内容を再度見てみる
The following Swift pods cannot yet be integrated as static libraries:
この部分については、「下記のpodsはまだStatic Librariesとして使えないよ」となっているので、上記の知識さえあれば、「あー、じゃあFrameworks使うように指示してみるか」ってなるのかな。
もうちょっと詳しく
知るためには、
http://blog.cocoapods.org/CocoaPods-1.5.0/
これとか読んでみるとわかるのかもしれない。
ただ、また別のバックグラウンド知識が必要になりそうなので、この記事での解説は控えますm(_ _)m
優しい詳しい方いたら、分かりやすい説明コメントで頂けると嬉しいです。