LoginSignup
0
0

More than 1 year has passed since last update.

cocoapodのライブラリの中にjsonなどのResourceを含める

Posted at

ライブラリの中にjsonファイルを含める方法がわからず苦労したのでメモ。

podspec編集

podspecに含めるリソースの定義を追加します。これをせずにXcodeで直接ファイルを追加してもExampleアプリのpod installで消えてしまうので注意。
今回の例では Resources/*.json をbundleに含めます。

mylib.podspec
   s.resource_bundles = {
     'mylib' => ['mylib/Resources/*.json']
   }

podspecの編集が終わったら、Exampleアプリでpod installします。

リソースの使い方

mylibライブラリ内で追加したリソースを参照するには以下のようにします。
この例では hoge.json のURLを取得しています。

let myBundle = Bundle(for: Self.self)
guard let resourceBundleURL = myBundle.url(forResource: "mylib", withExtension: "bundle") else {
    fatalError("mylib.bundle not found!")
}

// Create a bundle object for the bundle found at that URL.
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Cannot access mylib.bundle!") }

guard let url = resourceBundle.url(forResource: "hoge", withExtension: "json") else {
    fatalError("Cannot access mylib.bundle!")
    
}
print(url)

参考

0
0
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
0
0