通过 @react-native-google-signin/google-signin 授权时 React Native 应用程序崩溃

And*_*eev 5 react-native google-signin

我通过 @react-native-google-signin/google-signin 库进行 google 授权时遇到问题。在android中授权工作正常,在ios中它崩溃。崩溃日志片段:

...
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

...
Run Code Online (Sandbox Code Playgroud)

我的代码:

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>
  );
}
Run Code Online (Sandbox Code Playgroud)

崩溃发生在线路上

await GoogleSignin.hasPlayServices();
const item: any = await GoogleSignin.signIn();
Run Code Online (Sandbox Code Playgroud)

(调用signIn函数时,在向控制台显示信息之前)。

帮助了解 iOS 上可能出现的问题。如何修复它?

我遵循了 IOS 安装的所有步骤https://github.com/react-native-community/react-native-google-signin/blob/master/ios-guide.md 我检查了 GoogleSignIn 模块是否已正确安装在 cocoapods 中。重新安装了 @react-native-google-signin/google-signin 模块 崩溃发生在模拟器和 TestFlight 中。

我的依赖项:

  "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"
  },
Run Code Online (Sandbox Code Playgroud)

Nik*_* IO 10

就我个人而言,当我调用GoogleSignin.signIn()方法时,我的应用程序也会在iOS上崩溃。

为了解决这个问题,我需要打开 Xcode并:

  1. 转到我的GoogleService-Info.plist文件
  2. 复制 的值REVERSED_CLIENT_ID
  3. 导航到项目的目标并选择我的应用程序
  4. 选择“信息”选项卡并导航至“URL 类型”部分
  5. 通过点击加号 (+) 图标将新项目添加到 URL 类型部分
  6. 将 的值粘贴REVERSED_CLIENT_ID到 URL 方案字段中。

如果您觉得这些说明不够清楚,您可以观看5:20完成这些步骤的视频


小智 0

将react-native-google-signin/google-signin降级到9.0.2将解决此问题。