### Install Expo Wallet Module
Source: https://github.com/giulio987/expo-wallet/blob/master/README.md
Instructions for installing the @giulio987/expo-wallet module using npm or yarn. This is the initial step required before using the module's functionalities.
```bash
npm install @giulio987/expo-wallet
```
```bash
yarn add @giulio987/expo-wallet
```
--------------------------------
### React Native Wallet Integration with Expo Wallet
Source: https://context7.com/giulio987/expo-wallet/llms.txt
This snippet shows a complete example of integrating Expo Wallet into a React Native application. It includes checking wallet availability, fetching pass data (PKPASS for iOS, JWT for Android) from a mock API, and adding the pass to the wallet. It also handles various error codes specific to the wallet integration process and displays platform-specific messages.
```javascript
import React, { useState, useEffect } from 'react';
import {
View,
Text,
Button,
Alert,
Platform,
ActivityIndicator,
StyleSheet
} from 'react-native';
import ExpoWallet from '@giulio987/expo-wallet';
const TicketScreen = ({ ticketId }) => {
const [walletAvailable, setWalletAvailable] = useState(false);
const [loading, setLoading] = useState(true);
const [addingPass, setAddingPass] = useState(false);
useEffect(() => {
checkWallet();
}, []);
const checkWallet = async () => {
try {
const available = await ExpoWallet.isAvailable();
setWalletAvailable(available);
} catch (error) {
console.error('Wallet check failed:', error);
setWalletAvailable(false);
} finally {
setLoading(false);
}
};
const fetchPassData = async (ticketId) => {
// Fetch pass data from your backend
const response = await fetch(`https://api.example.com/tickets/${ticketId}/pass`, {
headers: {
'Platform': Platform.OS,
'Authorization': 'Bearer YOUR_TOKEN'
}
});
const data = await response.json();
// Backend should return base64 .pkpass for iOS or JWT token for Android
return Platform.OS === 'ios' ? data.pkpass : data.token;
};
const handleAddToWallet = async () => {
if (!walletAvailable) {
Alert.alert('Not Available', 'Wallet is not available on this device');
return;
}
setAddingPass(true);
try {
// Fetch platform-specific pass data
const passData = await fetchPassData(ticketId);
// Add pass to wallet
const result = await ExpoWallet.addPass(passData);
if (result === true) {
Alert.alert(
'Success',
'Your ticket has been added to your wallet!',
[{ text: 'OK' }]
);
}
} catch (error) {
let errorMessage = 'Failed to add ticket to wallet';
switch (error.code) {
case 'E_PASS_LIBRARY_CANNOT_ADD':
errorMessage = 'This ticket cannot be added. It may already be in your wallet.';
break;
case 'E_PASS_LIBRARY_INVALID_DATA':
errorMessage = 'Invalid ticket data. Please contact support.';
break;
case 'E_PASS_LIBRARY_UNAVAILABLE':
errorMessage = 'Wallet is not available on this device.';
break;
case 'GOOGLE_WALLET_API_NOT_AVAILABLE':
errorMessage = 'Google Wallet is not available. Please install it from Play Store.';
break;
}
Alert.alert('Error', errorMessage);
console.error('Add to wallet error:', error);
} finally {
setAddingPass(false);
}
};
if (loading) {
return (
Checking wallet availability...
);
}
return (
Your TicketID: {ticketId}
{walletAvailable ? (
) : (
Wallet is not available on this device
)}
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#fff'
},
center: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 10
},
ticketId: {
fontSize: 16,
color: '#666',
marginBottom: 20
},
unavailable: {
color: '#999',
fontStyle: 'italic'
}
});
export default TicketScreen;
```
--------------------------------
### Check Wallet Availability using Expo Wallet (JavaScript)
Source: https://context7.com/giulio987/expo-wallet/llms.txt
Checks if the native wallet functionality is available on the current device. This function returns a promise that resolves to a boolean indicating availability. It's recommended to call this before attempting to add passes to provide users with relevant feedback and disable the add pass functionality if the wallet is unavailable.
```javascript
import ExpoWallet from '@giulio987/expo-wallet';
import { Button, View, Text, StyleSheet } from 'react-native';
import { useEffect, useState } from 'react';
const WalletComponent = () => {
const [walletAvailable, setWalletAvailable] = useState(false);
const [loading, setLoading] = useState(true);
useEffect(() => {
checkWalletAvailability();
}, []);
const checkWalletAvailability = async () => {
try {
const available = await ExpoWallet.isAvailable();
setWalletAvailable(available);
console.log('Wallet available:', available);
} catch (error) {
console.error('Error checking wallet availability:', error);
setWalletAvailable(false);
} finally {
setLoading(false);
}
};
if (loading) {
return Checking wallet availability...;
}
return (
{walletAvailable ? (
<>
✓ Wallet is available
>
) : (
✗ Wallet is not available on this device
)}
);
};
const styles = StyleSheet.create({
container: { padding: 20 },
status: { fontSize: 16, marginBottom: 10 }
});
```
--------------------------------
### Check Wallet Availability (React Native)
Source: https://github.com/giulio987/expo-wallet/blob/master/README.md
This JavaScript function checks if the Wallet application is available on the device. It displays an alert indicating whether the Wallet is available or not.
```javascript
const isAvailable = async () => {
const res = await ExpoWallet.isAvailable();
if (res) {
alert("Available");
} else {
alert("Not available");
}
};
```
--------------------------------
### Handle Expo Wallet Errors with WALLET_ERRORS Enum (JavaScript)
Source: https://context7.com/giulio987/expo-wallet/llms.txt
This snippet demonstrates how to handle errors when adding passes using the Expo Wallet module. It utilizes the WALLET_ERRORS Enum to identify specific failure reasons and provides user-friendly messages. The function takes pass data as input and returns a success status along with an error message if applicable. Dependencies include the '@giulio987/expo-wallet' package.
```javascript
import ExpoWallet, { WALLET_ERRORS } from '@giulio987/expo-wallet';
const addPassWithDetailedErrorHandling = async (passData) => {
try {
const result = await ExpoWallet.addPass(passData);
return { success: true, result };
} catch (error) {
// Map error codes to user-friendly messages
const errorMessages = {
[WALLET_ERRORS.E_PASS_LIBRARY_CANNOT_ADD]:
'This pass cannot be added to your wallet. It may already exist or be incompatible.',
[WALLET_ERRORS.E_PASS_LIBRARY_INVALID_DATA]:
'The pass data is invalid or corrupted. Please try again.',
[WALLET_ERRORS.E_PASS_LIBRARY_GENERIC]:
'An unexpected error occurred while adding the pass.',
[WALLET_ERRORS.E_PASS_LIBRARY_UNAVAILABLE]:
'Wallet services are not available on this device.',
};
const message = errorMessages[error.code] || 'Failed to add pass to wallet';
console.error('Wallet Error:', {
code: error.code,
message: error.message,
userMessage: message
});
return {
success: false,
error: error.code,
message
};
}
};
// Usage example
const handleAddPass = async () => {
const passData = Platform.OS === 'ios' ? iosPass : androidToken;
const { success, message } = await addPassWithDetailedErrorHandling(passData);
if (success) {
showSuccessNotification('Pass added successfully');
} else {
showErrorNotification(message);
}
};
```
--------------------------------
### Add Pass to Wallet using Expo Wallet (JavaScript)
Source: https://context7.com/giulio987/expo-wallet/llms.txt
Adds a pass or card to the device's native wallet. It accepts a base64-encoded .pkpass file for iOS or a JWT token for Google Wallet on Android. The function returns a promise that resolves on success or rejects with an error code, handling platform differences and potential errors like invalid data or unavailability.
```javascript
import ExpoWallet from '@giulio987/expo-wallet';
import { Platform, Alert } from 'react-native';
// Example with platform-specific pass data
const addPassToWallet = async () => {
try {
// For iOS: base64 encoded .pkpass file
const iosPassData = "H4sIAAAAAAAAA+2dbW/bNhSG..."; // base64 string
// For Android: JWT token from Google Wallet API
const androidToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...";
const passData = Platform.OS === 'ios' ? iosPassData : androidToken;
const result = await ExpoWallet.addPass(passData);
if (result === true) {
Alert.alert('Success', 'Pass added to wallet successfully!');
} else {
Alert.alert('Cancelled', 'User cancelled adding pass to wallet');
}
} catch (error) {
// Handle specific error codes
if (error.code === 'E_PASS_LIBRARY_CANNOT_ADD') {
Alert.alert('Error', 'Cannot add this pass to the wallet');
} else if (error.code === 'E_PASS_LIBRARY_INVALID_DATA') {
Alert.alert('Error', 'Invalid pass data format');
} else if (error.code === 'E_PASS_LIBRARY_UNAVAILABLE') {
Alert.alert('Error', 'Wallet is not available on this device');
} else if (error.code === 'GOOGLE_WALLET_API_NOT_AVAILABLE') {
Alert.alert('Error', 'Google Wallet API is not available');
} else {
Alert.alert('Error', 'Failed to add pass to wallet');
}
console.error('Wallet error:', error);
}
};
```
--------------------------------
### Add Pass to Wallet (React Native)
Source: https://github.com/giulio987/expo-wallet/blob/master/README.md
This JavaScript function demonstrates how to add a pass to the user's Wallet. It conditionally uses a base64 encoded string for iOS and a token for Android, based on the platform.
```javascript
const addToWallet = async () => {
try {
const res = await ExpoWallet.addPass(Platform.OS === "ios" ? pass : token);
} catch (error) {}
};
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.