Sticky MPKnowledgeDrop

Installing Swift on iOS App

  • 29 April 2020
  • 0 replies
  • 50 views
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. n4Tfrx0t3_XNr14Hi69P1eDXG83ikp8BLImRiykvW6NPN6jZvRBy7el_cNlFa1uv1SbtaFcuq_OvAUChoRyMRVo-fJzJf5JVgiZurKvbIXkGzo3MR2vwdWKdzlyhhbLLhK6SLTsQ

    • To clone the Github file, run git clone https://github.com/smcueto/diceAppUnCompleted-SwiftiOS13.git

    • 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, Rz9j6RbG56Z9pRFgvCZJoCCOW2H_UuV5gtoHW2C2cRzGyw4wVDM8ik88WaHpLeO_MX0BX5dQk6pU0cGJhpphYG8SCCS0qsAGoieFveCI4a83OIeAwMfWO1JHRTaderAVxO7uCaVYTo 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.
    QsjNd4CXSeiKstOYhguVEwXrxYAyBCxGZJ0FMneR_v3n6-rUEI7BlsKr6bPfen2CErS5Z2O1QGIMMOJZwu9RcmiIl9MMjLOj2AXO9y7b3U0h3QV7ThTuXLvX9Nk5_DQu6BWge_br
     

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

    Here is a picture of the Podfile you should see.tCuv6IL2k8xozJzArJYDFPMK0tWueNhwciWh52iRVBbX4V694ZUQFnrvf6WTam9Y2fmYH5fLTw9zGT2ktSvEeRWTFEoE3aOd09TfmCra2oQj5DPrKsTezd3ifqLjNIxvG0v7bLe7

  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. yCljjsnmS6QemYOJyhesO8LP9ss3LvPcYAA1s8q8dnKbxJcAum3sUpmAJ34I8RBg64L14-QWNaOwvb7Wkhlp-j4PQboJOj8jaxZQIrvVePpDwNhy_uxZeoXweyVvNbwKXMTruwSh

  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 installl5GciFmo8wsVEYhQ8I2RE-K4stGYbEGyXQ3W-kPWVgHNudVpfRBOP6fOFjMBOmUJHYSm7ak3UTpj7ClcMMGp1dG65H_qzfTfmBYYZYdnFLGjRddi8Ebp9d3hagjV7nc-yjnLxTYn

    If you run pod init and pod install successfully, the terminal will create these files:FR5JihJa4SWEqbpXp1-M-PTmzcnfuzi0zDa-XfOtAIK7I13nB-kvkuCLg6x_f1KIqHbrPmHP98cK4hgXAMVktJVPhhjvH4F4CGfM6xTfrWsvs-RijhxCs7GXV8LAuSmUKbuqWk0c

  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.BsRl5RSo6NiZDBOmfGVbRb5i3I3ZClmMt4oIj3xSzyuniFQOCtKnbekJBWXdhDhxuI5INd6-oYmRjMj4Jb7hrgj4oHiVf6BnCh1jRupAuJ-ibqrjHdjYuYNeFn3ShHTNglKF6L3y

    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. iHaWfHVTzXK5XpqAUDPgdmffZ5VrTihBtD3qaSzXzbep368LOWMmhBMuDIUm3fUAky4Hn-QUNBmk9t8diNH6rnqIEQ1DkHnjxDQE1AyrelOz_dcpZXkXOnq8gsr8NQbwaHVsUB4b

    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.
    H2USJiLyjb9vVkmu8qgqCKe-IEsrX7C0haXl0bzIwm64DgYg4BbATZcCIHx1rhTapKU9tIloibUDxL2TPTLklkyzMmkx6c_n4UjOVB1N4tFHpJBYGCpI5bH3XYdyasAr7woKfWOM

  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. 

    2xHpCoJ8SaOik5v5WLCRGlup7Sdbh6hNX42WAqTYtoxZ3TJwtdFQc5i9CTCNF-wcy6hz3vEmqNLS1kNmmdwNHNiETLy-89vXW2sD286nz4ZUNOoACm3L8c50w-a3jG1NN6zZe4fN

    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. R676n5azgdsjfQQohPwJyP6kDiOS8kiT8WtZArZIYkJ_1SryRVVpSXBQviALcDDjvsSPtfOsvLNDhCKjJyaFJU6ZoxjjYnDDtpIikK5pZaf16AFWM7QIHq6DYoVUTMyxelfqdwYBIt 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!

Reply