【发布时间】:2017-04-28 03:39:00
【问题描述】:
我需要能够在我的 Today View Extension 中使用 Firebase,但是我似乎无法导入 Firebase 模块。我认为这是因为我的可可豆荚文件中需要一个新目标,但我不确定如何执行此操作。
谢谢。
【问题讨论】:
标签: ios swift firebase cocoapods today-extension
我需要能够在我的 Today View Extension 中使用 Firebase,但是我似乎无法导入 Firebase 模块。我认为这是因为我的可可豆荚文件中需要一个新目标,但我不确定如何执行此操作。
谢谢。
【问题讨论】:
标签: ios swift firebase cocoapods today-extension
您必须将今天的扩展程序视为它自己的独立应用程序(有点)
在您的 firebase 项目仪表板中,您需要点击“添加另一个应用程序”按钮。
选择 iOS,然后输入 TODAY EXTENSION 的 BUNDLE ID
完成向导并下载生成的 GoogleService-Info.plist 文件
将 plist 文件添加到 Today Extension 的根文件夹
转到您的 xcode 项目,并手动将 FirebaseCore.framework 和 FirebaseDatabase.framework 添加到您的 Today Extension
终于在今天的视图控制器调用 FirebaseApp.configure()
import FirebaseDatabase
import FirebaseCore
override func viewDidLoad() {
super.viewDidLoad()
FirebaseApp.configure()
}
【讨论】:
或者,无需在 Firebase 控制台中添加额外的应用,只需稍作修改即可重新使用主项目的 GoogleService-Info.plist(见下文)。 Firebase 应用单例必须在启动时在两个应用中进行配置。
要同步扩展程序和包含的应用程序,请参阅 App Extension Programming Guide: Handling Common Scenarios 或 this reddit comment。 This Stackoverflow thread 专门描述了这种场景。
步骤:
GoogleService-Info.plist 复制到 Xcode 中的扩展中GoogleService-Info.plist拖入Xcode到你的共享扩展中,然后Podfile
pod install)GoogleService-Info.plist 复制到 Xcode 中的扩展中GoogleService-Info.plist 拖入 Xcode 到您的共享扩展中,然后对我们来说,主要(即包含应用程序)是Access News,共享扩展名是Access-News-Uploader。
Podfile
# ...
target 'name-of-your-extension' do
use_frameworks!
pod 'Firebase/Core'
pod 'Firebase/Auth'
# etc.
end
Entire Podfile of our project.
pod install)/* 1. Import Firebase */
/**********************/
import Firebase
/**********************/
class WhereverInYourExtension: WhateverController {
// ...
override func viewDidLoad() {
super.viewDidLoad()
/* 2. Configure Firebase */
/*************************/
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
/*************************/
// ...
}
1) 仍然无法导入 Firebase!
确保为项目中的所有目标都安装了 pod。要实现这一点,请在 Podfile 中使用 inherit! 或 abstract_target。
使用来自the official documentation 的abstract_target 的最简单示例:
abstract_target 'Networking' do
pod 'AlamoFire'
target 'Networking App 1'
target 'Networking App 2'
end
对于inherit!,请参阅this SO question and answer。
2)如何在我现有的应用程序上实现这一点而不搞砸?
删除Podfile、Podfile.lock和YourProject.xcworkspace
发出pod init,它会一一列出你现有的目标。
通过在abstract_target 下分组或使用inherit! 来编辑Podfile
问题pod install
将生成一个新的YourProject.xcworkspace 文件,如果您使用此文件打开项目,在General > Linked Frameworks and Libraries 下会显示已添加 Firebase,可以从项目文件中导入。
(有关需要使用此清理方法的具体示例,请参阅this SO thread。)
3)
firebase 'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.
这对我有用:
通过从 Github 克隆我们的项目 repo 来清除所有内容,
已删除
在控制台发出pod init
重新创建Podfile(基本上是复制粘贴)
在控制台发出pod update
(下次可能就不行了。)
【讨论】:
据我所知,小部件不允许使用某些 api,例如 firebase。小部件应该显示主应用程序通过 UserDefaults 提供的数据,例如
TodayViewExtensions(或小部件)在代码方面可能只是非常简单。
【讨论】: