Skip to content

Commit

Permalink
feat: add app tracking transparency to webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
antoKeinanen committed Jul 30, 2024
1 parent 9767208 commit 4de5e08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions components/WhitelistedWebview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useNavigation } from '@react-navigation/native';
import { useTrackingPermissions } from 'expo-tracking-transparency';
import { useEffect, useRef } from 'react';
import { BackHandler, Linking, Platform } from 'react-native';
import WebView, { WebViewProps } from 'react-native-webview';
Expand All @@ -20,6 +21,7 @@ const injectedJavascript = `(function() {
})();`;

function WhitelistedWebview({ whitelistedUrls, ...props }: WhitelistedWebviewProps) {
const [status, requestPermission] = useTrackingPermissions();
const webviewRef = useRef<WebView | null>(null);
const navigation = useNavigation();

Expand Down Expand Up @@ -48,10 +50,18 @@ function WhitelistedWebview({ whitelistedUrls, ...props }: WhitelistedWebviewPro
}
}, [navigation]);

useEffect(() => {
(async () => {
await requestPermission();
})();
}, [requestPermission]);

return (
<WebView
{...props}
ref={webviewRef}
sharedCookiesEnabled={status?.granted}
thirdPartyCookiesEnabled={status?.granted}
injectedJavaScript={injectedJavascript}
onNavigationStateChange={(newNavState) => {
let foundMatch = whitelistedUrls.find((regex) => regex.test(newNavState.url));
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"expo-notifications": "~0.28.9",
"expo-device": "~6.0.2",
"react-native-webview": "13.8.6",
"expo-updates": "~0.25.21"
"expo-updates": "~0.25.21",
"expo-tracking-transparency": "~4.0.2"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down

0 comments on commit 4de5e08

Please sign in to comment.