谷歌分析与Swift 3 iOS 9

时间:2022-02-25 15:16:39

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.

我试图找到有关如何在Swift 3中使用Google Analytics的信息,即使看起来有些人成功了,我也无法让自己发挥作用。

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

Google Analytics文档没有帮助,只适用于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 :

我在版本3.17.0中使用了“Google / Analytics”窗格并尝试在桥接头文件中添加此行,正如有些人提到的:

#import <Google/Analytics.h>

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

但我得到一个关于Xcode抱怨桥接头不适用于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".

然后我尝试在.h中添加相同的行作为另一篇文章建议,但两者都不起作用,Xcode抱怨“在框架模块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.

在我使用Google Analytics的文件中,但现在GAI无法识别。

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

有什么建议 ?

2 个解决方案

#1


7  

Follow these steps in order to configure Google Analytics

请按照以下步骤配置Google Analytics

  1. Create a project on Google Analytics and download the configuration file "GoogleService-Info.plist".
  2. 在Google Analytics上创建项目并下载配置文件“GoogleService-Info.plist”。

  3. install the Google Analytics to you project using pods (make sure know error is shown in the terminal).
  4. 使用pod将Google Analytics安装到您的项目中(确保在终端中显示错误)。

  5. Clear and close your project and then navigate to you project folder and open " XXX.xcworkspace " instead of "XXX.xcodeproj".
  6. 清除并关闭项目,然后导航到项目文件夹并打开“XXX.xcworkspace”而不是“XXX.xcodeproj”。

  7. Then add the "GoogleService-Info.plist" to your project (check copy if need option).
  8. 然后将“GoogleService-Info.plist”添加到您的项目中(如果需要,请选中copy copy选项)。

  9. 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.

    5.1。要创建桥接文件,最简单的方法是向项目添加/创建新的objective-c类,弹出选项,要求您创建桥接文件,并默认设置所有设置。

  10. Open the Bridging file normally named as "YourProjectName-Bridging-Header.h" and copy this in it " #import <Google/Analytics.h> "
  11. 打开通常名为“YourProjectName-Bridging-Header.h”的桥接文件,并将其复制到其中“#import

  12. Open "AppDelegate.swift" and copy and paste the below code in didFinishLaunchingWithOptions for setting the Analytics tracker

    打开“AppDelegate.swift”并将以下代码复制并粘贴到didFinishLaunchingWithOptions中以设置Analytics跟踪器

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

    建立你的项目。

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

如果有任何问题,我希望它有用,然后让我知道。

#2


0  

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

我遇到了同样的问题。由于Xcode生成错误,我无法导入“Google / Analytics.h”标头。因为Google官方网页中提到的“GoogleAnalytics sdk”中没有“Google / Analytics.h”标题。所以,我只是在AppDelegete.m文件中使用了以下行

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

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

希望它能正常工作。环境Xcode:8.2 iOS:10.2

#1


7  

Follow these steps in order to configure Google Analytics

请按照以下步骤配置Google Analytics

  1. Create a project on Google Analytics and download the configuration file "GoogleService-Info.plist".
  2. 在Google Analytics上创建项目并下载配置文件“GoogleService-Info.plist”。

  3. install the Google Analytics to you project using pods (make sure know error is shown in the terminal).
  4. 使用pod将Google Analytics安装到您的项目中(确保在终端中显示错误)。

  5. Clear and close your project and then navigate to you project folder and open " XXX.xcworkspace " instead of "XXX.xcodeproj".
  6. 清除并关闭项目,然后导航到项目文件夹并打开“XXX.xcworkspace”而不是“XXX.xcodeproj”。

  7. Then add the "GoogleService-Info.plist" to your project (check copy if need option).
  8. 然后将“GoogleService-Info.plist”添加到您的项目中(如果需要,请选中copy copy选项)。

  9. 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.

    5.1。要创建桥接文件,最简单的方法是向项目添加/创建新的objective-c类,弹出选项,要求您创建桥接文件,并默认设置所有设置。

  10. Open the Bridging file normally named as "YourProjectName-Bridging-Header.h" and copy this in it " #import <Google/Analytics.h> "
  11. 打开通常名为“YourProjectName-Bridging-Header.h”的桥接文件,并将其复制到其中“#import

  12. Open "AppDelegate.swift" and copy and paste the below code in didFinishLaunchingWithOptions for setting the Analytics tracker

    打开“AppDelegate.swift”并将以下代码复制并粘贴到didFinishLaunchingWithOptions中以设置Analytics跟踪器

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

    建立你的项目。

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

如果有任何问题,我希望它有用,然后让我知道。

#2


0  

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

我遇到了同样的问题。由于Xcode生成错误,我无法导入“Google / Analytics.h”标头。因为Google官方网页中提到的“GoogleAnalytics sdk”中没有“Google / Analytics.h”标题。所以,我只是在AppDelegete.m文件中使用了以下行

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

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

希望它能正常工作。环境Xcode:8.2 iOS:10.2