🕹️

[React Native] Expo iOS アプリから Game Center を呼び出してみる

に公開
2026/01/05
1件

Expo 54 であれこれ試行錯誤してみた結果のメモです。

標準機能ではサポートしていないっぽい

https://docs.expo.dev/build-reference/ios-capabilities/

ダメ元で追加してみますが

app.json
  "ios": {
    〜,
    "entitlements": {
      "com.apple.developer.game-center": true
    }
  },
$ eas build --profile development --platform ios
🍏 iOS build failed:
The "Run fastlane" step failed because of an error in the Xcode build process. We automatically detected following errors in your Xcode build logs:
- Provisioning profile "*[expo] xxxx.yyyy.zzzzz AdHoc 0000000000" doesn't support the Game Center capability.  (in target 'test' from project 'test')
- Provisioning profile "*[expo] xxxx.yyyy.zzzzz AdHoc 0000000000" doesn't include the com.apple.developer.game-center entitlement. (in target 'test' from project 'test')
Refer to "Xcode Logs" below for additional, more detailed logs.

やっぱりダメでした。

Apple Developer サイトから直接 Game Center を有効にする

Expo 側からは有効にできないようなので Apple Developer の Identifiers から直接有効にします。

Apple Developer にログイン -> Identifiers -> 対象のアプリの Identifier を選択

Game Center にチェックを入れて Save します。

これで準備OKです。

Expo 側で設定できた方が全てコード上で管理できて便利ではありますが、このくらいの手間であれば問題ないですね。

Expo モジュールを作成

Game Center 関連の実装を追加するため土台のモジュールを追加します。

$ npx create-expo-module@latest --local game-kit-ios

作成すると WebView のサンプル実装がありますが削除します。Android 関連のコードもごっそり削除。

最終的にこのような構成にします。

ファイル階層
modules
  └ game-kit-ios
     └ ios
     │  ├ GameKitIos.podspec ・・・ 変更なし
     │  ├ GameKitIosModule.swift
     │
     └ src
     │  ├ GameKitIos.types.ts
     │  ├ GameKitIosModule.ts
     │
     ├ expo-module.config.json
     └ index.ts

expo-module.config.json も編集して iOS のみ対応に変更します。

expo-module.config.json
-  "platforms": ["apple", "android", "web"],
+  "platforms": ["apple"],
〜
-  },
+  }
-  "android": {
-    "modules": ["expo.modules.gamekit.GameKitIosModule"]
-  }

モジュールにネイティブコードを追加

GameKitIosModule にログイン・スコアの登録・表示処理の追加を行います。

追加するネイティブコードは Xcode での追加・編集は必須ではありませんがコードを編集するたびにアプリの再ビルドが必要になるため動作確認に時間がかかります。
そのためモジュール開発時は prebuild したプロジェクトを Xcode で実行しながら進めた方が簡単そうです。
(モジュールは workspace を開いて、Pods > Development Pods > MODULE_NAME から編集可能)

GameKitIosModule.swift
import ExpoModulesCore
import React
import GameKit

public class GameKitIosModule: Module {
  let gkGcDelegate = GKGCDelegate()
  public func definition() -> ModuleDefinition {
    Name("GameKitIosModule")

    Function("isAuthenticated") {
      GKLocalPlayer.local.isAuthenticated
    }

    // ダッシュボード表示
    Function("showLeaderboard") { (leaderboardId: String) in
      Task { @MainActor in
        guard let presentedViewController = RCTPresentedViewController() else {
          return
        }
        let gkGcViewController = GKGameCenterViewController(leaderboardID: leaderboardId, playerScope: .global, timeScope: .allTime)
        gkGcViewController.gameCenterDelegate = gkGcDelegate
        gkGcViewController.modalPresentationStyle = .fullScreen
        presentedViewController.present(gkGcViewController, animated: false)
      }
    }

    // ログイン処理
    AsyncFunction("authenticate") { (promise: Promise) in
      GKLocalPlayer.local.authenticateHandler = { vc, error in
        if let error {
          promise.reject(error)
        } else {
          promise.resolve()
        }
      }
    }

    // スコア送信
    AsyncFunction("submitScore") { (leaderboardId: String, score: Int, promise: Promise) in
      if !GKLocalPlayer.local.isAuthenticated {
        promise.resolve()
        return
      }
      GKLeaderboard.submitScore(score,
                                context: 0,
                                player: GKLocalPlayer.local,
                                leaderboardIDs: [leaderboardId]) { error in
        if let error {
          promise.reject(error)
        } else {
          promise.resolve()
        }
      }
    }

    // スコア取得
    AsyncFunction("loadScore") { (leaderboardId: String, promise: Promise) in
      if !GKLocalPlayer.local.isAuthenticated {
        promise.resolve(nil)
        return
      }
      GKLeaderboard.loadLeaderboards(IDs: [leaderboardId]) { scores, error in
          if let error {
            promise.reject(error)
            return
          }
          if let scores, let score = scores.first {
              score.loadEntries(for: [GKLocalPlayer.local], timeScope: .allTime) { entry, _, error in
                  if let error {
                    promise.reject(error)
                    return
                  }
                  if let entry {
                      let json = """
                          {
                            "rank": \(entry.rank),
                            "score": \(entry.score),
                            "formattedScore": "\(entry.formattedScore)",
                            "date": "\(entry.date)"
                          }
                          """
                      promise.resolve(json)
                  } else {
                    promise.resolve(nil)
                  }
              }
          } else {
            promise.resolve(nil)
          }
      }
    }
  }
}

class GKGCDelegate : NSObject, GKGameCenterControllerDelegate {
    func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
        gameCenterViewController.dismiss(animated: true, completion: nil)
    }
}

モジュール編集・アプリビルド

モジュール作成時に自動生成されたコードを以下のように変更します。

GameKitIos.types.ts
export type GameKitModuleEvents = {
};
GameKitIosModule.ts
import { NativeModule, requireNativeModule } from 'expo';

import { GameKitIosModuleEvents } from './GameKitIos.types';

declare class GameKitIosModule extends NativeModule<GameKitIosModuleEvents> {
  isAuthenticated(): boolean;
  showLeaderboard(leaderboardId: string): void;
  authenticate(): Promise<void>;
  submitScore(leaderboardId: string, score: number): Promise<void>;
  loadScore(leaderboardId: string): Promise<string>;
}

// This call loads the native module object from the JSI.
export default requireNativeModule<GameKitIosModule>('GameKitIosModule');
index.ts
export { default } from './src/GameKitIosModule';
export * from  './src/GameKitIos.types';

モジュールの準備はこれで完了なので開発用のアプリをビルドします。

$ eas build --profile development --platform ios

ビルドが完了すればモジュールの準備は完了です。

リーダーボードを作成

App Store Connect にアプリを登録して、Game Center の欄からリーダーボードを作成します。

今回は「score」という名称で作成しました。審査準備完了と表示されますが審査を通さなくてもサンプルコードからアクセスすることができました。

React Native 側からの呼び出し

App.js
import { useState } from 'react';
import { Alert, Button, SafeAreaView, StyleSheet, View } from 'react-native';
import GameKit from './modules/game-kit-ios';

export default function App() {
  const [modal, setModal] = useState(false);

  return (
    <View style={styles.container}>
      <Button title='login' onPress={async () => {
        GameKit.authenticate()
      }} />

      <Button title='save score' onPress={async () => {
        GameKit.submitScore('score', 567)
      }} />

      <Button title='load score' onPress={async () => {
        const score = await GameKit.loadScore('score')
        Alert.alert('SCORE', score)
      }} />

      <Button title='ranking' onPress={() => 
        GameKit.showLeaderboard('score')
      } />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

簡単にですが Game Center の機能を呼び出すことができました🎉

Discussion

tadaedotadaedo