Expo React Native中AWS Amplify listUsersInGroup仅iOS遇网络错误
iOS环境下AWS Amplify API.get调用AdminQueries/listUsersInGroup接口出现网络错误
环境信息
- Expo 48.0.0
- React Native 0.71.5
- AWS Amplify(用于用户认证与API调用)
- 用户通过Lambda函数自动分配到对应分组,调用AWS AdminQueries的
/listUsersInGroup接口
问题现象
- Android、Web端调用接口完全正常
- iPhone 14模拟器的iOS环境下,使用Amplify的
API.get方法调用接口时触发网络错误 - 直接使用原生
fetchAPI发起相同请求,iOS环境下可正常执行
相关代码
import React, { useState, useEffect } from 'react'; import { View, Text, Button, Pressable, StyleSheet } from 'react-native'; import {useNavigation} from '@react-navigation/native'; import { Amplify, Auth, API } from 'aws-amplify'; import awsconfig from '../../aws-exports'; Amplify.configure(awsconfig); export default function EmployeesCultureplace(){ const navigation = useNavigation(); const [user, setUser] = useState() const [isReady, setIsReady] = useState(false); const [employees,setEmployees] = useState([]) const [hasAnyUsers,setHasAnyUsers] = useState(null); const [tempText, setTempText] = useState("no employees") let prepare = async () => { try { let userData = JSON.parse(await AsyncStorage.getItem('user')) setUser(userData) setHasAnyUsers(null) setEmployees([]) return userData } catch(e){ console.log(e) } finally { setIsReady(true) } } useEffect(() => { prepare().then((user) => { listUsers(user).then((e) => { setEmployees(user) }).catch((e) => { // here we catch the error console.log("ERROR") console.log(e) console.log(JSON.stringify(e)) }) }) },[]) // THIS IS THE FUNCTION that's giving alot of issues let nextToken; let listUsers = async (user) => { let apiName = 'AdminQueries'; let path = '/listUsersInGroup'; let myInit = { queryStringParameters: { "groupname": [(user["custom:group"])] }, headers: { 'Content-Type' : 'application/json', Authorization: `${(await Auth.currentSession()).getAccessToken().getJwtToken()}` } } const { NextToken, ...rest } = await API.get(apiName, path, myInit); nextToken = NextToken; return rest; } return ( <></> ) }
iOS端错误信息
{"message":"Network Error","name":"Error","stack":"Error: Network Error\n at createError (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:238728:26)\n at handleError (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:238650:27)\n at call (native)\n at dispatchEvent (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:31717:31)\n at setReadyState (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30442:33)\n at __didCompleteResponse (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30253:29)\n at apply (native)\n at anonymous (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30378:52)\n at apply (native)\n at emit (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2278:40)\n at apply (native)\n at __callFunction (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2812:36)\n at anonymous (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2573:31)\n at __guard (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2763:15)\n at callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2572:21)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"headers":{"Accept":"application/json, text/plain, */*","User-Agent":"aws-amplify/5.1.5 react-native","Content-Type":"application/json","Authorization":"TOKENXXX","method":"get","url":"https://xxxxx.execute-api.eu-central-1.amazonaws.com/dev/listUsersInGroup?groupname=project","host":"xxxxxx.execute-api.eu-central-1.amazonaws.com","path":"/dev/listUsersInGroup","data":"null","responseType":"json","cancelToken":{"promise":{"_h":1,"_i":0,"_j":null,"_k":{"onRejected":null,"promise":{"_h":0,"_i":0,"_j":null,"_k":null}}},"_listeners":[null]}},"status":null}
调试建议
- 修正查询参数格式:代码中
groupname的值为数组格式,GET请求的查询参数通常需要字符串类型,尝试修改为:queryStringParameters: { "groupname": user["custom:group"] } - 检查iOS ATS配置:确认项目的
info.plist已添加对应API域名的ATS规则,允许非HTTPS或特定域名的网络访问 - 对比请求差异:在iOS端分别打印
fetch和API.get的完整请求URL、请求头,排查Amplify是否自动添加了iOS不兼容的参数或头信息 - 升级Amplify版本:当前使用的
aws-amplify/5.1.5可能存在iOS特定bug,尝试升级到最新兼容版本 - 开启Amplify调试日志:添加
Amplify.Logger.LOG_LEVEL = 'DEBUG';后重新运行,查看更详细的请求流程日志,定位网络错误的具体触发点
内容的提问来源于stack exchange,提问作者Lukas Guldstøv
相关产品推荐
相关产品推荐

