简体   繁体   English

React Native WebView,如何存储用户名和密码

[英]React Native WebView, how to store username and password

i am creating a very simple app for android and iphone/ipad that uses only webview. 我正在创建一个非常简单的Android和iphone / ipad应用程序,只使用webview。 How can i store username and password so the user would not have to type them in every single time. 如何存储用户名和密码,以便用户不必每次都输入它们。 Thanks in advance. 提前致谢。

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);

Thank you Fabian for a quick response. 感谢Fabian的快速回复。 I got it solved with injectedJavascript and the data persist even if i close and relaunch the app both on android and ios. 我用injectJavascript解决了它,即使我关闭并重新启动Android和ios上的应用程序,数据仍然存在。 I got stuck as at first as tried to go with asyncstorage and reactnativ-webview-bridge but i failed to implement them due to my lack of knowledge. 我最初因为尝试使用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);

I don't know if I understand you correctly: You are writing a "minified browser" with react native only to show your webpage and you want to prefill the login form on that page? 我不知道我是否理解正确:你正在编写一个“缩小的浏览器”,只有本地反应才显示你的网页,你想在该页面上预先填写登录表单?

If it's true you are searching for a possibility to exchange data from your React Native app with your page in the WebView component. 如果确实如此,那么您正在寻找将React Native应用程序中的数据与WebView组件中的页面进行交换的可能性。 Take a look at this tutorial of react-native-webview-bridge . 看一下react-native-webview-bridge的这个教程

I would try the following: 我会尝试以下方法:

  • Communicate with your Webpage and establish a listener for your login form to pass the credentials to your RN app 与您的网页通信并为您的登录表单建立一个监听器,以将凭据传递给您的RN应用程序
  • Use a module like react-native-simple-store to store the credentials in your RN app 使用react-native-simple-store之类的模块将凭证存储在RN应用程序中
  • If you start the app the next time check your storage and if the credentials are not empty send them to your webpage via bridge/ injected javascript 如果你下次启动应用程序检查你的存储,如果凭据不为空,请通过网桥/ 注入javascript将它们发送到你的网页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM