我有一个反应原生的测试应用程序,当我远程启用调试js时,一切正常.在运行后,它在设备(来自XCode)和模拟器中工作正常:
react-native run ios
Run Code Online (Sandbox Code Playgroud)
问题是,如果我停止远程js调试,登录测试将不再起作用.登录逻辑非常简单,我正在获取api来测试登录,API端点是通过https.
我需要改变什么?
更新:此代码与JS Debug Remote Enabled一起工作,如果我禁用它,它将不再起作用.
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
View,
Button,
Alert
} from 'react-native'
export default class MyClass extends Component {
constructor (props) {
super(props)
this.testFetch = this.testFetch.bind(this)
}
async testFetch () {
const email = 'email@example.com'
const password = '123456'
try {
const response = await fetch('https://www.example.com/api/auth/login', {
/* eslint no-undef: 0 */
method: 'POST',
headers: {
'Accept': 'application/json' /* eslint quote-props: 0 */,
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(email + ':' + password)
}
})
Alert.alert('Error fail!', 'Fail')
console.log(response)
} catch (error) {
Alert.alert('Error response!', 'Ok')
}
}
render () {
return (
<View style={styles.container}>
<Button
onPress={this.testFetch}
title="Test me!"
/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
}
})
AppRegistry.registerComponent('testingReactNative', () => MyClass)
Run Code Online (Sandbox Code Playgroud)
谢谢.
Pao*_*aez 43
这就是我修复它的方法.正如@chemitaxis建议的那样,从NPM添加base-64模块:
npm i -S base-64
Run Code Online (Sandbox Code Playgroud)
基于此,我提出了几种使用它的方法:
然后,您可以使用别名导入'encode'和'decode'方法,这样:
import {decode as atob, encode as btoa} from 'base-64'
Run Code Online (Sandbox Code Playgroud)
当然,使用别名是可选的.
您可以在React Native上设置atob和btoa作为全局变量.然后,您不需要在您需要的每个文件上导入它们.你必须添加这个代码:
import {decode, encode} from 'base-64'
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
Run Code Online (Sandbox Code Playgroud)
你需要将其放置在你的开头index.js,以便它可以在其他文件使用前加载atob和btoa.
我建议你把它复制到一个单独的文件上(比如说base64Polyfill.js),然后导入它 index.js
zvo*_*ona 32
你去(https://sketch.expo.io/BktW0xdje).创建一个单独的组件(例如Base64.js),导入它并准备使用它.例如Base64.btoa('123');
// @flow
// Inspired by: https://github.com/davidchambers/Base64.js/blob/master/base64.js
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const Base64 = {
btoa: (input:string = '') => {
let str = input;
let output = '';
for (let block = 0, charCode, i = 0, map = chars;
str.charAt(i | 0) || (map = '=', i % 1);
output += map.charAt(63 & block >> 8 - i % 1 * 8)) {
charCode = str.charCodeAt(i += 3/4);
if (charCode > 0xFF) {
throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
}
block = block << 8 | charCode;
}
return output;
},
atob: (input:string = '') => {
let str = input.replace(/=+$/, '');
let output = '';
if (str.length % 4 == 1) {
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
}
for (let bc = 0, bs = 0, buffer, i = 0;
buffer = str.charAt(i++);
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
) {
buffer = chars.indexOf(buffer);
}
return output;
}
};
export default Base64;
Run Code Online (Sandbox Code Playgroud)
Lin*_*ler 10
您的问题的主要部分已得到解答,但是我认为对于启用远程调试的原因仍存在一些不确定性。
进行远程调试时,JavaScript代码实际上在Chrome中运行,并且与虚拟dom的差异已通过Web套接字传递给本机应用程序。
http://facebook.github.io/react-native/docs/javascript-environment
atob并且btoa可以在浏览器的上下文中使用,这就是它在此处运行的原因。
但是,当您停止调试时,JavaScript会再次在设备或模拟器上的进程中解释,而该进程无法访问浏览器提供的功能。
| 归档时间: |
|
| 查看次数: |
19319 次 |
| 最近记录: |