使用未解析的标识符GGLContext和GAI

Mat*_*haw 25 xcode google-analytics swift swift2

我遇到了一些麻烦.我正在尝试将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
}
Run Code Online (Sandbox Code Playgroud)

没有任何其他内容可以导入AppDelegate.swift(例如Google),只是标准UIKit.

我经历了很多教程和其他SO问题,但都无济于事.我认为我遗失了一些小东西,却找不到它.

我做了什么:

我有.h文件,在我的项目,随着libGoogleAnalyticsServices.a,libsqlite3.0.tbd,libz.tbd,libsqlite3.tbd(所有这些都被链接到库以及CoreData和SystemConfiguration).

您可以在此处查看所有这些文件的布局:

在此输入图像描述

和这里:

在此输入图像描述

我创建-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"
Run Code Online (Sandbox Code Playgroud)

它-Bridging-Header.h在Build Settings中链接,我没有收到任何错误.这是我在研究期间找到的主要解决方案,在这种情况下没有帮助我.

我试图用CocoaPods从头开始两次,没有(我在开始之前制作了我的项目的副本),每次都收到同样的错误.

任何帮助肯定会受到赞赏.提前致谢.

Mat*_*haw 16

嗯,看起来我能够把它平方.

所有尝试都有几个问题.

  1. Cocoapods没有正确安装.我重新安装,然后导入正确的文件取得了更好的成功.

  2. 手动完成,如上所述,不是最好的选择.

  3. Cocoapods重新安装并从我的项目的新副本开始,我能够import Google进入我的AppDelegate.swift.

那些可能最终在我所在的同一条船上的人的关键点:

  • 请务必为您的目录添加正确的目录-Bridging-Header.h.您可以在Project - Build Settings - Swift Compiler Code Generation下找到它.使用它可以轻松定位头文件$(SWIFT_MODULE_NAME)-Bridging-Header.h

  • 在您的-Bridging-Header.h,不要#import <Google/Analytics.h>,而是单独导入文件.以下是可导入文件的图像.

在此输入图像描述

  • 如有疑问,请重新安装Cocoapods

  • 不要相信Google教程提供最有效的指导并利用该主题上的许多SO帖子.

我真的希望这可以帮助一个人不要像我一样花10个小时解决这个问题.


kar*_*ran 13

最后我解决了Swift 4.0和xcode 9.0.1.

对我来说,2天后我解决了..不要按照Google的旧文档说 #import <Google/Analytics.h>

  1. 转到终端类型 pod init
  2. 创建pod工作区后,重新打开项目作为工作区,打开podfile.之前写pod 'GoogleAnalytics'在你的pod文件中target 'GoogleAnalytics' do
  3. 回到终端pod install你会发现框架GAI.h和其他文件将在pods文件夹下
  4. 创建Header.h根目录的文件.不要#import <Google/Analytics.h>在桥接头文件中单独添加以下导入

例如在桥接头文件中删除 #import <Google/Analytics.h>

#import GAI.h
#import "GAITracker.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"
Run Code Online (Sandbox Code Playgroud)
  1. 将您的网桥指向目标Swift编译器的Build Settings下 - General - > Objective-C Bridging Header.写下Header.h您的桥接文件名

  2. 从google for swift添加代码didFinishLaunchingWithOptions 不要忘记从Google Analytics页面替换您的跟踪ID

        guard 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;
    
    Run Code Online (Sandbox Code Playgroud)

田田....运行你的项目......


dan*_*dan 9

当我使用单例时,我的错误是使用了未解析的标识符GAI.sharedInstance().

我实现这一目标的步骤是:

  1. 加 pod 'Google/Analytics'
  2. pod安装
  3. 重启xcode
  4. 在我的项目中创建一个objc类来获取桥接头
  5. 添加#import "GAI.h"到桥接头文件中

一切都很完美.


Dev*_*B2F 9

这适用于swift 2.3,swift 3.0和swift 4:

  1. 将GoogleService-Info.plist文件添加到项目的根目录
  2. 将其添加到podfile:

      pod 'Google/Analytics'
    
    Run Code Online (Sandbox Code Playgroud)
  3. 退出Xcode,在终端中运行"pod install"并再次打开Xcode
  4. 在项目根目录中创建一个名为Bridging-Header.h的头文件,在构建设置下确保桥接头定义如图所示 在此输入图像描述

  5. 确保你的Bridging-Header.h看起来像这样:

      #ifndef Bridging_Header_h
      #define Bridging_Header_h
      #import <Google/Analytics.h> 
      #endif /* Bridging_Header_h */
    
    Run Code Online (Sandbox Code Playgroud)
  6. 添加到AppDelegate:

    import Google        
    
    Run Code Online (Sandbox Code Playgroud)
  7. 在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
    
    Run Code Online (Sandbox Code Playgroud)

如果有错误,删除DerivedData并清理项目可能会有所帮助.