LiSA Social Commerce
Developers
Developers
  • Developers
  • Guides
    • Integration Guide
      • Web Integration
      • App Integration
        • Android
          • Communication
          • Storage
          • File Downloads
          • Theming
        • iOS
          • Communication
          • Storage
          • File Downloads
          • Theming
        • React Native
          • Communication
          • Storage
          • File Downloads
          • Theming
        • Picture in Picture
      • Entrypoints
        • Query Parameter (v1)
    • Player Communication
      • Message API Reference
        • App
          • App — Heartbeat
          • App — Message Acknowledge
          • App — Message Error
          • App — Listen
        • CTA
          • CTA — Link in Comments
          • CTA — Sponsor Badge
        • Cart — View Cart
        • Comments — Join
        • Media
          • Media — Complete
          • Media — Pause
          • Media — Progress
          • Media — Resume
        • Media Item
          • Media Item — Emoji
          • Media Item — Impression
        • Player
          • Player — Dismiss
          • Player — Native Picture-in-Picture (PiP)
          • Player — Pass Visual Viewport
          • Player — Request Visual Viewport
          • Player — UI Transition
        • Products
          • Products — Add to Cart
          • Products — Add to Wishlist
          • Products — Click
          • Products — Close Product List
          • Products — Emoji
          • Products — Emoji State Update
          • Products — Impression
          • Products — Open Product List
        • Stickers
          • Stickers — Activate
          • Stickers — Click
          • Stickers — Expire
          • Stickers — Impression
          • Stickers — Publish
          • Stickers — Unlock
          • Stickers — Unpublish
          • Stickers — Update
          • Stickers — Voucher Claim
        • Visitor — Pass User Context
        • Shared Legacy Message Properties
    • Products
      • Product Update Notification API
  • Widgets
    • Content Hub
    • Quick Start Guide
    • Appearance
      • Markup
      • Responsive design
    • Configuration options
      • Autoplay
      • Channel
      • Client
      • Data
      • Debug
      • Host node
      • Layout
      • Language
      • On
      • Player
      • Query string
      • Quick view
      • Sections
      • Store
      • Template
    • API reference
      • Library API reference
      • Player API reference
      • Quick View API reference
    • Customisations
      • Template
      • Bring your own template
      • Type definitions
    • Examples
    • Type definitions
      • Asset
      • Product
      • Other
    • Promo Widget
      • Quick Start Guide
      • Configuration options
        • Autoplay
        • Channel
        • Countdown (deprecated)
        • Enabled
        • Image modifier
        • Lead time
        • Live (deprecated)
        • Position
        • Replay (deprecated)
        • Show ID
        • URL
  • Media Player Introduction
    • Picture-in-Picture Mode
  • Analytics
  • REST API
    • Authentication
    • Rate limits
    • Response status and error codes
    • API Documentation
Powered by GitBook
On this page
  • Extended WebView with .ics File Download Handling
  • Key Implementation Details
  • Permissions Setup
  • Features and Limitations
  1. Guides
  2. Integration Guide
  3. App Integration
  4. React Native

File Downloads

This implementation allows users to download .ics files (calendar events) from the LiSA Player running in a WebView, and save them to the device for easy "Add to Calendar" functionality.

Extended WebView with .ics File Download Handling

Basic WebView Setup

Below is the basic implementation of a WebView in React Native, extended to handle .ics file downloads:

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

Opening .ics Files

iOS handles .ics files natively. The implementation uses Linking.openURL to open the .ics file directly in the default calendar app.

Android

Download Handling

  • Android requires runtime permissions to write files to storage. The implementation uses PermissionsAndroid to request storage access.

  • Files are saved to the Downloads directory using the react-native-fs library.

React Native FS (File System)

Install the library to handle file downloads:

npm install react-native-fs

Link the native modules if you're not using React Native 0.60+:

react-native link react-native-fs

Permissions Setup

Android

Add the following permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

iOS

No special permissions are required for .ics downloads on iOS, but ensure the server URL is HTTPS to comply with App Transport Security.


Features and Limitations

Features

  1. File Detection: The onShouldStartLoadWithRequest method detects .ics files by checking the URL and MIME type.

  2. Platform-Specific Behavior:

    • iOS: Opens .ics files directly in the default calendar app.

    • Android: Downloads .ics files to the Downloads directory for manual import into calendar apps.

  3. Permissions Handling: Asks for storage permission on Android during runtime.

Limitations:

  • iOS Safari View Fallback: If you prefer opening files directly in a Safari View or an external browser, you can use react-native-inappbrowser-reborn instead of WebView.

  • Custom Calendar Handling: If you need to handle .ics files programmatically (e.g., parsing the file), additional libraries or custom solutions are required.


PreviousStorageNextTheming

Last updated 3 months ago