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

Ionic iOS端谷歌Firebase认证跳转失败及通用链接报错排查

解决Ionic + Firebase谷歌认证iOS端跳转问题

问题背景

我通过Ionic按钮结合Firebase谷歌认证实现用户注册功能,Web端运行正常,但iOS应用构建完成后无法完成认证流程。尝试使用cordova-universal-links-plugin解决跳转问题时,先遇到配置缺失报错,添加配置后又出现文件路径不存在的错误。最终期望iOS端能打开系统默认浏览器完成谷歌注册流程。

现有代码

前端按钮代码

<ion-buttons end>
      <a href="#" (click)="GoogleAuth()" target="_blank">
        <img src="../../assets/signInButton.png">
      </a>
    </ion-buttons>

认证函数代码

async GoogleAuth() {
    try {
      const provider = new GoogleAuthProvider();
      await this.afAuth.signInWithRedirect(provider);
    } catch (error) {
      console.log(error);
    }
}

遇到的错误

  1. 首次构建iOS应用后无法打开认证页面,安装cordova-universal-links-plugin后提示:
<universal-links> tag is not set in the config.xml. Universal Links plugin is not going to work.
  1. 在config.xml中添加universal-links配置后,构建时触发文件路径错误:
IOS project Code Sign Entitlements now set to: DrinkTogether/Resources/DrinkTogether.entitlements
Entitlements file is not in references section, adding it
Error: ENOENT: no such file or directory, open '/Users/markbaumann/Desktop/drink-together/ul_web_hooks/ios/https:/drink-together-3d735.firebaseapp.com#apple-app-site-association'
    at Object.openSync (node:fs:600:3)
    at Object.writeFileSync (node:fs:2221:35)
    at saveContentToFile (/Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/lib/ios/appleAppSiteAssociationFile.js:118:8)
    at /Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/lib/ios/appleAppSiteAssociationFile.js:72:5
    at Array.forEach (<anonymous>)
    at createNewAssociationFiles (/Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/lib/ios/appleAppSiteAssociationFile.js:70:27)
    at Object.generate (/Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/lib/ios/appleAppSiteAssociationFile.js:45:3)
    at activateUniversalLinksInIos (/Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/afterPrepareHook.js:85:29)
    at /Users/markbaumann/Desktop/drink-together/plugins/cordova-universal-links-plugin/hooks/afterPrepareHook.js:50:11
    at Array.forEach (<anonymous>) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/Users/markbaumann/Desktop/drink-together/ul_web_hooks/ios/https:/drink-together-3d735.firebaseapp.com#apple-app-site-association'
}

解决方案

1. 移除问题插件

先卸载导致路径错误的cordova-universal-links-plugin:

ionic cordova plugin remove cordova-universal-links-plugin
npm uninstall @ionic-native/universal-links --save

2. 安装InAppBrowser插件

使用cordova-plugin-inappbrowser实现系统默认浏览器跳转:

ionic cordova plugin add cordova-plugin-inappbrowser
npm install @ionic-native/in-app-browser --save

3. 修改认证逻辑

更新GoogleAuth函数,优先使用弹窗认证,失败则 fallback 到系统浏览器,并监听回调完成认证:

import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';

constructor(private afAuth: AngularFireAuth, private iab: InAppBrowser) {}

async GoogleAuth() {
  try {
    const provider = new GoogleAuthProvider();
    // 优先尝试弹窗认证
    const result = await this.afAuth.signInWithPopup(provider);
    // 登录成功后的业务逻辑
    console.log('登录成功:', result.user);
  } catch (popupError) {
    // 弹窗失败则用系统浏览器打开认证页面
    await this.afAuth.signInWithRedirect(provider);
    const authUrl = await this.afAuth.getRedirectResult().catch(() => {
      // 获取认证跳转URL
      return provider.signInWithRedirect(this.afAuth);
    });
    const browser = this.iab.create(authUrl, '_system');
    
    // 监听回调URL,完成认证
    browser.on('loadstop').subscribe(async (event) => {
      if (event.url.includes('__/auth/handler')) {
        browser.close();
        const result = await this.afAuth.getRedirectResult();
        // 处理登录结果
        if (result.user) {
          console.log('浏览器登录成功:', result.user);
        }
      }
    });
  }
}

4. 调整config.xml配置

移除之前的universal-links配置,确保allow-intent包含所有必要的URL规则:

<widget id="de.markb.drinktogether" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>DrinkTogether</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="ScrollEnabled" value="false" />
    <preference name="BackupWebStorage" value="none" />
    <preference name="SplashMaintainAspectRatio" value="true" />
</widget>

5. Firebase配置检查

  • 确保Firebase控制台已添加iOS应用,且GoogleService-Info.plist文件已正确放置在项目的ios/DrinkTogether目录下
  • 确认Firebase认证面板中已启用谷歌登录方式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 03:09:27