LoginSignup
5

More than 5 years have passed since last update.

swaggerのAPIをCocoaPods経由で取り込むまでの手順

Posted at

はじめに

Swaggerで生成したAPIをCocoaPodsを使ってXcodeにframeworkとして取り込むまでのアレコレ。
ほぼ自分用のメモ。

CocoaPodsインストール~PodFile配置まで

参考ページiOSライブラリ管理ツール「CocoaPods」の使用方法
参考ページ【Swift】CocoaPods導入手順

この2ページを参考にすればOK。
注意点はPodFile生成時はpod initコマンドを使うこと

swaggerでSwift3のAPIを生成

重要なのはビルド時のコンフィグ。これでCocoaPodsのアレコレを設定する。
以下は、「社内のスマホ端末管理にTSUTAYAレジ風システムを導入した(予定)」で使用したAPI用に作成したやつ。

swift-config.json
{
  "artifactId":"device-kanri-api",
  "podSource": "{ :git => \"https://github.com/kiuchikeisuke/DeviceKanriSanAPI-for-Swift.git\"}",
  "podAuthors": "k-kiuchi",
  "projectName": "DeviceKanriSanAPI",
  "responseAs": "RxSwift",
  "podHomepage": "https://github.com/kiuchikeisuke/DeviceKanriSanAPI-for-Swift",
  "podSummary": "DeviceKanriAPI",
  "podVersion": "1.0.0"
}

podXXXとなっているものがCocoaPodsで使用するパラメータ。厳密に調べてはいないが、必須パラメータが混じってるので注意(podSummaryの設定消したら取り込み時に怒られたのでこれは必須っぽい) 。

Githubの指定方法は「初心者向けCocoaPodsで最低限必要な用語の解説とTips」の「Gitリポジトリを直接指定する方法」を参考に。
「{}」内がそのまま反映される。
podSourceに指定したgitリポジトリが最終的にXcodeで参照するgitリポジトリになるので注意

これらの設定をしたconfigファイルとswagger-codegen-cli.jarを用意したら以下のコマンドでAPIをビルド

java -jar ./swagger-codegen-cli.jar generate -l swift3 -i ./swagger.yaml -o ./out-swift3 -c ./swift3-config.json

out-swift3ディレクトリにAPI一式が生成される

APIをGitリポジトリに配置

これはGithubでもGithubEnterpirseでもGitlabでも同じ。出力されたout-swift3の中身をGitレポジトリにpushしてあげればよい

PodfileにAPI用の記載を追加

こんな感じ

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'PlayGround' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for PlayGround
  pod 'DeviceKanriSanAPI', :git => 'https://github.com/kiuchikeisuke/DeviceKanriSanAPI-for-Swift.git'

  target 'PlayGroundTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'PlayGroundUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

あとはpod installやらpod updateを叩けばOK
正しく設定できていればPods系の管理下に置かれるようになる。

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
5