LoginSignup
1
2

More than 1 year has passed since last update.

macOSのSwiftアプリをPKG化、DMG化してみよう

Last updated at Posted at 2021-05-02

更新履歴

2021/5/13 参考文献の追加
内容: Mac用Installerの作り方
2021/5/10 DMG化のコマンドを修正
内容: -format UDZOは、指定しなくてもデフォルトでUDZOのため削除

我のPCスペック

macOS Catalina

uname -v
# Darwin Kernel Version 19.6.0....RELEASE_X86_64

PKG化

  1. XcodeでSwiftアプリプロジェクトを作成し、リリースビルドまで行う。(DerivedDataをプロジェクトディレクトリ内に出力できる様に設定しておくと楽)
  2. プロジェクトフォルダ直下にbuildフォルダを作成し、DerivedData/[プロジェクト名]/Build/Products/Release/にあるappファイルをコピー
  3. プロジェクトフォルダ直下で、下記コマンドを実行し、.plistファイルを作成

    pkgbuild --analyze --root ./build [任意の名前 ].plist
    
  4. 生成されたplistを開き、BundlesRelocatable<false/>にする。[1]

  5. プロジェクトフォルダ直下で、下記コマンドを実行し、.pkgファイルを生成。(公開用pkgではないらしい)

    pkgbuild --root ./build --component-plist [3.で生成した.plistファイル ] --identifier [アプリの識別子(ex: jp.co.hogeo) ] --version 0 --install-location /Applications [任意の名前 ].pkg
    
  6. プロジェクトフォルダ直下で、下記コマンドを実行し、Distribution.xmlファイルを生成。

    productbuild --synthesize --package [「5.」のPKGファイル ] Distribution.xml
    
  7. Distribution.xmlを開き、<pkg-ref>...</pkg-ref>の下に下記を追記。

    <title>[任意のタイトル]</title>
    

    (もし、よくある企業ロゴをインストーラーに表記させたい場合は、上記に加え、下記を追記する。pngファイルは、Resourcesフォルダをプロジェクトフォルダ直下に配置し、その中においておく。)

    <background file="hoge-logo.png" alignment="bottomleft" mime-type="image/png"/>
    
  8. プロジェクトフォルダ直下で、下記コマンドを実行し、公開用PKGを生成する。

    productbuild --distribution [「6.」のxmlファイル ] --resources [xmlファイルで追記したpngファイル入れたフォルダ ] --package-path . ./[任意の名前].pkg
    
  9. Developer ID証明書の署名(いる人は)

    productsign  --sign "Developer ID Installer: xxxx Inc."  [「8.」のPKGファイル] [任意の名前].pkg
    

DMG化

  1. プロジェクトフォルダ直下にdmg-resourceフォルダを作成し、その中にREAD_ME.txtや上記で作成した署名PKG(又は未署名PKG)を入れる。
  2. プロジェクトフォルダ直下で、下記コマンドを実行し、DMGファイルを生成する。
hdiutil create -srcfolder [「1.」のフォルダ] -fs HFS+ -volname [dmgをマウントした時にシステム側で出る名前] [任意の名前].dmg

最終フォルダ構成

[test-hello-world_ProjectFolder]
   |
   |-- test-hello-world.xcodeproj
   |-- test-hello-world/
   |-- test-hello-worldTests/
   |-- test-hello-worldUITests/
   |-- DerivedData/
   |-- build/
   |-- Resources/
   |-- dmg-resources/
   |-- TestHelloWorld.plist
   |-- Distribution.xml
   |-- TestHelloWorld.pkg
   |-- Test-Hello-World-Release.pkg
   |-- test_hello_world.dmg

参考文献

[1]pkgbuildでインストーラ(.pkg)作ったけどあれ上手くアプリがインストールされないなーって時にはBundleIsRelocatableがfalseになっているかチェックしましょう

Mac package installer installs the folder “Contents” in /Applications instead of the bundle

Mac用Installerの作り方

macOSのインストーラーを作成するには

hdiutilの使い方

Developer IDで署名したインストーラーパッケージを作る

Mac用Installerの作り方

1
2
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
1
2