Community Tip: Implement Mixpanel in Swift AppsLast edited: Dec 10, 2017
This Community Tip will describe how to implement the Mixpanel iOS SDK, written in Objective-C, within your Swift app. We will walk through integration using CocoaPods, library set-up, provide code samples, and ultimately save you development time.
This guide assumes you are using CocoaPods since that is the recommended way to integrate your app with the Mixpanel iOS library. For assistance integrating Mixpanel into Swift projects without CocoaPods, please follow the instructions to build the Mixpanel iOS library from source.
Please note the below instructions for integration will only work for Swift apps targeting iOS 8.0 or higher. If you are targeting any versions below iOS 8.0 please jump to the final section of this guide.
Integration with CocoaPods
With any version of CocoaPods greater than 0.36, integration of Objective-C libraries within Swift apps is easier than ever thanks to the use_frameworks! podfile directive. In the past, many developers used a file called the Objective-C Bridging Header to utilize Objective-C files within Swift projects.
By instead employing the use_frameworks! directive you do not need a Bridging Header as all CocoaPods (Swift and Objective-C) will be compiled as dynamic frameworks instead of static libraries. Please note that for any Objective-C files in your Swift project which are not CocoaPods you will still need to enumerate these within a bridging header.
To update to the latest version of CocoaPods run the following command from your Terminal.
sudo gem install cocoapods
Once CocoaPods is installed and up to date, you should go to the directory of your project and create a podfile. Open the podfile and add the below, noting that use_frameworks! must come before any other pods are declared.
use_frameworks! pod 'Mixpanel'
Save your podfile, then in your Terminal run the below to have CocoaPods install the Mixpanel library as a dynamic framework.
Once installed, open the resulting .xcworkspace file and in any Swift file where you want to use Mixpanel add the following import statement.
Please note that prior to building for the first time, Xcode may display an error: No such module found ‘Mixpanel’. Upon your first successful build this error should disappear and Mixpanel will be integrated within your Swift app!
Swift Syntax with Mixpanel
Once you have successfully imported the Mixpanel framework into your AppDelegate.swift file, you can initialize the Mixpanel singleton in your applicationDidFinishLaunchingWithOptions method using the below, replacing the string with your Mixpanel project token.
You can now utilize this shared instance of Mixpanel in any file where you’ve imported the Mixpanel framework and begin to utilize all of the Mixpanel iOS SDK methods.
// Tracking an event Mixpanel.sharedInstance().track("Event Name", properties:["Prop Name":"Prop Value"]); // Setting People properties Mixpanel.sharedInstance().people.set(["$email":"email@example.com"]);
If your Swift project is targeting a version of iOS below 8.0 (Swift can target down to iOS 7.0), then you will not have the luxury of using the use_frameworks! directive as dynamic frameworks weren’t introduced until iOS 8.0. In this case, you will need the Objective-C Bridging Header we discussed earlier.
To implement a bridging header, create a new header file within your project, name it YourProduct-Bridging-Header.h and then add the following #import statement to the file.
Finally, within your app target’s Build Settings, set the path to the bridging header file.
Once linked, build your workspace and voilå! The Mixpanel library is now accessible from all of your Swift files using the syntax from above, no additional import statements needed.
If you’re struggling to get Mixpanel integrated within your Swift app or just have questions, please reach out to firstname.lastname@example.org to speak to someone smart, quickly.