在 React Native 回调后重定向
redirect after callback in React Native
我正在尝试通过 facebook 和 twitter 登录我的用户,验证过程在 API 中,所以我显示 facebook(或 twitter)登录页面,当用户使用他的凭据进行登录时, API returns 令牌。
问题是当我尝试在 Android 中进行测试时,不要重定向到主视图。我正在阅读并说这可能是我注入 javascript 代码的方式,但似乎没有任何效果。
然后在想可能是AndroidManifest中的uri方案,问题是回调url是这样的:
但在计划中我得到了这样的东西:
<data android:host="www.ciudad-facil.perceptum.cl"
android:pathPrefix="/callback/facebook"
android:scheme="http" />
但是不起作用,这是我用来向用户显示页面的代码:
_onMessage = ( event ) => {
console.log('entro');
var response = event.nativeEvent.data;
if (response != '') {
response = JSON.parse(response);
console.log(response);
try {
AsyncStorage.setItem('auth_token', response.token);
AsyncStorage.setItem('created_user', (response.created).toString());
global.auth_token = response.token;
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
Actions.Main();
}
}
render() {
var jsCode = "window.postMessage(document.getElementsByTagName('PRE')[0].innerHTML)";
return (
<WebView
source={{ uri: this.state.authUrl }}
onMessage={this._onMessage}
injectedJavaScript={jsCode}
startInLoadingState={true}
/>
);
}
PD:在 iOS 中,这非常有效。问题只是 Android
这是给所有路过的人的。解决方案是:
首先,在您的 AndroidManifest
上设置方案,例如:
<data android:host="www.xxxxx.com"/>
然后,当你使用Android时,注入JS代码的形式是:
window.__REACT_WEB_VIEW_BRIDGE.postMessage(....)
是的,这就是解决方案。
我正在尝试通过 facebook 和 twitter 登录我的用户,验证过程在 API 中,所以我显示 facebook(或 twitter)登录页面,当用户使用他的凭据进行登录时, API returns 令牌。
问题是当我尝试在 Android 中进行测试时,不要重定向到主视图。我正在阅读并说这可能是我注入 javascript 代码的方式,但似乎没有任何效果。
然后在想可能是AndroidManifest中的uri方案,问题是回调url是这样的:
但在计划中我得到了这样的东西:
<data android:host="www.ciudad-facil.perceptum.cl"
android:pathPrefix="/callback/facebook"
android:scheme="http" />
但是不起作用,这是我用来向用户显示页面的代码:
_onMessage = ( event ) => {
console.log('entro');
var response = event.nativeEvent.data;
if (response != '') {
response = JSON.parse(response);
console.log(response);
try {
AsyncStorage.setItem('auth_token', response.token);
AsyncStorage.setItem('created_user', (response.created).toString());
global.auth_token = response.token;
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
Actions.Main();
}
}
render() {
var jsCode = "window.postMessage(document.getElementsByTagName('PRE')[0].innerHTML)";
return (
<WebView
source={{ uri: this.state.authUrl }}
onMessage={this._onMessage}
injectedJavaScript={jsCode}
startInLoadingState={true}
/>
);
}
PD:在 iOS 中,这非常有效。问题只是 Android
这是给所有路过的人的。解决方案是:
首先,在您的 AndroidManifest
上设置方案,例如:
<data android:host="www.xxxxx.com"/>
然后,当你使用Android时,注入JS代码的形式是:
window.__REACT_WEB_VIEW_BRIDGE.postMessage(....)
是的,这就是解决方案。