File Downloads
Extended WebView with .ics File Download Handling
WebView with .ics File Download HandlingBasic WebView Setup
import * as IntentLauncher from 'expo-intent-launcher'; // Optional: If you're using Expo
import React from 'react';
import { Platform, PermissionsAndroid, Alert, Linking } from 'react-native';
import { WebView } from 'react-native-webview';
import RNFS from 'react-native-fs';
const WebViewScreen = () => {
const requestAndroidPermissions = async () => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission Required',
message:
'This app needs access to your storage to download calendar files.',
}
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
}
return true;
};
const handleFileDownload = async (url, mimeType) => {
if (Platform.OS === 'android') {
const hasPermission = await requestAndroidPermissions();
if (!hasPermission) {
Alert.alert('Permission Denied', 'Cannot download the file without permission.');
return;
}
const fileName = url.split('/').pop(); // Extract the filename
const downloadPath = `${RNFS.DownloadDirectoryPath}/${fileName}`;
// Download the file using RNFS
RNFS.downloadFile({
fromUrl: url,
toFile: downloadPath,
})
.promise.then(() => {
Alert.alert('Download Complete', `File saved to ${downloadPath}`);
})
.catch((err) => {
Alert.alert('Download Failed', `Error: ${err.message}`);
});
} else if (Platform.OS === 'ios') {
// Open the `.ics` file directly on iOS
Linking.openURL(url).catch(() => {
Alert.alert('Error', 'Unable to open the calendar file.');
});
}
};
return (
<WebView
source={{ uri: 'https://{clientId}.loveslisa.tech/s/{showId}' }}
javaScriptEnabled={true}
domStorageEnabled={true}
allowsInlineMediaPlayback={true}
onShouldStartLoadWithRequest={(request) => {
const { url } = request;
// Handle `.ics` file download
if (url.endsWith('.ics')) {
handleFileDownload(url, 'text/calendar');
return false; // Prevent the WebView from handling the download itself
}
return true; // Allow other navigation
}}
/>
);
};
export default WebViewScreen;Key Implementation Details
iOS
Android
Permissions Setup
Android
iOS
Features and Limitations
Features
Limitations:
Last updated