6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElixirDesktop iOS Exampleを参考にiOSアプリを1から作ってみる

Last updated at Posted at 2023-09-27

はじめに

本記事は以下を参考にXCodeでプロジェクトを作るところから初めて
実際に起動するまでの手順の備忘録です

フォルダ構成

LiveViewNativeがPhoenixのProjectの配下にnativeディレクトリを作成してそこに各OSのPJおいてたのでそれに合わせる
こんな感じになる

basic/native/basic

iOS PJを作る

xcode new project phoenix pj/native/ に作成

  • ZIPFoundation
  • liberlang
  • native-lib.cpp
  • native-lib.hpp

をXcodeの画面同士でサンプルからコピー

Bridge-Headerを作るかを訊かれるのでOK

ヘッダーに以下を追加

native/xx/xx-Bridging-Header
#import "native-lib.hpp"

Carthage まわり

ライブラリのインストールだったりビルドをしてくれます

Cartfileのファイルを作成

native/xx/Cartfile
github "weichsel/ZIPFoundation" ~> 0.9

以下を実行

carthage update --use-xcframeworks

XcodeでliberlangとZIPFoundation を embed & sign にする

スクリーンショット 2023-09-27 13.49.00.png

アプリの中身のコードを作成

TodoApp.swiftの中身をコピー

native/basic/BasicApp.swift
import SwiftUI

@main
struct BasicApp: App {
    @Environment(\.scenePhase) var scenePhase
    
    var content = ContentView()
    var body: some Scene {
        WindowGroup {
            self.content
        }
        .onChange(of: scenePhase) { phase in
            switch phase {
            case .background:
                print(".background")
            case .active:
                print(".active")
                if let bridge = Bridge.instance {
                    bridge.reinit()
                }
            default: break
            }
        }
    }
}

以下はXCode同士でファイルコピー

  • Bridge.swift
  • ContentView.swift
  • WebView.swift
  • WebViewController.swift

ビルド設定

run_mixをVSCode経由でコピー(実行権限とかがめんどくさいので)
パスがいつものと違うのでそこを注意

native/basic/run_mix
#!/bin/bash
set -e

# Setting up the PATH environment
[ -s /opt/homebrew/bin/brew ] && eval $(/opt/homebrew/bin/brew shellenv)
[ -s /usr/local/bin/brew ] && eval $(/usr/local/bin/brew shellenv)

# This loads nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  

# This loads asdf
if [ -s "$HOMEBREW_PREFIX/opt/asdf/libexec/asdf.sh" ]; then 
  \. "$HOMEBREW_PREFIX/opt/asdf/libexec/asdf.sh" 
elif [ -s "$HOME/.asdf/asdf.sh" ]; then
  \. "$HOME/.asdf/asdf.sh"
fi

BASE=`pwd`
export MIX_ENV=prod
export MIX_TARGET=ios

mix local.hex --force --if-missing
mix local.rebar --force --if-missing

# Phoenix PJまでのパスに変更 
cd ../../

if [ ! -d "deps/desktop" ]; then
  mix deps.get
fi

if [ ! -d "assets/node_modules" ]; then
  cd assets && npm i && cd ..
fi

# xcodeで作成したプロジェクト名に変更
if [ -f "$BASE/basic/app.zip" ]; then
  rm "$BASE/basic/app.zip"
fi

mix assets.deploy && \
  mix release --overwrite && \
  cd _build/ios_prod/rel/basic && \ # phoenix PJ名
  zip -9r "$BASE/basic/app.zip" lib/ releases/ --exclude "*.so"
# ↑ここも忘れずに

Build PhaseにRun Scriptを追加

スクリーンショット 2023-09-27 14.08.16.png

以下のように設定

#!/bin/bash
set -e

./run_mix
  • run_mixを実行
  • output fileに app.zipを追加

スクリーンショット 2023-09-27 15.18.29.png

build時にrun_mixを実行できるようにProjectの設定で User Script SandboxをNoにする

スクリーンショット 2023-09-27 16.02.19.png

ビルド実行!

動いた!

スクリーンショット 2023-09-27 16.20.04.png

最後に

これでアプリ名の変更とかめんどくさい処理をしなくてもアプリ作れるので提出が楽になるといいなぁ

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?