xcode 使用未解析的标识符 GGLContext 和 GAI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37241346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of unresolved identifier GGLContext and GAI
提问by Matthew Bradshaw
I am having a bit of trouble. I am attempting to install Google Analytics into an app and am consistently getting the use of unresolved identifier GGLContext
and GAI
error. I receive the same error whether I am using CocoaPods or not. The location of the error is in the AppDelegate.swift
here:
我有点麻烦。我正在尝试将 Google Analytics 安装到应用程序中,并且一直在使用未解析的标识符GGLContext
和GAI
错误。无论我是否使用 CocoaPods,我都会收到同样的错误。错误的位置在AppDelegate.swift
这里:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
// Optional: configure GAI options.
let gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true // report uncaught exceptions
return true
}
Nothing additional is able to be imported into the AppDelegate.swift
(such as Google), just the standard UIKit
.
没有任何其他内容能够导入AppDelegate.swift
(例如 Google),只有标准的UIKit
.
I have been through many tutorials and other SO questions, all to no avail. I figure that there is some small thing I am missing, but cannot find it.
我已经阅读了许多教程和其他 SO 问题,但都无济于事。我想我缺少一些小东西,但找不到。
What I've done:
我所做的:
I have the .h
files in my project, along with libGoogleAnalyticsServices.a
, libsqlite3.0.tbd
, libz.tbd
, libsqlite3.tbd
(all of which have been linked to library as well as CoreData and SystemConfiguration).
我的.h
项目中有这些文件,以及libGoogleAnalyticsServices.a
, libsqlite3.0.tbd
, libz.tbd
, libsqlite3.tbd
(所有这些文件都已链接到库以及 CoreData 和 SystemConfiguration)。
You can see the layout of all these files here:
您可以在此处查看所有这些文件的布局:
and here:
和这里:
I have created the -Bridging-Header.h
and included these .h
imports in it.
我已经创建-Bridging-Header.h
并在其中包含了这些.h
导入。
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
#import "GAIEcommerceFields.h"
#import "GAIEcommerceProduct.h"
#import "GAIEcommerceProductAction.h"
#import "GAIEcommercePromotion.h"
#import "GAIFields.h"
#import "GAILogger.h"
#import "GAITrackedViewController.h"
#import "GAITracker.h"
The -Bridging-Header.h
is linked in the Build Settings and I receive no errors with that. That is the main solution that I have found during my research, which hasn't helped me in this situation.
将-Bridging-Header.h
在构建设置链接,我收到没有错误。这是我在研究期间找到的主要解决方案,在这种情况下对我没有帮助。
I have tried to start over from scratch twice with CocoaPods and without (I made a copy of my project before starting) and I received the same error each time.
我曾尝试使用 CocoaPods 从头开始两次,没有使用(我在开始之前复制了我的项目)并且每次都收到相同的错误。
Any help would certainly be appreciated. Thanks in advance.
任何帮助肯定会受到赞赏。提前致谢。
采纳答案by Matthew Bradshaw
Well, it looks like I was able to get it squared away.
好吧,看起来我能够把它摆平。
There were several problems with all attempts on this.
在这方面的所有尝试都存在一些问题。
Cocoapods had not installed correctly. I reinstalled and then had better success importing the correct files.
Doing it manually, as I posted above is not the best option.
After the Cocoapods re-install and starting over from a fresh copy of my project, I was able to
import Google
into myAppDelegate.swift
.
Cocoapods 没有正确安装。我重新安装,然后更好地成功导入了正确的文件。
手动进行,正如我在上面发布的那样并不是最好的选择。
在 Cocoapods 重新安装并从我的项目的新副本重新开始后,我能够
import Google
进入我的AppDelegate.swift
.
Key points for those who may end up in the same boat I was in:
对于那些最终可能与我处于同一条船上的人的关键点:
Be sure to add the correct directory for your
-Bridging-Header.h
. You can find this under Project - Build Settings - Swift Compiler Code Generation. Use this to easily target your header file$(SWIFT_MODULE_NAME)-Bridging-Header.h
In your
-Bridging-Header.h
, do not#import <Google/Analytics.h>
, instead import the files individually. Here is an image of the files available to be imported.
请务必为您的
-Bridging-Header.h
. 您可以在 Project - Build Settings - Swift Compiler Code Generation 下找到它。使用它可以轻松定位您的头文件$(SWIFT_MODULE_NAME)-Bridging-Header.h
在您的 中
-Bridging-Header.h
,不要#import <Google/Analytics.h>
,而是单独导入文件。这是可导入的文件的图像。
When in doubt, reinstall Cocoapods
Do not trust Google tutorials to provide the most effective instruction and utilize the many SO posts on the topic.
如有疑问,请重新安装 Cocoapods
不要相信 Google 教程会提供最有效的指导并利用有关该主题的许多 SO 帖子。
I really hope this helps someone not spend 10 hours on the problem as I have.
我真的希望这可以帮助人们不要像我一样在这个问题上花费 10 个小时。
回答by karan
Swift 4.0 and xcode 9.0.1 finally I resolved.
Swift 4.0 和 xcode 9.0.1 终于解决了。
For me after 2 days I resolved.. Don't follow Google's old documentation says #import <Google/Analytics.h>
2天后我解决了..不要按照谷歌的旧文档说 #import <Google/Analytics.h>
- Go to Terminal type
pod init
- Reopen project as workspace obvious after pod workspace is created, open podfile. write
pod 'GoogleAnalytics'
in your pod file beforetarget 'GoogleAnalytics' do
- Go back to Terminal
pod install
you will find frameworksGAI.h
and other files will be there under pods folder - Create
Header.h
file to your root. Don't add#import <Google/Analytics.h>
instead import following individually in bridging header file
- 转到终端类型
pod init
- 创建 pod 工作区后,重新打开项目作为工作区明显,打开 podfile。之前写入
pod 'GoogleAnalytics'
您的 pod 文件target 'GoogleAnalytics' do
- 返回终端,
pod install
您会GAI.h
在 pods 文件夹下找到框架和其他文件 - 创建
Header.h
文件到您的根目录。不要#import <Google/Analytics.h>
在桥接头文件中单独添加import 以下
e.g. in bridging header file remove #import <Google/Analytics.h>
例如在桥接头文件中删除 #import <Google/Analytics.h>
#import GAI.h
#import "GAITracker.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"
Point your bridge under Build Settings for target Swift Compiler - General -> Objective-C Bridging Header. write
Header.h
of your bridging file nameAdd code from google for swift to
didFinishLaunchingWithOptions
Don't forget to replace your tracking id from Google Analytics pageguard let gai = GAI.sharedInstance() else { assert(false, "Google Analytics not configured correctly") } gai.tracker(withTrackingId: "YOUR_TRACKING_ID") // Optional: automatically report uncaught exceptions. gai.trackUncaughtExceptions = true // Optional: set Logger to VERBOSE for debug information. // Remove before app release. gai.logger.logLevel = .verbose;
将您的网桥指向目标 Swift 编译器的构建设置 - 常规 -> Objective-C 桥接标头。写下
Header.h
你的桥接文件名将 google for swift 的代码添加到
didFinishLaunchingWithOptions
不要忘记从 Google Analytics 页面替换您的跟踪 IDguard let gai = GAI.sharedInstance() else { assert(false, "Google Analytics not configured correctly") } gai.tracker(withTrackingId: "YOUR_TRACKING_ID") // Optional: automatically report uncaught exceptions. gai.trackUncaughtExceptions = true // Optional: set Logger to VERBOSE for debug information. // Remove before app release. gai.logger.logLevel = .verbose;
Tada.... Run your project...
Tada ......运行你的项目......
回答by DevB2F
This works for swift 2.3, swift 3.0 and swift 4:
这适用于 swift 2.3、swift 3.0 和 swift 4:
- add the GoogleService-Info.plist file to the root of your project
add this to the podfile:
pod 'Google/Analytics'
- quit Xcode, run "pod install" in terminal and open Xcode again
create a header file in the project root, named Bridging-Header.h, under build settings make sure the bridging header is defined like in the picture
make sure your Bridging-Header.h looks like this:
#ifndef Bridging_Header_h #define Bridging_Header_h #import <Google/Analytics.h> #endif /* Bridging_Header_h */
add to AppDelegate:
import Google
add this code to AppDelegate in the didFinishLaunchingWithOptions method:
// Configure tracker from GoogleService-Info.plist. var configureError: NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error configuring Google services:\(configureError)") // Optional: configure GAI options. guard let gai = GAI.sharedInstance() else { assert(false, "Google Analytics not configured correctly") } gai.trackUncaughtExceptions = true // report uncaught exceptions gai.logger.logLevel = GAILogLevel.Verbose // remove before app release
- 将 GoogleService-Info.plist 文件添加到项目的根目录
将此添加到 podfile:
pod 'Google/Analytics'
- 退出 Xcode,在终端中运行“pod install”并再次打开 Xcode
确保您的 Bridging-Header.h 如下所示:
#ifndef Bridging_Header_h #define Bridging_Header_h #import <Google/Analytics.h> #endif /* Bridging_Header_h */
添加到 AppDelegate:
import Google
在 didFinishLaunchingWithOptions 方法中将此代码添加到 AppDelegate:
// Configure tracker from GoogleService-Info.plist. var configureError: NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error configuring Google services:\(configureError)") // Optional: configure GAI options. guard let gai = GAI.sharedInstance() else { assert(false, "Google Analytics not configured correctly") } gai.trackUncaughtExceptions = true // report uncaught exceptions gai.logger.logLevel = GAILogLevel.Verbose // remove before app release
If there are errors, deleting DerivedData and cleaning the project can help.
如果出现错误,删除 DerivedData 并清理项目会有所帮助。
回答by dan
My error was use of unresolved identifier when i was using the singleton GAI.sharedInstance()
.
我的错误是在使用 singleton 时使用了未解析的标识符GAI.sharedInstance()
。
My steps to bring this to work were:
我将其付诸实践的步骤是:
- add
pod 'Google/Analytics'
- pod install
- restart xcode
- create a objc class in my project to get a bridging header
- added
#import "GAI.h"
in to the bridging header file
- 添加
pod 'Google/Analytics'
- 吊舱安装
- 重启xcode
- 在我的项目中创建一个 objc 类以获得桥接头
- 添加
#import "GAI.h"
到桥接头文件中
everything works perfectly.
一切正常。
回答by user2477512
The only way to make it work for me was downgrade Google Analytics pod version to 2.0.4.
让它对我有用的唯一方法是将 Google Analytics pod 版本降级到 2.0.4。
pod 'Google/Analytics', '~> 2.0.4'
pod 'Google/Analytics', '~> 2.0.4'