Sticky MPKnowledgeDrop

Installing Swift on iOS App

  • 29 April 2020
  • 0 replies
Installing Swift on iOS App
Userlevel 5
Badge +4

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.


Tools needed:  Git, GitHub, Cocoapods installed, Xcode, Terminal (How to open Terminal on Mac or Windows.), a Mixpanel project to send data

Computer Systems I am using: MacOS Mojave 

Reference Guide: Mixpanel Swift Docs, Git Cheatsheet

Project Clone: dice app with no Mixpanel installed

  1. 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. n6GRwc_rWMovngkDPmyZrqPTP6GRNqzbyOfPOoyitNSqd_NnQ61mhmkpHn9_9fSvO4qH6W-6-sJZmtjYj0es8dfMX3a06ICdsVTx-_b9Gala1KQUGPOjstHxP0gh35EFOVM5ik0G

    • To clone the Github file, run git clone

    • After you clone it make sure you open it by entering
      cd diceAppUnCompleted-SwiftiOS13 in your terminal.


  2. 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, m_e-WeD7E9KVHz5ay1CNFIaQ3aGrzFGtEnE00iERBYid3zi8OF1nCrLaDp2LzGEWE7-w1sA8tbbtV4joXDdLSV5q54asCS-vduhCCU7SAvx7XVt9Q3Z8bP1JJuUCCGUuEwZ7OvotTo 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.

  3. Once you have installed Cocoapods, run pod setup to create a local CocoaPods spec mirror. Below is a picture of the computer being nice and confirming the  Setup completed.

  4. Run pod init to create a Podfile in your project. 

    Here is a picture of the Podfile you should see.n8U-rnphMBUVS-6XuRuDYhBdhUaffewVL3Y0W4dzN1JAGobSOO8vCIjBFMu8w8FCbEJ2FLPwOZHDqbKTtLW3hAB8uH9XqFPEywKjna2BFtfohFrnnupAvoQ5xmOh_Cakg38NQ0RL

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

    1. First, here’s a quick trick to open your Podfile with Xcode in your terminal: run xed Podfile

    2. You can then add pod ‘Mixpanel-swift’ (copy and paste it, or type it in) to the file and save it. 7LZ5KceKGdX0Ga65XB6sxWUevXJns5CTJjFM4WW5BLotiur-b93pRZoY49RgMeNvTTzOnbYB06zbJlOLYvdKp-azM2FBhNtwfMPLXxwunvPmdz-atzDgXJorY4EW-Nr_46ZrGJuI

  6. Now you can run pod install in 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 pod installEPWAJShEJQ7DbXpIRgj2HSiy0swbwKUQGF63g8Qt2C_x5b9LeOGZLRoo2oskRIapOCOcQ6hU9A0oH9tePrCyZFb6OoKOKibbW-kDC4JiKvGVVsheKQd8pry-l0Yza7omElgWPwcF

    If you run pod init and pod install successfully, the terminal will create these files:maA6la4zBQK2WTW993lVU-EaaBM9Tr6cAMVFyzDxBfcERHqojD9wpFzlAIo5AeAgAwcbCaXg5P9ZA4vlDb84AS72qvhtu6KUMhclgJIp8kjzzwu3THVKrzFysDhTJ_jJDn52r3JC

  7. 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 xed AutoLayout-iOS13.xcworkspace in your terminal to open your project in Xcode.CZmag9SRTPFsMvKz_OgoQhjeC9KzpuljwG685_GJxoagEicddhTPHepD9SAwTp4eMONv1r1CDBadM8TBgetEJEjRG6NKeMaRIJ_Gn0nhWDGAeDBupUSTJN_dXYMykf-FkjuJd6DE

    When you ran pod install in 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. Yf3fVnqW_CluY5jDPFJuJSQVCOwo9aHdLL_sbQuG2ROm-QZdjigTJHhW-B6SQ19yw-RLQLxgxEEbf5ejhA6CgU33ElDMGd70eKKRbY0BXGwaMVTNnFDx98WkoY2zd1lq2HvI7atV

    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 AutoLayout-iOS13.xcworkspace project in Xcode.


  1. Once we are in Xcode, we want to initialize the Mixpanel Swift library in our AppDelegate.swift file that you can find in your file directory on the left hand side. When you open the file, you will need to include import Mixpanel at the top and then call Mixpanel.initialize(“YOUR-PROJECT-TOKEN”) and replace YOUR-PROJECT-TOKEN with your actual project token. F-q27mEs3VVcYBGz0aVl27n3CTjtSUtf2NaY3bh6hzG1Ndbgqr2uKycvlPIIsxnBU0Q8yy4gkVTwW-jJCFS0KTMxMx1_-PMyv6uG2n21AOUv7hZ1bn7PKQj35PJlxuydlloOpLRs

  2. 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. xBM7kzD8G4G8XfgWNLfYsl8K3jLdRh-JrRSvBADNBK1OCw8Q1cGFCKTrmlXK50LzfkQPovMBb3ULFbxw4MZxSEwhjMxHItw5jgVR1B7qBs8Zovf2xlLfh_Q223pcAI4a5K2p64qZIt 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!

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

0 replies

Be the first to reply!