You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在React Native中使用Linking.openURL打开Gmail收件箱

React Native 调用Linking.openURL打开Gmail收件箱实现方案

前置配置

iOS端配置

需要在项目的ios/[项目名]/Info.plist中添加Gmail的URL Scheme白名单,找到LSApplicationQueriesSchemes节点,添加googlegmail字段:

<key>LSApplicationQueriesSchemes</key>
<array>
  <!-- 已有其他白名单配置保持不变,新增以下行 -->
  <string>googlegmail</string>
</array>

Android端配置

Android 11(API 30)及以上版本需要在android/app/src/main/AndroidManifest.xml的<manifest>根节点下添加包查询声明,否则canOpenURL会一直返回false:

<queries>
  <package android:name="com.google.android.gm" />
</queries>

核心实现代码

import { Linking, Alert, Platform } from 'react-native';

const openGmailInbox = async () => {
  // Gmail收件箱专用Scheme,iOS和Android通用
  const gmailInboxUrl = 'googlegmail:///inbox';
  try {
    const isGmailInstalled = await Linking.canOpenURL(gmailInboxUrl);
    if (isGmailInstalled) {
      await Linking.openURL(gmailInboxUrl);
    } else {
      Alert.alert('提示', '未检测到Gmail应用,请安装后重试');
    }
  } catch (error) {
    console.error('打开Gmail失败:', error);
    Alert.alert('提示', '打开Gmail失败,请稍后重试');
  }
}

补充说明

  • 直接调用上述openGmailInbox方法即可唤起Gmail并直接跳转到收件箱页面,不需要申请额外权限
  • 如果需要调整跳转目标,可修改Scheme路径:比如跳转发件页面可以用googlegmail:///co?to=xxx@gmail.com,还可通过query参数拼接主题、正文、抄送人等信息
  • 若用户未安装Gmail,可以根据业务需求调整兜底逻辑,比如跳转系统邮箱、引导用户下载Gmail等

内容的提问来源于stack exchange,提问作者Rajdeepsinghsakarwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 14:57:03