问题描述:

I tried to find informations about how to use Google Analytics with Swift 3, and even if it looks like some people succeeded, I can't make it work myself.

The Google Analytics documentation doesn't help, it's only for working for Swift 2.

I used the pod "Google/Analytics" in version 3.17.0 and tried to add this line inside a bridging header file as some people mentioned :

#import <Google/Analytics.h>

But I got an error about Xcode complaining that bridging headers doesn't work with Swift 3.

Then I tried to add the same line inside the .h as another post suggest, but doesn't work neither, Xcode complains "Include of non-modular header inside framework module XXX".

I tried to set "Allow Non-modular Includes in Framework Modules to YES but it doesn't change anything, still got the same error.

Last thing I tried is to add :

import Google

Inside the file where I use Google Analytics, but now GAI is not recognized.

// 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

Any suggestions ?

网友答案:

Follow these steps in order to configure Google Analytics

  1. Create a project on Google Analytics and download the configuration file "GoogleService-Info.plist".
  2. install the Google Analytics to you project using pods (make sure know error is shown in the terminal).
  3. Clear and close your project and then navigate to you project folder and open " XXX.xcworkspace " instead of "XXX.xcodeproj".
  4. Then add the "GoogleService-Info.plist" to your project (check copy if need option).
  5. Create a Bridging file in your project if there is already one exists then no need to recreate it.

    5.1. To create a bridging file the easiest way is to add/create new objective-c class to the project and the option pops up which ask you to create a bridging file and it sets all of the setting by default.

  6. Open the Bridging file normally named as "YourProjectName-Bridging-Header.h" and copy this in it " #import <Google/Analytics.h> "
  7. Open "AppDelegate.swift" and copy and paste the below code in didFinishLaunchingWithOptions for setting the Analytics tracker

    // 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
    
  8. Build your project.

I hope it works if there is any problem then let me know.

网友答案:

I faced the same problem. I could not import the "Google/Analytics.h" header as Xcode generate error. Because "Google/Analytics.h" header is not available in the 'GoogleAnalytics sdk' as mentioned in Google's Official page. So, i just used following line in AppDelegete.m file

#import "AppDelegate.h"
#import "GAI.h"

Hope it will work just fine. Environment Xcode: 8.2 iOS :10.2

相关阅读:
Top