Every week, we will release tips to help you get the most out of Mixpanel. Want to see more? Click here to see other #mpknowledgedrop articles!
Hi there lovely people of our QBQ community! In the past couple of months, I have started learning mobile development, specifically iOS13 & Swift5 on Udemy with Angela Yu. As part of the course, I made an app that lets you roll a pair of dice. In this post, I want to invite you on my learning journey and show you how I installed the Mixpanel Swift on my app.
Computer Systems I am using: MacOS Mojave
Project Clone: dice app with no Mixpanel installed
Clone the dice app from GitHub into your computer.
You want to save a copy (clone) of the project somewhere where you remember. For me it is under a Swift folder.
To clone the Github file, run
After you clone it make sure you open it by entering
cd diceAppUnCompleted-SwiftiOS13in your terminal.
After you clone and open the project to your computer, you need to install Cocoapods by running
gem install cocoapods.
When I tried to
gem install Cocoapods, I ran into this error, To fix this, I looked up the issue on Stackoverflow and found a great explanation on why this is happening by joki, and steps on how to execute a fix by ryanpcmcquen.
Once you have
installed Cocoapods, run
pod setupto create a local CocoaPods spec mirror. Below is a picture of the computer being nice and confirming the
pod initto create a Podfile in your project.
Here is a picture of the Podfile you should see.
Once you have the Podfile in your folder, the next step is to open it, add pod ‘Mixpanel-swift’ to it, and save the file.
First, here’s a quick trick to open your Podfile with Xcode in your terminal: run
You can then add
pod ‘Mixpanel-swift’(copy and paste it, or type it in) to the file and save it.
Now you can run
pod installin your diceAppUnCompleted-SwiftiOS13 folder. This will install Mixpanel Swift and create pod files for your project.
This is what you should see in your Terminal when you run
If you run
pod installsuccessfully, the terminal will create these files:
At this point, you are ready to open our dice app project in Xcode to load Mixpanel Swift and initialize it.
You can use the simple trick of
xcworkspacein your terminal to open your project in Xcode.
When you ran
pod installin your project folder, this creates another mirrored file that we need to use ending in
xcworkspace. Below is a picture of what the file name looks like in your folder.
You would think to open the AutoLayout-iOS13.xcodeproj file in Xcode, but no no. Do not make the same mistake I did. We actually need to open the
xcworkspaceproject in Xcode.
Once we are in Xcode, we want to initialize the Mixpanel Swift library in our
AppDelegate.swiftfile that you can find in your file directory on the left hand side. When you open the file, you will need to include
import Mixpanelat the top and then call
Mixpanel.initialize(“YOUR-PROJECT-TOKEN”)and replace YOUR-PROJECT-TOKEN with your actual project token.
To make sure Mixpanel Swift is working in the dice app, you can run your app in the simulator by clicking the build button on the top left hand side of Xcode.
This will then trigger one of Mixpanel’s default mobile events, First App Open (
$ae_first_open). To confirm this event has been sent in, look at Live View in your Mixpanel project.
This is what you should see in Live view when you build your app. It might be that my wifi is really slow but it wanted to give a warning that it might take a minute or two for this event to show up. Make sure you check in on it!
That is it! You have now initialized Mixpanel Swift in your iOS dice app!
Let me know in the comments below if you got stuck on any step. I ran this a couple of times so happy to help!
Stay tuned for another MPKnowledgeDrop when we will be sending out events in our dice app.