Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Implement native iOS clipboard clear after delay (#1157)

Leendert de Borstc6203b9e1991
diff --git a/apps/mobile-app/components/credentials/CredentialCard.tsx b/apps/mobile-app/components/credentials/CredentialCard.tsx
index 3cbd5e1cc..61ac4c43c 100644
--- a/apps/mobile-app/components/credentials/CredentialCard.tsx
+++ b/apps/mobile-app/components/credentials/CredentialCard.tsx
@@ -1,4 +1,3 @@
-import * as Clipboard from 'expo-clipboard';
import { router } from 'expo-router';
import { useTranslation } from 'react-i18next';
import { StyleSheet, View, Text, TouchableOpacity, Keyboard, Platform, Alert } from 'react-native';
@@ -11,7 +10,7 @@ import { useColors } from '@/hooks/useColorScheme';
import { CredentialIcon } from '@/components/credentials/CredentialIcon';
import { useAuth } from '@/context/AuthContext';
-import NativeVaultManager from '@/specs/NativeVaultManager';
+import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';
type CredentialCardProps = {
credential: Credential;
@@ -66,15 +65,11 @@ export function CredentialCard({ credential, onCredentialDelete }: CredentialCar
*/
const copyToClipboard = async (text: string): Promise<void> => {
try {
- await Clipboard.setStringAsync(text);
-
// Get clipboard clear timeout from settings
const timeoutSeconds = await getClipboardClearTimeout();
- // Schedule clipboard clear if timeout is set
- if (timeoutSeconds > 0) {
- await NativeVaultManager.clearClipboardAfterDelay(timeoutSeconds);
- }
+ // Use centralized clipboard utility
+ await copyToClipboardWithExpiration(text, timeoutSeconds);
} catch (error) {
console.error('Failed to copy to clipboard:', error);
}
diff --git a/apps/mobile-app/components/credentials/details/TotpSection.tsx b/apps/mobile-app/components/credentials/details/TotpSection.tsx
index 08983b201..af15d5884 100644
--- a/apps/mobile-app/components/credentials/details/TotpSection.tsx
+++ b/apps/mobile-app/components/credentials/details/TotpSection.tsx
@@ -1,4 +1,3 @@
-import * as Clipboard from 'expo-clipboard';
import * as OTPAuth from 'otpauth';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
@@ -13,7 +12,7 @@ import { ThemedText } from '@/components/themed/ThemedText';
import { ThemedView } from '@/components/themed/ThemedView';
import { useAuth } from '@/context/AuthContext';
import { useDb } from '@/context/DbContext';
-import NativeVaultManager from '@/specs/NativeVaultManager';
+import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';
type TotpSectionProps = {
credential: Credential;
@@ -74,15 +73,11 @@ export const TotpSection: React.FC<TotpSectionProps> = ({ credential }) : React.
*/
const copyToClipboardWithClear = async (code: string): Promise<void> => {
try {
- await Clipboard.setStringAsync(code);
-
// Get clipboard clear timeout from settings
const timeoutSeconds = await getClipboardClearTimeout();
- // Schedule clipboard clear if timeout is set
- if (timeoutSeconds > 0) {
- await NativeVaultManager.clearClipboardAfterDelay(timeoutSeconds);
- }
+ // Use centralized clipboard utility
+ await copyToClipboardWithExpiration(code, timeoutSeconds);
if (Platform.OS !== 'android') {
// Only show toast on iOS, Android already shows a native toast on clipboard interactions.
diff --git a/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx b/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
index 867747a49..80f32a37e 100644
--- a/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
+++ b/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
@@ -1,15 +1,14 @@
import { MaterialIcons } from '@expo/vector-icons';
-import * as Clipboard from 'expo-clipboard';
import React, { useState, useRef, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { View, Text, TouchableOpacity, StyleSheet, Platform, Animated, Easing } from 'react-native';
import Toast from 'react-native-toast-message';
+import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';
import { useColors } from '@/hooks/useColorScheme';
import { useAuth } from '@/context/AuthContext';
import { useClipboardCountdown } from '@/context/ClipboardCountdownContext';
-import NativeVaultManager from '@/specs/NativeVaultManager';
type FormInputCopyToClipboardProps = {
label: string;
@@ -30,7 +29,7 @@ const FormInputCopyToClipboard: React.FC<FormInputCopyToClipboardProps> = ({
const { t } = useTranslation();
const { getClipboardClearTimeout } = useAuth();
const { activeFieldId, setActiveField } = useClipboardCountdown();
-
+
const animatedWidth = useRef(new Animated.Value(0)).current;
// Create a stable unique ID based on label and value
const fieldId = useRef(`${label}-${value}-${Math.random().toString(36).substring(2, 11)}`).current;
@@ -49,7 +48,7 @@ const FormInputCopyToClipboard: React.FC<FormInputCopyToClipboardProps> = ({
// This field is now active - reset and start animation
animatedWidth.stopAnimation();
animatedWidth.setValue(100);
-
+
// Get timeout and start animation
getClipboardClearTimeout().then((timeoutSeconds) => {
if (timeoutSeconds > 0 && activeFieldId === fieldId) {
@@ -78,20 +77,17 @@ const FormInputCopyToClipboard: React.FC<FormInputCopyToClipboardProps> = ({
const copyToClipboard = async () : Promise<void> => {
if (value) {
try {
- // Copy to clipboard using expo-clipboard
- await Clipboard.setStringAsync(value);
-
// Get clipboard clear timeout from settings
const timeoutSeconds = await getClipboardClearTimeout();
- // Schedule clipboard clear if timeout is set
+ // Use centralized clipboard utility
+ await copyToClipboardWithExpiration(value, timeoutSeconds);
+
+ // Handle animation state
if (timeoutSeconds > 0) {
// Clear any existing active field first (this will cancel its animation)
setActiveField(null);
-
- // Schedule the clipboard clear
- await NativeVaultManager.clearClipboardAfterDelay(timeoutSeconds);
-
+
/*
* Now set this field as active - animation will be handled by the effect
* Use setTimeout to ensure state update happens in next tick
diff --git a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
index a3e42655f..fc798bc86 100644
--- a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
+++ b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
@@ -137,4 +137,8 @@
[vaultManager openAutofillSettingsPage:resolve rejecter:reject];
}
+- (void)copyToClipboardWithExpiration:(NSString *)text expirationSeconds:(double)expirationSeconds resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
+ [vaultManager copyToClipboardWithExpiration:text expirationSeconds:expirationSeconds resolver:resolve rejecter:reject];
+}
+
@end
diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
index 7a0b7384c..c804bf7d8 100644
--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
@@ -380,6 +380,37 @@ public class VaultManager: NSObject {
}
}
+ @objc
+ func copyToClipboardWithExpiration(_ text: String,
+ expirationSeconds: Double,
+ resolver resolve: @escaping RCTPromiseResolveBlock,
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
+ NSLog("VaultManager: Copying to clipboard with expiration of %.0f seconds", expirationSeconds)
+
+ DispatchQueue.main.async {
+ if expirationSeconds > 0 {
+ // Create expiration date
+ let expirationDate = Date().addingTimeInterval(expirationSeconds)
+
+ // Set clipboard with expiration and local-only options
+ UIPasteboard.general.setItems(
+ [[UIPasteboard.typeAutomatic: text]],
+ options: [
+ .expirationDate: expirationDate,
+ .localOnly: true // Prevent sync to Universal Clipboard/iCloud
+ ]
+ )
+
+ NSLog("VaultManager: Text copied to clipboard with expiration at %@", expirationDate.description)
+ } else {
+ // No expiration, just copy normally
+ UIPasteboard.general.string = text
+ NSLog("VaultManager: Text copied to clipboard without expiration")
+ }
+ resolve(nil)
+ }
+ }
+
@objc
func requiresMainQueueSetup() -> Bool {
return false
diff --git a/apps/mobile-app/specs/NativeVaultManager.ts b/apps/mobile-app/specs/NativeVaultManager.ts
index a3b82404d..fa88f437b 100644
--- a/apps/mobile-app/specs/NativeVaultManager.ts
+++ b/apps/mobile-app/specs/NativeVaultManager.ts
@@ -40,6 +40,7 @@ export interface Spec extends TurboModule {
// Clipboard management
clearClipboardAfterDelay(delayInSeconds: number): Promise<void>;
+ copyToClipboardWithExpiration(text: string, expirationSeconds: number): Promise<void>;
}
export default TurboModuleRegistry.getEnforcing<Spec>('NativeVaultManager');
diff --git a/apps/mobile-app/utils/ClipboardUtility.ts b/apps/mobile-app/utils/ClipboardUtility.ts
new file mode 100644
index 000000000..74dc9e64b
--- /dev/null
+++ b/apps/mobile-app/utils/ClipboardUtility.ts
@@ -0,0 +1,38 @@
+import * as Clipboard from 'expo-clipboard';
+import { Platform } from 'react-native';
+
+import NativeVaultManager from '@/specs/NativeVaultManager';
+
+/**
+ * Copy text to clipboard with automatic expiration based on platform capabilities.
+ * On iOS, uses native clipboard expiration. On Android, schedules manual clear.
+ *
+ * @param text - The text to copy to clipboard
+ * @param expirationSeconds - Number of seconds after which clipboard should be cleared (0 = no expiration)
+ */
+export async function copyToClipboardWithExpiration(
+ text: string,
+ expirationSeconds: number
+): Promise<void> {
+ if (Platform.OS === 'ios') {
+ // Use native iOS method with built-in expiration
+ await NativeVaultManager.copyToClipboardWithExpiration(text, expirationSeconds);
+ } else {
+ // For Android, use expo-clipboard and schedule manual clear
+ await Clipboard.setStringAsync(text);
+
+ // Schedule clipboard clear if timeout is set
+ if (expirationSeconds > 0) {
+ await NativeVaultManager.clearClipboardAfterDelay(expirationSeconds);
+ }
+ }
+}
+
+/**
+ * Copy text to clipboard without expiration.
+ *
+ * @param text - The text to copy to clipboard
+ */
+export async function copyToClipboard(text: string): Promise<void> {
+ await copyToClipboardWithExpiration(text, 0);
+}
\ No newline at end of file