8

Anyone knows How do I write Bridging Header for Swift with CocoaPods 0.36?

I tried these ways.

(1)

#import <GoogleAnalytics-iOS-SDK/GAI.h>

=> this is cocoapods 0.35 style. failed to compile.

(2)

#import <GoogleAnalytics-iOS-SDK/GoogleAnalytics-iOS-SDK/GAI.h>

=> failed to compile.

(3)

#import "../Pods/GoogleAnalytics-iOS-SDK/GoogleAnalytics/Library/GAI.h"

=> it can be complied. but failed linking.

user3786678
  • 431
  • 3
  • 7

4 Answers4

15

I managed to successfully include Google Analytics iOS SDK 3.10 via Cocoapods into my Swift project using frameworks following these steps.

On the Podfile add (note the use_frameworks!):

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target "XXXX" do
    pod 'GoogleAnalytics-iOS-SDK', '~> 3.10'
end

target "XXXXTests" do
    pod 'GoogleAnalytics-iOS-SDK', '~> 3.10'
end

On the AppDelegate import section add:

import GoogleAnalytics_iOS_SDK

On the application didFinishLaunchingWithOptions method add:

    GAI.sharedInstance().trackUncaughtExceptions = true
    GAI.sharedInstance().dispatchInterval = 20
    GAI.sharedInstance().logger.logLevel = GAILogLevel.Verbose
    GAI.sharedInstance().trackerWithTrackingId("XXXX")
    GAI.sharedInstance().defaultTracker.allowIDFACollection = true

At this point, your code won't compile. You need to add other dependencies manually to your targets, both the application and the unit tests (as indicated at https://developers.google.com/analytics/devguides/collection/ios/v3/#headers).

  • CoreData.framework
  • SystemConfiguration.framework
  • libz.dylib
  • libsqlite3.dylib
  • libGoogleAnalyticsServices.a

Notice the libGoogleAnalyticsServices.a. For some reason, this is not included by Cocoapods when using frameworks. Without adding this file, the linker will fail with the following error:

Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_GAI", referenced from: __TMaCSo3GAI in AppDelegate.o

In order to add it, I downloaded the SDK manually (from this page: https://developers.google.com/analytics/devguides/collection/ios/resources) and dragged the libGoogleAnalyticsServices.a to my project making sure it was added to both targets and the 'copy' checkbox was checked.

enter image description here

After adding the file and other mentioned dependencies the project builds properly.

It seems as if Cocoapods was including nothing but the header files from the Google Analytics SDK. This solution is not perfect but avoids requiring to add a bridging header just for Google Analytics.

Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
  • It works, but I still get a bunch of warnings, saying that a class is implemented twice (objc[27350]: Class TAGDataProvider is implemented in both ...) – Paul van Roosendaal Apr 28 '15 at 08:00
  • Doing in this way you are trying to import GoogleAnalytics as a static library *and* as a dynamic library. So you end up with a lot of warnings similar to "Class TAG* is implemented in both GoogleAnalyticsSDK.framework and AppName.app/AppName" – Daniele Apr 28 '15 at 15:21
  • This method worked for me, but after seeing all the message noise and since I was already importing the other libraries I ended up just putting the whole thing in my project separate from CocoaPods for now. I'm not sure if it's a bug in the podspec or CocoaPods. – jimejim Apr 28 '15 at 22:52
  • Yeah, I see the warnings too. I'd be interested on finding a better solution. – Eneko Alonso May 01 '15 at 02:06
  • Just as a follow up, this no longer works with CocoaPods 0.37.0 nor 0.37.1 (see [Can't find headers for GoogleAnalytics-iOS-SDK with Cocoapods 0.37, Swift, frameworks](http://stackoverflow.com/questions/30020578/cant-find-headers-for-googleanalytics-ios-sdk-with-cocoapods-0-37-swift-frame) and [Missing header files for 'static object and headers' pod in 0.37.0. Swift, use_frameworks!](https://github.com/CocoaPods/CocoaPods/issues/3499)). – Eneko Alonso May 12 '15 at 05:43
1

I had a similar problem on CocoaPods 0.39.0.

$(inherited) in build setting 'OTHER_LDFLAGS' solved it.

https://stackoverflow.com/a/32004207/3129306

Community
  • 1
  • 1
Jan F.
  • 1,681
  • 2
  • 15
  • 27
0

Having the same issue currently...

I got the bridging header to work by using:

#import <GoogleAnalytics_iOS_SDK/GAI.h>
#import <GoogleAnalytics_iOS_SDK/GAITrackedViewController.h>
#import <GoogleAnalytics_iOS_SDK/GAIDictionaryBuilder.h>
#import <GoogleAnalytics_iOS_SDK/GAIFields.h>

Unfortunately, while this passes the build, I cannot use it in my project. I will update if I find more...

lostincode
  • 39
  • 1
  • 1
  • 4
0

When using a bridging header, you should be able to import the Google Analytics iOS SDK by using the following import statement:

#import <GoogleAnalytics-iOS-SDK/GAI.h>

If you're using pods as frameworks (by setting 'use_frameworks!' in your podfile), you don't need the bridging file. In the swift class you then simply import it with the following statement:

import GoogleAnalytics_iOS_SDK

If this is not working, I think something's wrong in your project settings. To fix that, I'll need more info.

  • 1
    Have u been able to run it with "use_frameworks!"? I still get a linker error when I try to access GAI.sharedInstance.... – Hons Apr 27 '15 at 07:58
  • My response was premature, I still get an error as well. I will let you know when I have an answer. – Paul van Roosendaal Apr 27 '15 at 18:39
  • Seems like Google Analytics SDK still does not support use_frameworks! I'm adding it to my projects by hand, instead of using cocoa pods. – Eneko Alonso Apr 27 '15 at 22:10