用户未在Share App Extension中登录到Firebase [英] User not signed into Firebase in Share App Extension
问题描述
我在我的应用程序共享扩展程序中进行了一些API Firebase调用(下载了一些用户特定的数据),但是用户未登录Firebase(又名currentUser
为nil),所以我没有任何数据,因为我需要uid.换句话说,数据库仅允许经过身份验证的用户访问数据.我该如何解决?
要在 KutakMir的答案上进行扩展,这些步骤可以正常工作对我们来说:
步骤1.为共享扩展名设置Firebase
此 Stackoverflow答案中的步骤:
- 将包含应用程序的GoogleService-Info.plist复制到您的 Xcode中的扩展名
- 将复制的GoogleService-Info.plist拖入 Xcode进入您的共享扩展名和
- 将BUNDLE_ID更改为共享扩展目标的名称
- 将新目标添加到您的Podfile
- 安装依赖项(
pod install
) - 配置Firebase 扩展程序中的应用程序对象
步骤2.在包含(即,主)应用和共享扩展程序之间设置共享容器
描述于应用程序扩展编程指南:处理常见情况.基本上,在两个目标(即包含应用程序和共享扩展名)的功能选项卡上打开"应用程序组",并在两个位置提供相同的名称.>
第3步.设置钥匙串共享
基本步骤与步骤2相似:在两个目标的功能标签上打开"钥匙串共享",并在两个位置都提供名称. (有关详细信息,请参见官方文档.)
在使用文档中的查询时,请确保将您的APP_ID附加到钥匙串"访问组中!上面的链接指出了这一点,但是 此Github问题评论更有用.
第4步.使用钥匙串和共享容器同步用户访问权限
我们的工作流程:
-
在登录时(在共享扩展程序或主应用程序中),将用户ID保存到共享容器中,并使用用户ID作为密钥将用户名和密码保存到钥匙串中
-
在启动时(共享扩展名或主应用程序),检查共享容器中的用户ID.如果存在,则意味着用户已经登录了主应用程序或共享扩展名,因此应同时登录这两个应用程序.因此,请从钥匙串中检索凭据,然后使用我们的项目在所有步骤都已设置好.重复很多,很麻烦,但是可以.相关文件是
- 访问News/LoginViewController.swift
- 访问News/SessionStartViewController.swift
- 访问新闻/NVC.swift
- 访问-新闻-上传器/UploaderNavigationViewController.swift
跟踪钥匙串错误代码
这是最令人沮丧的部分.错误代码的类型为
OSStatus
,并在安全框架结果代码中列出a>,但有时在测试时只会看到一个有符号的整数.我找不到官方列表,但是 OSStatus.com 可以省去很多麻烦.例如,这是所有安全框架代码的查询.
官方钥匙串文档的其他有用链接:
- 向钥匙串添加密码
- 搜索钥匙串项
- 更新和删除钥匙串项目
-
kSecValueData
(这不是我期望的位置) - 项目返回结果键
- 项目类键和值
- 项目属性键和值
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:
- Copy the containing app's GoogleService-Info.plist into your extension in Xcode
- Drag the copied GoogleService-Info.plist into Xcode into your share extension and
- Change the BUNDLE_ID to the name of your share extension's target
- Add new target to your Podfile
- Install dependencies (
pod install
) - 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:
- Adding a Password to the Keychain
- Searching for Keychain Items
- Updating and Deleting Keychain Items
kSecValueData
(it wasn't where I expected it to be)- Item Return Result Keys
- Item Class Keys and Values
- Item Attribute Keys and Values
这篇关于用户未在Share App Extension中登录到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!