Based on this answer I tried to retain my session in IOS and even though I am passing the cookies back with the request, I am still coming back when I exit and re-open the app, the difference from the above link is I am using django and its sessions
export default class App extends Component {
constructor(props) {
super(props);
this.currentUrl = '';
this.myWebView = React.createRef();
this.state = {
isReady: false,
cookiesString: '',
userAgent: '',
};
}
UNSAFE_componentWillMount() {
// CookieManager.clearAll();
this.provideMeSavedCookies()
.then(async (savedCookies) => {
let cookiesString = this.jsonCookiesToCookieString(savedCookies);
const sessionid = await AsyncStorage.getItem('sessionid');
if (sessionid) {
cookiesString += `sessionid=${sessionid};`;
}
DeviceInfo.getUserAgent().then((userAgent) => {
this.setState({userAgent: userAgent, cookiesString, isReady: true});
});
})
.catch((e) => {
this.setState({isReady: true});
});
}
onLoadEnd = () => {
let successUrl = `${domain}`;
if (this.currentUrl === successUrl) {
CookieManager.getAll().then((res) => {
console.log('RES');
console.log(res)
AsyncStorage.setItem('savedCookies', JSON.stringify(res));
if (res.sessionid) {
AsyncStorage.setItem('sessionid', res.sessionid.value);
}
});
}
};
jsonCookiesToCookieString = (json) => {
let cookiesString = '';
for (let [key, value] of Object.entries(json)) {
cookiesString += `${key}=${value.value}; `;
}
return cookiesString;
};
onNavigationStateChange = (navState) => {
this.currentUrl = navState.url;
};
provideMeSavedCookies = async () => {
try {
let value = await AsyncStorage.getItem('savedCookies');
if (value !== null) {
return Promise.resolve(JSON.parse(value));
}
} catch (error) {
return {}
}
};
render() {
const {userAgent, cookiesString, isReady} = this.state;
return (
<SafeAreaView style={{ flex:1 }}>
{isReady && <WebView
ref={this.myWebView}
source={{
uri: `${domain}`,
headers: {
'Cookie': cookiesString,
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'User-Agent': userAgent
},
}}
useWebView2={true} # this is just to test
cacheEnabled={false}
incognito={false}
scalesPageToFit
useWebKit
onLoadEnd={this.onLoadEnd}
onNavigationStateChange={this.onNavigationStateChange}
sharedCookiesEnabled
javaScriptEnabled={true}
domStorageEnabled={true}
/>}
{!isReady &&
<View><Text>Loading...</Text></View>
}
</SafeAreaView>
);
}
}
This is my cookiesString
csrftoken=6dkeV4w5qcUbAnM1IvUoIt7EhZVScsSbj4bkWHJLRXQWyk3zy40eSREqaeE0mpaT; sessionid=x2x65ksaz9k9izw756vnie4dmlgdx1zk; sessionid=x2x65ksaz9k9izw756vnie4dmlgdx1zk;