在Google Analytics for IOS中,defaultTracker为nil

时间:2022-02-26 13:55:20

I'm trying to set up Google Analytics in my iOS app by following this guide Google Analytics for iOS I have completed all the steps but when I run my app it crashes and says that my defaultTracker is nil. This is the following code in my ViewController:

我正在尝试按照iOS Analytics for iOS指南在我的iOS应用中设置Google Analytics我已经完成了所有步骤,但是当我运行我的应用时它会崩溃并说我的defaultTracker是零。这是我的ViewController中的以下代码:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var tracker = GAI.sharedInstance().defaultTracker
    tracker.set(kGAIScreenName, value: nil)

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

}

In my AppDelegate I have this code that should initialize everything regarding the tracker:

在我的AppDelegate中,我有这个代码,应该初始化有关跟踪器的所有内容:

func applicationDidFinishLaunching(application: UIApplication) {
    // 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.
    var gai = GAI.sharedInstance()
    gai.trackUncaughtExceptions = true  // report uncaught exceptions
    gai.logger.logLevel = GAILogLevel.Verbose  // remove before app release
}

Does anyone know what the problem might be and how to solve it?

有谁知道问题可能是什么以及如何解决?

Thanks in advance Vatan

在此先感谢Vatan

2 个解决方案

#1


7  

I had same issue, fixed with adding default tracker initialization at AppDelegate applicationDidFinishLaunching method like below;

我有同样的问题,修复了在AppDelegate applicationDidFinishLaunching方法中添加默认跟踪器初始化,如下所示;

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    //...

    var gai = GAI.sharedInstance()
    gai.trackUncaughtExceptions = true  // report uncaught exceptions
    gai.defaultTracker = gai.trackerWithTrackingId("UA-XXXXX-X")
    //...
 }

#2


0  

I think you need you set your defaultTracker to gai variable in applicationDidFinishLaunching after defining the gai variable.

我认为您需要在定义gai变量后在applicationDidFinishLaunching中将defaultTracker设置为gai变量。

#1


7  

I had same issue, fixed with adding default tracker initialization at AppDelegate applicationDidFinishLaunching method like below;

我有同样的问题,修复了在AppDelegate applicationDidFinishLaunching方法中添加默认跟踪器初始化,如下所示;

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    //...

    var gai = GAI.sharedInstance()
    gai.trackUncaughtExceptions = true  // report uncaught exceptions
    gai.defaultTracker = gai.trackerWithTrackingId("UA-XXXXX-X")
    //...
 }

#2


0  

I think you need you set your defaultTracker to gai variable in applicationDidFinishLaunching after defining the gai variable.

我认为您需要在定义gai变量后在applicationDidFinishLaunching中将defaultTracker设置为gai变量。