react-native-game-services
v0.1.47
Published
RN lib
Readme
react-native-game-services - BETA
A React Native library that provides a unified API for integrating Game Center (iOS) and Google Play Games (Android) services. This library enables authentication, leaderboards, and score submission across both platforms.
⚠️ Beta Warning: This library is currently in beta. We recommend using it for testing and development purposes only—production use is not advised at this time.
Features
- 🔐 Authentication - Sign in with Game Center (iOS) or Google Play Games (Android)
- 🏆 Leaderboards - Display and manage leaderboards
- 📊 Score Submission - Submit scores to leaderboards
- ⚡ Expo Support - Works with Expo managed workflow
- 🚀 Turbo Modules - Built with React Native Turbo Modules for better performance
- 🎖️ Achievements - To-Do
Installation
npm install react-native-game-services
# or
yarn add react-native-game-servicesiOS Setup
The library automatically configures Game Center entitlements for you. No manual Xcode configuration is needed.
Configure Game Center in App Store Connect:
- Go to App Store Connect
- Select your app (or create one)
- Go to Growth & Marketing > Game Center
- Enable Game Center to your app
Configure plugin (Expo only):
Add the plugin to your
app.json(no configuration needed for iOS):{ "expo": { "plugins": ["react-native-game-services"] } }After adding the plugin, rebuild your app:
# For React Native projects # Rebuild the native iOS app # For Expo projects npx expo prebuild --clean
Android Setup
Enable Google Play Games Services:
- Go to Google Play Console
- Select your app (or create one)
- Go to Grow users > Play Game Services > Setup and management > Configuration
- Get your App ID (called as Project ID) from the Play Game Services dashboard
Configure the Android App ID:
The library automatically configures the Android App ID. You have two options depending on your project type:
Option A: React Native Projects
Create an
app.jsonfile in your project root (if it doesn't exist) and add:{ "react-native-game-services": { "android_app_id": "YOUR_GOOGLE_PLAY_GAMES_APP_ID" } }Option B: Expo Projects
Add the plugin to your
app.json:{ "expo": { "plugins": [ [ "react-native-game-services", { "androidAppId": "YOUR_GOOGLE_PLAY_GAMES_APP_ID" } ] ] } }After configuring, rebuild your app:
# For React Native projects # Rebuild the native Android app # For Expo projects npx expo prebuild --clean
Usage
Basic Example
import React, { useEffect, useState } from 'react';
import { View, Button, Text, Platform } from 'react-native';
import {
initialize,
isAuthenticated,
signIn,
showLeaderboard,
submitScore,
} from 'react-native-game-services';
export default function App() {
const [signedIn, setSignedIn] = useState(false);
useEffect(() => {
// Initialize Game Services (required for Android)
initialize();
// Check authentication status
checkAuth();
async function checkAuth() {
const auth = await isAuthenticated();
if (!auth) {
try {
await signIn();
setSignedIn(true);
} catch (error) {
console.error('Sign in failed:', error);
}
} else {
setSignedIn(true);
}
}
}, []);
const leaderboardId = Platform.select({
ios: 'your-ios-leaderboard-id',
android: 'your-android-leaderboard-id',
default: 'default-leaderboard-id',
});
const handleSubmitScore = async () => {
try {
await submitScore(leaderboardId!, 1500);
console.log('Score submitted successfully!');
} catch (error) {
console.error('Failed to submit score:', error);
}
};
const handleShowLeaderboard = async () => {
try {
await showLeaderboard(leaderboardId!);
} catch (error) {
console.error('Failed to show leaderboard:', error);
}
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Game Services Example</Text>
{signedIn ? (
<>
<Button title="Submit Score" onPress={handleSubmitScore} />
<Button title="Show Leaderboard" onPress={handleShowLeaderboard} />
</>
) : (
<Text>Signing in...</Text>
)}
</View>
);
}API Reference
initialize()
Initializes the Game Services SDK. Required for Android, optional for iOS.
import { initialize } from 'react-native-game-services';
initialize();isAuthenticated(): Promise<boolean>
Checks if the user is currently authenticated with Game Center (iOS) or Google Play Games (Android).
const authenticated = await isAuthenticated();
if (authenticated) {
console.log('User is authenticated');
}Returns: Promise<boolean> - true if authenticated, false otherwise
signIn(): Promise<void>
Initiates the sign-in flow for the user. On iOS, this will show the Game Center authentication UI if not already signed in. On Android, this will show the Google Play Games sign-in UI.
try {
await signIn();
console.log('Signed in successfully');
} catch (error) {
console.error('Sign in failed:', error);
}Returns: Promise<void>
Throws: Error if authentication fails
showLeaderboard(leaderboardId: string, timeSpan?: number): Promise<void>
Displays the native leaderboard UI for the specified leaderboard.
await showLeaderboard('your-leaderboard-id', 0);Parameters:
leaderboardId(string, required) - The ID of the leaderboard to displaytimeSpan(number, optional) - Time span filter:0- All time (default)1- Today2- Week3- Today and Week
Returns: Promise<void>
Throws: Error if the leaderboard cannot be displayed
submitScore(leaderboardId: string, score: number): Promise<void>
Submits a score to the specified leaderboard.
await submitScore('your-leaderboard-id', 1000);Parameters:
leaderboardId(string, required) - The ID of the leaderboard to submit toscore(number, required) - The score value to submit
Returns: Promise<void>
Throws: Error if the score cannot be submitted
Requirements
- React Native >= 0.81.0
- iOS >= 12.0
- Android API Level >= 21
- For Expo: Expo SDK >= 54.0.0
Contributing
Contributions are welcome! Please read our Contributing Guide and Code of Conduct before submitting a pull request.
License
MIT
Made with ❤️ using create-react-native-library
