谷歌分析崩溃与Swift 2.0

时间:2022-03-26 14:41:20

I've just installed GoogleAnalytics from CocoaPods and tried to use it, suddenly it crashes with an error:

我刚从CocoaPods安装了GoogleAnalytics并尝试使用它,突然它崩溃了,出现了一个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

The crash occurs in this part:

事故发生在这部分:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let tracker = GAI.sharedInstance().defaultTracker
    tracker.set(kGAIScreenName, value: "Main")

    let builder = GAIDictionaryBuilder.createScreenView()
    tracker.send(builder.build() as [NSObject : AnyObject])
}

And when reaching this line:

当到达这条线时:

tracker.set(kGAIScreenName, value: "Main")

Maybe it has something to do with the framework is bridged from Objective-C?

也许这和Objective-C的框架有关?


Update

更新

So I fixed the crash by wrapping it with an if statment and still nothing sent to Google Analytics:

因此,我用if语句包装了崩溃,但仍然没有发送到谷歌Analytics:

let name = "Main"
if let gai = GAI.sharedInstance() {
    if let tracker: GAITracker = gai.trackerWithTrackingId("TRACKING_ID") {
        tracker.set(kGAIScreenName, value: name)

        let builder = GAIDictionaryBuilder.createScreenView()
        tracker.send(builder.build() as [NSObject : AnyObject])

        print("tracker initialized")
    }
}

3 个解决方案

#1


1  

The docs for the GAI class have this to say about defaultTracker

GAI类的文档中有关于defaultTracker的信息

> For convenience, this class exposes a default tracker instance. This is initialized to nil and will be set to the first tracker that is instantiated in trackerWithTrackingId:. It may be overridden as desired.

为了方便起见,这个类公开一个默认的跟踪器实例。它被初始化为nil,并将被设置为在trackerWithTrackingId:中实例化的第一个跟踪器。它可能会被您所期望的覆盖。

So I'm guessing you need to call trackerWithTrackingId: somewhere, or if you are already doing it, make sure it happens before this method is called.

所以我猜您需要调用trackerWithTrackingId:某处,或者如果您已经在这样做,请确保在调用这个方法之前发生。

#2


1  

So I figured out the problem, I used Google Analytics with CocoaPods as explained in the Google Analytics iOS tutorial and added this line to the Bridging Header file:

所以我找到了问题所在,我使用了谷歌分析和CocoaPods,正如谷歌Analytics iOS教程所解释的,并将这一行添加到桥接头文件中:

#import <Google/Analytics.h>

but I guess that's not enough, after adding these lines:

但我想这还不够,加上这几行:

#import <GoogleAnalytics/GAI.h>
#import <GoogleAnalytics/GAIDictionaryBuilder.h>
#import <GoogleAnalytics/GAILogger.h>
#import <GoogleAnalytics/GAITrackedViewController.h>
#import <GoogleAnalytics/GAITracker.h>

It worked perfect. (Thanks Rich Tolley for the help)

它完美的工作。(感谢Rich Tolley的帮助)

#3


0  

For me I had to add those lines in the AppDelegate:

对于我来说,我必须在AppDelegate中添加这些行:

var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")

#1


1  

The docs for the GAI class have this to say about defaultTracker

GAI类的文档中有关于defaultTracker的信息

> For convenience, this class exposes a default tracker instance. This is initialized to nil and will be set to the first tracker that is instantiated in trackerWithTrackingId:. It may be overridden as desired.

为了方便起见,这个类公开一个默认的跟踪器实例。它被初始化为nil,并将被设置为在trackerWithTrackingId:中实例化的第一个跟踪器。它可能会被您所期望的覆盖。

So I'm guessing you need to call trackerWithTrackingId: somewhere, or if you are already doing it, make sure it happens before this method is called.

所以我猜您需要调用trackerWithTrackingId:某处,或者如果您已经在这样做,请确保在调用这个方法之前发生。

#2


1  

So I figured out the problem, I used Google Analytics with CocoaPods as explained in the Google Analytics iOS tutorial and added this line to the Bridging Header file:

所以我找到了问题所在,我使用了谷歌分析和CocoaPods,正如谷歌Analytics iOS教程所解释的,并将这一行添加到桥接头文件中:

#import <Google/Analytics.h>

but I guess that's not enough, after adding these lines:

但我想这还不够,加上这几行:

#import <GoogleAnalytics/GAI.h>
#import <GoogleAnalytics/GAIDictionaryBuilder.h>
#import <GoogleAnalytics/GAILogger.h>
#import <GoogleAnalytics/GAITrackedViewController.h>
#import <GoogleAnalytics/GAITracker.h>

It worked perfect. (Thanks Rich Tolley for the help)

它完美的工作。(感谢Rich Tolley的帮助)

#3


0  

For me I had to add those lines in the AppDelegate:

对于我来说,我必须在AppDelegate中添加这些行:

var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")