fastlane Tutorial: Getting Started

In this fastlane tutorial you’ll learn how to take advantage of many of the tools provided to automate large and laborious parts of your life as an iOS dev! By Lyndsey Scott.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Creating Certificates and Provisioning Profiles

Open Fastfile in a text editor of your choice, disable smart quotes if your text editor supports them, then replace the contents of the file with the following code:

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.6.0"

default_platform :ios

platform :ios do

  # 1
  desc "Creating a code signing certificate and provisioning profile"
  # 2
  lane :provision do
    # 3
    produce(
      app_name: 'YOUR_UNIQUE_APP_NAME',
      language: 'English',
      app_version: '1.0',
      sku: '123abc'
    )
    # 4
    cert
    # 5
    sigh(force: true)
  end

  error do |lane, exception|
  # This block is called if there was an error running a lane.
  end

end

Replace YOUR_UNIQUE_APP_NAME with the app name you specified earlier. Your iTC username and the app identifier are loaded automatically from the Appfile, so you don’t need to provide them here.

Also replace the fastlane version with the most recent version if it’s no longer 2.6.0 (and it will most probably not be 2.6.0 since fastlane is well-maintained and updated frequently).

Note: To find out your current version, enter fastlane -v into the terminal.

If at any point, fastlane tells you to update to a newer version, but running sudo gem update fastlane outputs that there is “Nothing to update,” perhaps the ruby manager you’re using isn’t up to date. Run gem sources --add https://rubygems.org/ to install Ruby Gems since it’s likely to produce the most current information.

Note: To find out your current version, enter fastlane -v into the terminal.

If at any point, fastlane tells you to update to a newer version, but running sudo gem update fastlane outputs that there is “Nothing to update,” perhaps the ruby manager you’re using isn’t up to date. Run gem sources --add https://rubygems.org/ to install Ruby Gems since it’s likely to produce the most current information.

If you’ve never seen Ruby before, this may look like gibberish to you, so here’s what this code does:

  1. Provides a description for the lane. A lane is a workflow of sequential tasks.
  2. Names this lane provision.
  3. Uses produce to create a base app in both iTunes Connect and the Developer Portal with the specified identifier, name, language and version number.
  4. Uses cert to create a new private key and signing request, download and install the generated certificate and import all generated files in your keychain.
  5. Uses sigh to generate a provisioning profile. By specifying force: true, a new provisioning profile is created on each run; this ensures you’re always using the correct code signing certificate.

Note: sigh creates an App Store distribution profile by default. If you want to create an ad hoc profile you would need to specify sigh(adhoc: true). For a development profile it would be sigh(development: true). For simplicity, you’re only making a distribution profile in this tutorial.

Note: sigh creates an App Store distribution profile by default. If you want to create an ad hoc profile you would need to specify sigh(adhoc: true). For a development profile it would be sigh(development: true). For simplicity, you’re only making a distribution profile in this tutorial.

Woohoo! You have now created your very first lane. Save the file, open Terminal inside your project folder and enter the following command:

fastlane provision

This tells fastlane to run your provision lane.

After a minute or so, fastlane asks for your iTunes Connect password, which is safely stored in your computer’s keychain. Enter your password, and upon successful completion, your Terminal window should output the “fastlane summary”:

Note: If you see any errors, particularly one about “Creation of apps of this type is not available,” log in to iTunes Connect and make sure you don’t have any updated agreements waiting to be signed.

Note: If you see any errors, particularly one about “Creation of apps of this type is not available,” log in to iTunes Connect and make sure you don’t have any updated agreements waiting to be signed.

Log in to iTunes Connect, and voila! Your app has already been created. How cool is that?

fastlane tutorial

In order to use the provisioning profile you just created, you’ll have to make modifications in Xcode. Open the mZone Poker.xcodeproj, navigate to mZone Poker Target\Build Settings\Code Signing and set your Provisioning Profile to the newly created <app ID> AppStore. Then choose your Code Signing Identity based on this provisioning profile as shown below:

fastlane tutorial

Note that your code signing identity will be based on the identities available in the provisioning profile. By doing this, your app can use the newly created provisioning profile when gym builds the IPA file.

Navigate to General, deselect Automatically manage signing then set both the Signing (Debug) and Signing (Release) to that same provisioning profile you just specified.

With just a few commands and settings, you have added an app to your Dev Portal and iTunes Connect, created a provisioning profile and code signed your app. You have already saved yourself hours of work! :]

Screenshots Made Easy

When submitting an app, taking screenshots is by far the most tedious task. The more useful your app is (meaning the more devices and languages it supports), the more hours of valuable time you’ll burn taking screenshots. Painful!

mZone supports two languages, English and French, and four screen sizes. If you had to take five screenshots per device for each language and screen size, that would be 40 screenshots! With fastlane, however, you can do all this by running a single command.

But first, set up the project for snapshot by entering the following in Terminal:

fastlane snapshot init

A Snapfile file will now appear in your fastlane folder. Snapfile lets you specify the devices and languages you want to provide screenshots for.

Open Snapfile and replace the contents of the file with the following code:

# A list of devices you want to take the screenshots from
devices([
  "iPhone 5",
  "iPhone 6",
  "iPhone 6 Plus"
])
 
# A list of supported languages
languages([
  'en-US',
  'fr-FR'
])
 
# Where should the resulting screenshots be stored?
output_directory "./fastlane/screenshots"
 
# Clears previous screenshots
clear_previous_screenshots true
 
# Latest version of iOS
ios_version '10.1'

This simply sets the devices you want to support, the languages the app supports and specifies the location of the current screenshots directory. clear_previous_screenshots will clear any previously taken screenshots.

Wait a second… Why isn’t the iPhone 7 and iPhone 7 Plus on that device list? That’s because the screenshots on iTunes Connect are categorized by display size, not device; and since the iPhone 6 & 7 both have 4.7 inch displays, and the iPhone 6 Plus & 7 Plus both have 5.5 inch displays, including both the 6’s and 7’s in our screenshots folder would produce duplicate screenshots in iTunes Connect.

Save the file and close it.

Return to your terminal and note the instructions that appeared after running fastlane snapshot init:

Open your Xcode project and make sure to do the following:
1) Add a new UI Test target to your project
2) Add the ./fastlane/SnapshotHelper.swift to your UI Test target
   You can move the file anywhere you want
3) Call `setupSnapshot(app)` when launching your app

    let app = XCUIApplication()
    setupSnapshot(app)
    app.launch()

4) Add `snapshot("0Launch")` to wherever you want to create the screenshots

So that’s what you’ll do next.

Again open mZone Poker.xcodeproj in Xcode, then navigate to File\New\Target, within the iOS tab’s Test section, select iOS UI Testing Bundle then hit Next\Finish.

Return to your mZone folder’s fastlane directory then drag SnapshotHelper.swift into the newly created mZone PokerUITests folder in Xcode.

Open mZone_PokerUITests.swift, remove both the setUp and tearDown methods, then and add the following code within testExample:

  // 1
  let app = XCUIApplication()
  setupSnapshot(app)
  app.launch()

  // 2
  let chipCountTextField = app.textFields["chip count"]
  chipCountTextField.tap()
  chipCountTextField.typeText("10")
  // 3
  let bigBlindTextField = app.textFields["big blind"]
  bigBlindTextField.tap()
  bigBlindTextField.typeText("100")
  // 4
  snapshot("01UserEntries")    
  // 5
  app.buttons["what should i do"].tap()
  snapshot("02Suggestion")   

This code will help create your screenshots at certain points within the app’s execution. Here’s what you’re doing bit by bit:

  1. Set up your test to take snapshots and launch the app.
  2. The launched app will automate tapping the Chip Count text field (the accessibility identifier of which was pre-set to “chip count” in the Storyboard); and then it will automate entering the number 10 into that field.
  3. Then the app will tap the Big Blind text field (pre-set with the accessibility identifier of “big blind”) and enter the number 100 into that field.
  4. Take a snapshot at this point in order to produce a screenshot showing how the app looks with user entries.
  5. Automate tapping the What Should I Do? button, then take another screenshot to show the resulting alert.

Close Xcode, open Fastfile and add the following code right above error do |lane, exception|:

  desc "Take screenshots"
  lane :screenshot do
    snapshot
  end

Here you’re creating a new lane called screenshot that uses snapshot to take screenshots as specified by the Snapfile, which you just edited.

Save the file, return to Terminal and enter:

fastlane screenshot

Now watch … the screenshots are captured without you having to do anything else! Bask in the joy of skipping grunt work. :]

After the process finishes, the html file screenshots.html should automatically open upon screenshot completion and you can scroll through all the screenshots fastlane has taken.

Note: In order for snapshot to take screenshots, it needs access to the simulator equivalents of the devices listed in Snapfile. If you are missing one or more devices from that list, add them in Xcode by going to Window\Devices. In the bottom left corner, you can use the + to add new simulators.

You now have all your device screenshots in both English and French in just one Terminal command – it doesn’t get better than that!

Note: If you see warnings about ambiguous simulator names, you may need to delete some of your simulators or change the contents of your Snapfile.

snapshot can work in tandem with Xcode’s UI tests to give you screenshots of specific parts of the app as well! All the more reason to use tests. :]

Note: If you see warnings about ambiguous simulator names, you may need to delete some of your simulators or change the contents of your Snapfile.

snapshot can work in tandem with Xcode’s UI tests to give you screenshots of specific parts of the app as well! All the more reason to use tests. :]

Lyndsey Scott

Contributors

Lyndsey Scott

Author

Joshua Greene

Tech Editor

Chris Belanger

Editor

James Frost

Final Pass Editor

Andy Obusek

Team Lead

Over 300 content creators. Join our team.