Embedding ReferralHero Widget in React Native WebView
1. Identify the Subscriber Using the API
2. Set Authentication Cookies in the WebView
3. Embed the Widget Inside a WebView
4. Auto-Redirect to the Share Screen
Implementation
import React, { useEffect, useState, useRef } from 'react';
import { View, StyleSheet, Alert } from 'react-native';
import { WebView } from 'react-native-webview';
import AsyncStorage from '@react-native-async-storage/async-storage';
const ReferralWidget = () => {
const webViewRef = useRef(null);
const [injectedJS, setInjectedJS] = useState('');
useEffect(() => {
const fetchId = async () => {
const id = await AsyncStorage.getItem("id");
if (id) {
const script = `
document.cookie = "__maitre-anonymous-MFxxxxxxxxx=${id}; path=/; max-age=86400";
document.cookie = "__maitre-session-MFxxxxxxxxx=${id}; path=/; max-age=86400";
window.ReactNativeWebView.postMessage(document.cookie);
`;
setInjectedJS(script);
}
};
fetchId();
}, []);
const handleMessage = (event) => {
//Alert.alert("Cookies", event.nativeEvent.data);
};
return (
<View style={styles.container}>
{injectedJS ? (
<WebView
ref={webViewRef}
source={{ uri: 'https://campaign.referralhero.com/MFxxxxxxxx/dashboard' }}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
onLoadEnd={() => console.log("WebView Loaded Successfully")}
injectedJavaScript={injectedJS}
onMessage={handleMessage}
/>
) : null}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
export default ReferralWidget;Last updated