React Native集成@react-native-google-signin时iOS端授权崩溃问题
React Native Google登录iOS端崩溃问题排查与修复
问题描述
Android端Google授权功能正常,但iOS端调用signIn时崩溃,崩溃发生在await GoogleSignin.hasPlayServices()和await GoogleSignin.signIn()执行阶段,控制台输出前就崩溃,模拟器和TestFlight环境均出现该问题。已按官方iOS安装指南完成配置,确认CocoaPods安装正常,重新安装过@react-native-google-signin/google-signin模块。
崩溃日志片段
... AppStoreTools: 14E221 AppVariant: 1:iPhone11,8:16 Beta: YES Code Type: ARM-64 (Native) Role: Foreground Parent Process: launchd [1] Date/Time: 2023-08-10 13:38:04.8666 +0300 Launch Time: 2023-08-10 13:38:02.2774 +0300 OS Version: iPhone OS 16.5.1 (20F770750d) Release Type: User Baseband Version: 5.02.02 Report Version: 104 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Triggered by Thread: 1 ... Thread 1 name: Thread 1 Crashed: 0 libsystem_kernel.dylib 0x000000020d9b6558 __pthread_kill + 8 (:-1) 1 libsystem_pthread.dylib 0x000000022e7f7118 pthread_kill + 268 (pthread.c:1670) 2 libsystem_c.dylib 0x00000001d5f9b178 abort + 180 (abort.c:118) 3 libc++abi.dylib 0x000000022e735bf8 abort_message + 132 (:-1) 4 libc++abi.dylib 0x000000022e725444 demangling_terminate_handler() + 348 (:-1) 5 libobjc.A.dylib 0x00000001c7b21ea4 _objc_terminate() + 144 (objc-exception.mm:498) 6 libc++abi.dylib 0x000000022e734fbc std::__terminate(void (*)()) + 16 (:-1) 7 libc++abi.dylib 0x000000022e734f60 std::terminate() + 56 (:-1) 8 libdispatch.dylib 0x00000001d5f3aec0 _dispatch_client_callout + 40 (object.m:563) 9 libdispatch.dylib 0x00000001d5f42534 _dispatch_lane_serial_drain + 668 (queue.c:3885) 10 libdispatch.dylib 0x00000001d5f430a4 _dispatch_lane_invoke + 384 (queue.c:3976) 11 libdispatch.dylib 0x00000001d5f4dcdc _dispatch_workloop_worker_thread + 648 (queue.c:6507) 12 libsystem_pthread.dylib 0x000000022e7f0ddc _pthread_wqthread + 288 (pthread.c:2618) 13 libsystem_pthread.dylib 0x000000022e7f0b7c start_wqthread + 8 (:-1) ... Thread 1 crashed with ARM Thread State (64-bit): x0: 0x0000000000000000 x1: 0x0000000000000000 x2: 0x0000000000000000 x3: 0x0000000000000000 x4: 0x000000022e739647 x5: 0x000000016b6d2850 x6: 0x000000000000006e x7: 0x0000000000000300 x8: 0x1999e4bb343495b2 x9: 0x1999e4ba5f59a5b2 x10: 0x0000000000000200 x11: 0x000000000000000b x12: 0x000000000000000b x13: 0x00000000001ff800 x14: 0x00000000000007fb x15: 0x0000000095634010 x16: 0x0000000000000148 x17: 0x000000016b6d3000 x18: 0x0000000000000000 x19: 0x0000000000000006 x20: 0x0000000000001c03 x21: 0x000000016b6d30e0 x22: 0x0000000000000000 x23: 0x000000016b6d30e0 x24: 0x0000000280841068 x25: 0x0000000000000000 x26: 0x0000000000000114 x27: 0x000000028334d940 x28: 0x000000028334d940 fp: 0x000000016b6d27c0 lr: 0x000000022e7f7118 sp: 0x000000016b6d27a0 pc: 0x000000020d9b6558 cpsr: 0x40000000 esr: 0x56000080 Address size fault ...
实现代码
import * as React from 'react'; import { View } from 'react-native'; import { useDispatch, useSelector } from 'react-redux'; import { GoogleSignin } from '@react-native-google-signin/google-signin'; export default function Start() { const signIn = async () => { try { await GoogleSignin.hasPlayServices(); const item: any = await GoogleSignin.signIn(); console.log('tokens', item); } catch (error: any) { console.log('error', error); } }; const isSignedIn = async () => { const isSignedIn1 = await GoogleSignin.isSignedIn(); if (!!isSignedIn1) { getCurrentUserInfo(); } else { console.log('Login Please'); } }; const getCurrentUserInfo = async () => { try { const userInfo1 = await GoogleSignin.signInSilently(); console.log(userInfo1); } catch (error: any) { console.log('Error', error.message); } }; React.useEffect(() => { GoogleSignin.configure({ scopes: [ 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', ], webClientId: '...', offlineAccess: true, forceCodeForRefreshToken: true, }); isSignedIn(); }, []); return ( <View> <React.Button onPress={() => signIn()} title="Grant account access" /> </View> ); }
项目依赖
"dependencies": { "@react-native-google-signin/google-signin": "^10.0.1", "@react-navigation/bottom-tabs": "^6.5.8", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.13", "@reduxjs/toolkit": "^1.9.5", "@types/react-navigation": "^3.4.0", "react": "18.2.0", "react-dom": "^18.2.0", "react-native": "0.72.3", "react-navigation": "^4.4.4", "react-navigation-stack": "^2.10.4", "react-redux": "^8.1.1" },
问题原因与修复方案
核心原因
hasPlayServices()API不兼容iOS:该方法是Android平台专属,用于检查Google Play服务可用性,iOS无对应服务,直接调用会触发原生崩溃。- iOS配置缺失或错误:URL Scheme、
Info.plist参数、GoogleService-Info.plist配置不全或错误,导致登录流程触发异常。 - CocoaPods依赖缓存或冲突:依赖安装不彻底,存在版本不匹配或缓存残留问题。
修复步骤
移除iOS端的
hasPlayServices()调用
仅在Android平台执行该检查,修改signIn函数:import { View, Platform } from 'react-native'; // 新增Platform引入 const signIn = async () => { try { // 仅Android平台调用hasPlayServices if (Platform.OS === 'android') { await GoogleSignin.hasPlayServices(); } const item: any = await GoogleSignin.signIn(); console.log('tokens', item); } catch (error: any) { console.log('error', error); } };验证iOS配置完整性
- 确认
Info.plist中添加了Google登录URL Scheme,格式为com.googleusercontent.apps.${iOS_CLIENT_ID}(iOS_CLIENT_ID为Google Cloud控制台中iOS客户端的ID,不是webClientId)。 - 确认
Info.plist中添加LSApplicationQueriesSchemes数组,包含"googleauth"条目。 - 检查
GoogleService-Info.plist已放入iOS项目根目录,且在Xcode中设置为目标成员。
- 确认
清理CocoaPods缓存并重新安装
执行以下命令:cd ios pod deintegrate pod cache clean --all pod install完成后重新编译iOS项目。
版本兼容性检查
当前@react-native-google-signin/google-signinv10.0.1与React Native 0.72.3兼容,若上述步骤无效,可尝试升级到库的最新版本,或确认GoogleSignIn iOS原生库版本匹配。
内容的提问来源于stack exchange,提问作者Andrey Alekseev
相关产品推荐
相关产品推荐

