ios - 提取被重定向到自定义 url 方案并失败

标签 ios http react-native

我有一个 React Native 项目,我通过 fetch() 将 http 请求发送到一个页面,该页面将我重定向到一个 url 方案,如 appname://data=123456。

我需要从位置 url 中提取这些数据,然后继续向 api 发出其他请求。问题是ios有这个问题。 Android 可以正常运行并且可以正常工作,但在 iOS 上我会收到“网络请求失败”的结果。

经过较长时间的调试后,我发现 react native 背后的实现捕获了一个错误,指出“不支持的 URL”。

有什么办法可以实现吗?我正在尝试使用自定义 URL 方案启动应用程序,现在 appname://写入 safari 会启动我的应用程序,但它对请求没有影响。

我用于请求的代码:OAUTH2_IS_URL 是 https://login.server.com 和 action/api/login

 const postData = {username, password};
    return fetch(BuildConfig.OAUTH2_IS_URL + action, {
        method: 'POST',
        credentials: 'same-origin',
        mode: 'same-origin',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: Object.keys(postData).map(key => key + "=" + 
     encodeURIComponent(postData[key])).join('&')
    });

最佳答案

我从您的问题中了解到,您正在尝试使用来自 fetch 请求的 URL 打开应用程序。

如果是这种情况,您应该使用 Linking API react native 。您可以使用canOpenURL()测试该URL是否受支持。然后使用 openURL() 启动应用程序

Linking.canOpenURL(url).then(supported => {
  if (!supported) {
    console.log('Can\'t handle url: ' + url);
  } else {
    return Linking.openURL(url);
  }
}).catch(err => console.error('An error occurred', err));

关于ios - 提取被重定向到自定义 url 方案并失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46346500/

相关文章:

image - resizeMode 包含在 native react 中显示图像前后的空间

ios - 在 Swift 中批量创建变量

ios - 将现有的 sencha touch Web 应用程序移植到 ios native 应用程序

php - 将 PHP 应用程序部署到 Apache 服务器 Httpd 时出现问题

python - 如何向 urllib2 开启程序添加标题?

ios - 如何为 React Native 构建 .ipa?

ios - setContentOffset 从 ScrollView 的底部开始不起作用

ios - UIPopoverController 顶部的 UIButton 消失了

Python 套接字模块。连接到 HTTP 代理然后对外部资源执行 GET 请求

javascript - React Native 中组件之间的通信(子 -> 父)