用户未在Share App Extension中登录到Firebase [英] User not signed into Firebase in Share App Extension

查看:69
本文介绍了用户未在Share App Extension中登录到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序共享扩展程序中进行了一些API Firebase调用(下载了一些用户特定的数据),但是用户未登录Firebase(又名currentUser为nil),所以我没有任何数据,因为我需要uid.换句话说,数据库仅允许经过身份验证的用户访问数据.我该如何解决?

解决方案

要在 KutakMir的答案上进行扩展,这些步骤可以正常工作对我们来说:

步骤1.为共享扩展名设置Firebase

Stackoverflow答案中的步骤:

  1. 将包含应用程序的GoogleService-Info.plist复制到您的 Xcode中的扩展名
  2. 将复制的GoogleService-Info.plist拖入 Xcode进入您的共享扩展名和
  3. 将BUNDLE_ID更改为共享扩展目标的名称
  4. 将新目标添加到您的Podfile
  5. 安装依赖项(pod install)
  6. 配置Firebase 扩展程序中的应用程序对象

步骤2.在包含(即,主)应用和共享扩展程序之间设置共享容器

描述于应用程序扩展编程指南:处理常见情况.基本上,在两个目标(即包含应用程序和共享扩展名)的功能选项卡上打开"应用程序组",并在两个位置提供相同的名称.

第3步.设置钥匙串共享

基本步骤与步骤2相似:在两个目标的功能标签上打开"钥匙串共享",并在两个位置都提供名称. (有关详细信息,请参见官方文档.)

在使用文档中的查询时,请确保将您的APP_ID附加到钥匙串"访问组中!上面的链接指出了这一点,但是 此Github问题评论更有用.

第4步.使用钥匙串和共享容器同步用户访问权限

我们的工作流程:

  • 在登录时(在共享扩展程序或主应用程序中),将用户ID保存到共享容器中,并使用用户ID作为密钥将用户名和密码保存到钥匙串中

  • 在启动时(共享扩展名或主应用程序),检查共享容器中的用户ID.如果存在,则意味着用户已经登录了主应用程序或共享扩展名,因此应同时登录这两个应用程序.因此,请从钥匙串中检索凭据,然后使用我们的项目在所有步骤都已设置好.重复很多,很麻烦,但是可以.相关文件是

    • 访问News/LoginViewController.swift
    • 访问News/SessionStartViewController.swift
    • 访问新闻/NVC.swift
    • 访问-新闻-上传器/UploaderNavigationViewController.swift

    跟踪钥匙串错误代码

    这是最令人沮丧的部分.错误代码的类型为OSStatus,并在安全框架结果代码,但有时在测试时只会看到一个有符号的整数.我找不到官方列表,但是 OSStatus.com 可以省去很多麻烦.例如,这是所有安全框架代码的查询.


    官方钥匙串文档的其他有用链接:

    I am making some API Firebase calls (download some user specific data) in my app share extension but the user is not signed into Firebase (aka currentUser is nil) so I don't get any data because I need the uid. In other words, the database only allows authenticated users to access the data. How can I fix this?

    解决方案

    To expand on KutakMir's answer, these steps worked for us:

    Step 1. Set up Firebase for the share extension

    Steps from this Stackoverflow answer:

    1. Copy the containing app's GoogleService-Info.plist into your extension in Xcode
    2. Drag the copied GoogleService-Info.plist into Xcode into your share extension and
    3. Change the BUNDLE_ID to the name of your share extension's target
    4. Add new target to your Podfile
    5. Install dependencies (pod install)
    6. Configure the Firebase application object in your extension

    Step 2. Set up shared container between containing (i.e., main) app and shared extension

    Described in App Extension Programming Guide: Handling Common Scenarios. Basically turn on "App Groups" at the Capabilities tab for both your targets (i.e., containing app and share extension), and provide the same designation at both places.

    Step 3. Set up keychain sharing

    The basic steps are similar to step 2: turn on "Keychain Sharing" at the Capabilities tab for both targets, and provide the designation at both places. (In more detail in the official docs.)

    Make sure to append your APP_ID to the Keychain access group when using the queries in the docs! The link above points this out, but this Github issue comment is more useful.

    Step 4. Use the Keychain and shared container to synchronize user access

    Our workflow:

    • On login (either in share extension or main app), save user ID to the shared container, and save username and password to the Keychain using the user ID as the key

    • On startup (either share extension or main app), check shared container for user ID. If there, it means that user already signed in either the main app or the share extension therefore they should be signed into both. So retrieve credentials from Keychain, and sign in user using signIn(withEmail:password:completion:)

    Our project at the point when everything was set up. It is messy with lots of duplication, but works. Relevant files are

    • Access News/LoginViewController.swift
    • Access News/SessionStartViewController.swift
    • Access News/NVC.swift
    • Access-News-Uploader/UploaderNavigationViewController.swift

    Tracking down Keychain error codes

    This was the most frustrating part. The error codes are of type OSStatus, and they are listed in Security Framework Result Codes, but sometimes one just sees a signed integer when testing. I couldn't find an official list, but OSStatus.com will save a lot of frustration. For example, here's a query of all the Security Framework codes.


    Other useful links to official Keychain docs:

    这篇关于用户未在Share App Extension中登录到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆