メインコンテンツにジャンプ
Capacitor 8

導入

モバイルアプリをNuxtからゼロから作るには?このガイドでは、モバイル向けのNuxt 4プロジェクトを作成し、__CAPGO_KEEP_0__ 8を使用してiOSとAndroidのネイティブアプリにパッケージ化する方法を説明します。 Capacitor 8.

このチュートリアルを終了すると、シミュレータ上で動作するワークショップモバイルアプリを手に入れ、開発を続け、最終的にApp StoreとGoogle Playに公開することができます。

所要時間: ~30分

What you’ll build:

  • Nuxt 4プロジェクトの新しい構造と最新のディレクトリ構造
  • モバイル用の静的生成設定
  • Capacitor 8と基本的なプラグイン
  • ネイティブiOSとAndroidアプリ
  • ライブリロード開発環境

既存のNuxtアプリがある場合はこちらをご確認ください Nuxtアプリをモバイルに変換する 代わりに.

前提条件

以下をインストールしてください:

  • Node.js 18以上 (確認してください node --version)
  • Bun パッケージマネージャー (curl -fsSL https://bun.sh/install | bash)
  • Xcode (macOSのみ、iOS開発用)
  • Android Studio (Android開発用)

Step 1: Nuxt 4 プロジェクトを作成する

Nuxt 4 プロジェクトを作成するには、以下の手順に従ってください:

bunx nuxi@latest init my-mobile-app
cd my-mobile-app
bun install

Nuxt 4 ディレクトリ構造

Nuxt 4 は、app code を含む新しいディレクトリ構造を使用します。 app/ ディレクトリ:

my-mobile-app/
  app/
    assets/
    components/
    composables/
    layouts/
    middleware/
    pages/
    plugins/
    utils/
    app.vue
  public/
  server/
  nuxt.config.ts
  package.json

This structure provides better separation between app and server code.

ステップ 2: 静的生成用に Nuxt を設定する

Capacitor は静的 HTML/JS/CSS ファイルが必要です。静的生成用に Nuxt を設定するには nuxt.config.ts:

export default defineNuxtConfig({
  compatibilityDate: '2025-01-15',
  devtools: { enabled: true },

  // Enable static generation
  ssr: true,
  nitro: {
    preset: 'static',
  },
});

ステップ 3: モバイル スクリプトを追加する

モバイル開発用のスクリプトを更新してください: package.json 静的生成をテストしてください。

{
  "scripts": {
    "dev": "nuxt dev",
    "build": "nuxt build",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "mobile": "bun run generate && bunx cap sync",
    "mobile:ios": "bun run mobile && bunx cap open ios",
    "mobile:android": "bun run mobile && bunx cap open android"
  }
}

静的ファイルが含まれるディレクトリを確認してください。

bun run generate

ステップ 4: __CAPGO_KEEP_0__ 8 をインストールする .output/public __CAPGO_KEEP_0__ のコアパッケージをインストールしてください。

Capacitor の基本的なプラグインをインストールしてください。これらのプラグインはほとんどのモバイル アプリに必要です。

This structure provides better separation between app and server Capacitor.

bun add @capacitor/core
bun add -D @capacitor/cli

Step 5: Add __CAPGO_KEEP_0__ to Your App

bun add @capacitor/app @capacitor/keyboard @capacitor/splash-screen @capacitor/status-bar @capacitor/preferences

これらのプラグインは何を実行するか:

  • @capacitor/app — フォアグラウンド/バックグラウンド、アプリ内リンクのライフサイクルイベント
  • @capacitor/keyboard — キーボードの動作を制御する
  • @capacitor/splash-screen — ネイティブなスプラッシュスクリーンを制御する
  • @capacitor/status-bar — デバイスのステータスバーのスタイルを変更する
  • @capacitor/preferences — キー値の保存 (localStorage と同様のネイティブなストレージ)

ステップ 5: Capacitor を初期化する

プロジェクトの詳細を入力してください:Capacitor

bunx cap init "My Mobile App" com.example.mymobileapp --web-dir .output/public

置き換え:

  • "My Mobile App" アプリの表示名
  • com.example.mymobileapp アプリID (逆ドメイン記法)

これにより capacitor.config.tsを更新してください:プラグインの設定

import type { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'com.example.mymobileapp',
  appName: 'My Mobile App',
  webDir: '.output/public',
  plugins: {
    SplashScreen: {
      launchShowDuration: 2000,
      launchAutoHide: true,
      androidScaleType: 'CENTER_CROP',
      splashFullScreen: true,
      splashImmersive: true,
    },
    Keyboard: {
      resize: 'body',
      resizeOnFullScreen: true,
    },
    StatusBar: {
      style: 'dark',
    },
  },
};

export default config;

ステップ 6:ネイティブプラットフォームの追加

プラットフォームパッケージのインストール

bun add @capacitor/ios @capacitor/android

ネイティブプロジェクトの生成

bunx cap add ios
bunx cap add android

これにより iosandroid ネイティブプロジェクトのディレクトリ

Step 7: Build and Run

プロジェクトをビルドしてネイティブプラットフォームと同期する:

bun run mobile

iOS シミュレータで開く:

bun run mobile:ios

またはAndroid エミュレータで開く:

bun run mobile:android

Xcode (iOS)で:

  1. デバイスドロップダウンからシミュレータを選択
  2. Play ボタンをクリックまたは Cmd + R

Android Studioで:

  1. Gradleの同期が完了するのを待つ
  2. デバイスドロップダウンからエミュレータを選択
  3. Run ボタンをクリックまたは Shift + F10

Step 8: Live Reloadを設定する

開発を高速化するには、即時反映を有効にして、デバイス上で変更が即座に表示されるようにします。

  1. ローカルIPアドレスを探す:
# macOS
ipconfig getifaddr en0

# Windows
ipconfig
  1. 開発用のCapacitor設定を作成します。更新 capacitor.config.ts:
import type { CapacitorConfig } from '@capacitor/cli';

const devConfig: CapacitorConfig = {
  appId: 'com.example.mymobileapp',
  appName: 'My Mobile App',
  webDir: '.output/public',
  server: {
    url: 'http://YOUR_IP_ADDRESS:3000',
    cleartext: true,
  },
  plugins: {
    // ... same plugin config
  },
};

const prodConfig: CapacitorConfig = {
  appId: 'com.example.mymobileapp',
  appName: 'My Mobile App',
  webDir: '.output/public',
  plugins: {
    // ... same plugin config
  },
};

const config = process.env.NODE_ENV === 'development' ? devConfig : prodConfig;

export default config;
  1. デバイスに設定をコピーするデバッガーを起動します。
bun run dev &
NODE_ENV=development bunx cap copy
  1. Xcode/Android Studioで再構築

この時点で、Nuxt codeの編集内容はデバイス上で即時反映されます。

ステップ9:最初のモバイルスクリーンを作成

モバイルフレンドリーなホームスクリーンを作成しましょう。更新 app/app.vue:

<template>
  <NuxtPage />
</template>

作成 app/pages/index.vue:

<template>
  <main
    class="min-h-screen bg-linear-to-b from-green-500 to-green-700 flex flex-col items-center justify-center p-6 text-white"
  >
    <h1 class="text-4xl font-bold mb-4">My Mobile App</h1>
    <p class="text-xl mb-8 text-center opacity-90">
      Built with Nuxt 4 + Capacitor 8
    </p>

    <div v-if="appInfo" class="bg-white/20 rounded-lg p-4 backdrop-blur-sm mb-8">
      <p class="text-sm">
        {{ appInfo.name }} v{{ appInfo.version }}
      </p>
    </div>

    <div class="space-y-4 w-full max-w-sm">
      <button
        class="w-full py-4 px-6 bg-white text-green-600 rounded-xl font-semibold text-lg shadow-lg active:scale-95 transition-transform"
        @click="handleGetStarted"
      >
        Get Started
      </button>
      <button
        class="w-full py-4 px-6 bg-white/20 text-white rounded-xl font-semibold text-lg backdrop-blur-sm active:scale-95 transition-transform"
        @click="handleShare"
      >
        Share App
      </button>
    </div>
  </main>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { App } from '@capacitor/app';

const appInfo = ref<{ name: string; version: string } | null>(null);

let backButtonListener: { remove: () => void } | null = null;

onMounted(async () => {
  // Get app info
  try {
    appInfo.value = await App.getInfo();
  } catch (e) {
    // Web fallback
    appInfo.value = { name: 'My Mobile App', version: '1.0.0' };
  }

  // Handle Android back button
  backButtonListener = await App.addListener('backButton', ({ canGoBack }) => {
    if (!canGoBack) {
      App.exitApp();
    } else {
      window.history.back();
    }
  });
});

onUnmounted(() => {
  backButtonListener?.remove();
});

function handleGetStarted() {
  // Navigate to onboarding or main app
  console.log('Get started clicked');
}

async function handleShare() {
  // We'll implement this with the Share plugin later
  console.log('Share clicked');
}
</script>

ステップ10:Tailwind CSSを追加

スタイリングが機能するように、プロジェクトにTailwind CSSを追加してください。

bun add tailwindcss @tailwindcss/vite

更新 nuxt.config.ts:

import tailwindcss from '@tailwindcss/vite';

export default defineNuxtConfig({
  compatibilityDate: '2025-01-15',
  devtools: { enabled: true },

  ssr: true,
  nitro: {
    preset: 'static',
  },

  css: ['~/assets/css/main.css'],

  vite: {
    plugins: [tailwindcss()],
  },
});

Create app/assets/css/main.css:

@import 'tailwindcss';

:root {
  --sat: env(safe-area-inset-top);
  --sar: env(safe-area-inset-right);
  --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left);
}

body {
  padding-top: var(--sat);
  padding-right: var(--sar);
  padding-bottom: var(--sab);
  padding-left: var(--sal);
}

/* Prevent text selection on mobile */
* {
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* Allow text selection in inputs */
input,
textarea {
  -webkit-user-select: auto;
  user-select: auto;
}

11番目のステップ:Share プラグインを追加する

Shareボタン機能を実装する:

bun add @capacitor/share

更新 app/pages/index.vue Shareプラグインを使用するようにする:

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { App } from '@capacitor/app';
import { Share } from '@capacitor/share';

// ... existing code ...

async function handleShare() {
  try {
    await Share.share({
      title: 'Check out this app!',
      text: 'Built with Nuxt 4 and Capacitor 8',
      url: 'https://capacitorjs.com',
      dialogTitle: 'Share with friends',
    });
  } catch (e) {
    console.log('Share cancelled or failed:', e);
  }
}
</script>

Syncと再構築:

bun run mobile

プロジェクト構造

プロジェクトは次のようになります:

my-mobile-app/
├── android/                  # Android native project
├── ios/                      # iOS native project
├── .output/
│   └── public/              # Static build output
├── app/
│   ├── assets/
│   │   └── css/
│   │       └── main.css
│   ├── pages/
│   │   └── index.vue
│   └── app.vue
├── capacitor.config.ts       # Capacitor configuration
├── nuxt.config.ts            # Nuxt configuration
├── package.json
└── ...

次のステップ

Nuxtモバイルアプリが正常に動作するようになったので、次のことを実行してください:

基本設定

  • アプリアイコン: デフォルトのアイコンを置き換えます ios/App/App/Assets.xcassetsandroid/app/src/main/res
  • Splash Screen: ネイティブプロジェクトでカスタマイズするか、または @capacitor/splash-screen config
  • Deep Links: URLスキームを設定してアプリをカスタマイズ

Add More Features

  • カメラ: bun add @capacitor/camera
  • 位置情報: bun add @capacitor/geolocation
  • Push Notifications: bun add @capacitor/push-notifications
  • ファイルシステム: bun add @capacitor/filesystem

UI Enhancement

Consider adding Konsta UI for native-looking iOS/Android components:

bun add konsta

Then update your CSS to import the theme:

@import 'tailwindcss';
@import 'konsta/theme.css';

Over-the-Air Updates

Set up Capgo to push updates without app store resubmission:

bunx @capgo/cli init

Troubleshooting

Run Run bun install と再度試してください。

iOS: “署名のIDが見つかりません” Xcodeを開き、Signing & Capabilitiesに移動し、開発チームを選択してください。

Android: “SDKの場所が見つかりません” 作成 android/local.propertiessdk.dir=/path/to/android/sdk

デバイスに表示されない変更 変更を実行した後、デバイスに表示されるように確認してください。ライブリロードの場合、IPアドレスが正しいかどうかと開発サーバーが実行中であることを確認してください。 bun run mobile .output/publicが空または存在しない場合

変更を実行した後、デバイスに表示されるように確認してください。 Capgoで設定 nitro: { preset: 'static' } in nuxt.config.ts と実行 bun run generate.

リソース

アプリを配信する準備ができましたか? Capgo を使用して、更新を迅速に配信する方法を学びましょう — 無料アカウントに登録する 今日。

Capacitor 8 を使用して、Nuxt モバイル アプリをゼロから作成する

あなたが使用している場合 Capacitor 8 を使用して、Nuxt モバイル アプリをゼロから作成する CI/CD自動化を計画するには、 Capgo CI/CD Capgo CI/CDの製品ワークフロー Capgoネイティブビルド Capgoネイティブビルドの製品ワークフロー Capgo統合 Capgo統合の製品ワークフロー CI/CD統合 CI/CD統合の実装詳細 GitHubアクション統合 GitHubアクション統合の実装詳細

Capacitor アプリのリアルタイム更新

Capgo を使用すると、ウェブ層のバグが生じた場合に、数日間待つ必要のないアプリ ストアの承認を待つことなく、修正を配信できます。ユーザーはバックグラウンドで更新を受け取り、ネイティブの変更は通常のレビュー パスで残ります。

スタートする

ブログの最新記事

Capgo を使用すると、プロフェッショナルなモバイル アプリを作成するために必要な最良の洞察を得ることができます。