我正在为仅使用webview的android和iphone / ipad创建一个非常简单的应用程序。 如何存储用户名和密码,以便用户不必每次都输入它们。提前谢谢。
import React, { Component } from 'react';
import {
AppRegistry,
WebView
} from 'react-native';
export default class myapp extends Component {
render() {
return (
<WebView
source={{uri: 'https://mysecretappurl.com'}}
/>
);
}
}
AppRegistry.registerComponent('myapp', () => myapp);
感谢Fabian的快速回复。 我用injectJavascript解决了它,即使我关闭并重新启动Android和ios上的应用程序,数据仍然存在。我最初因为尝试使用asyncstorage和reactnativ-webview-bridge而陷入困境,但由于我缺乏知识,我未能实现它们。
import React, { Component } from 'react';
import {
AppRegistry,
WebView
} from 'react-native';
export default class myapp extends Component {
render() {
let jsCode = `
var cookie={};
document.cookie.split('; ').forEach(function(i){cookie[i.split('=')[0]]=i.split('=')[1]});
document.querySelector('#email').value=cookie['email'] || '';
document.querySelector('#password').value=cookie['password'] || '';
document.querySelector('#login button').onclick = function(){
document.cookie = 'email='+document.querySelector('#email').value;
document.cookie = 'password='+document.querySelector('#password').value;
};
`;
return (
<WebView
source={{uri: 'https://mysecretappurl.com'}}
injectedJavaScript={jsCode}
/>
);
}
}
AppRegistry.registerComponent('myapp', () => myapp);
答案 0 :(得分:4)
我不知道我是否理解正确: 您正在编写一个缩小的浏览器&#34; with react本身只显示您的网页,并且您想要在该页面上预填充登录表单?
如果确实如此,您正在寻找将React Native应用程序中的数据与WebView组件中的页面进行交换的可能性。看看this tutorial of react-native-webview-bridge。
我会尝试以下方法: