Categories:
Search articles

Search for articles

Sorry, but we couldn't find any matches...

But perhaps we can interest you in one of our more popular articles?

Distributing Native iOS app with Flutter Module using Codemagic

Mar 15, 2020

Written by Souvik Biswas

In this article, I will be showing how to distribute Native iOS project containing Flutter modules using Codemagic CI/CD.

The two main steps for distribution are:

  1. Code signing
  2. Publishing to App Store Connect

This is a follow-up article to “How to add Flutter modules to native iOS project and test it on Codemagic”.

Code signing

In order to publish your app to Apple App Store, you need to have a code-signed .ipa file. Before starting with the actual code signing process, you will need to have the following things:

  1. Certificate Signing Request (CSR)
  2. iOS Distribution Certificate
  3. App ID
  4. Provisioning Profile

Generating the Certificate Signing Request (CSR)

You will need a Mac to generate the CSR. The generated CSR will be stored on the local machine and contains some information related to the developer identity, which is later used by Apple for confirming the identity while generating the certificate.

  1. Open Keychain Access using Spotlight Search on Mac.

  2. From the top menu bar go to Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority.

  1. Fill out the Certificate Information form. Leave the CA Email Address field empty. Select the option Saved to disk. Then, click on Continue.
  1. Now, you can click on Show in Finder to go to the folder where the CSR file has been created.
  1. Click on Done.

iOS Distribution Certificate

You will need an Apple Developer Membership in order to generate a certificate from the Apple Developer Portal. Various types of certificates can be generated from the portal, but to publish our app to Apple App Store we will be needing an iOS Distribution Certificate.

Follow the steps below to generate the distribution certificate:

  1. Go to the Apple Developer Portal and sign in using your Apple ID and password.

  2. Select the Certificates, IDs & Profiles option from the left menu on the Dashboard.

  3. Under the Certificates option, click on the “+” button.

  4. Select the iOS Distribution option and click on Continue.

  5. Click on Choose File to upload the CSR file that we had generated previously. Click Continue.

  6. Now, you can Download the generated iOS Distribution Certificate to your local machine.

  7. Double-click on the downloaded certificate to add it to your local device's Keychain.

  8. You can export any certificate from your Keychain by right-clicking and selecting Export.

  9. You will have to supply a Certificate Password for securing the certificate.

App ID

You must register an App ID to make the app uniquely identifiable on the App Store after publication. This App ID will also be required for generating the Provisioning Profile in the next step.

The steps for registering an App ID are:

  1. Go to the Identifiers option in the left menu and click on the “+” button.

  2. Select the App IDs option under Register a New Identifier and click on Continue.

  3. Enter the Description and Bundle ID of the app. Click on Continue.

    You will get the Bundle ID of your project under the Xcode General tab.

    This Bundle ID needs to be unique. So, if the already defined Bundle ID of your project is not accepted in the portal, make sure you update the Bundle ID of the Xcode project with the accepted ID from the portal.

  4. Click on Register.

With this, you have successfully registered the Bundle ID of your iOS app.

Provisioning Profile

A Distribution Provisioning Profile is needed in order to publish an app to Apple App Store. To generate a distribution provisioning profile you will require the following:

  • App ID
  • Distribution Certificate

We already have a registered App ID and an iOS Distribution Certificate at hand, so we can move on to generating the provisioning profile.

Follow the steps below:

  1. Go to the Profiles option in the left menu and click on the “+” button.

  2. Select the App Store option from the list, and click on Continue.

    This will help to generate a distribution provisioning profile for publishing the app to Apple App Store.

  3. Select the App ID that we had registered previously from the drop-down list and click on Continue.

  4. Select a Distribution Certificate with which the app was signed and click on Continue.

  5. Enter a friendly Provisioning Profile Name for identifying the profile in the Apple Developer Portal. Click on Generate.

  6. You can Download the distribution provisioning profile to your local machine.

Managing Xcode code signing

Now that you have completed all the code signing steps on the Apple Developer Portal, you'll need to configure Xcode to use the proper Provisioning Profile for the app. This is very simple:

  1. Open the project using Xcode.

  2. Select the root project directory and go to the Signing and Capabilities tab.

  3. Here, you can either check Automatically manage signing or do the signing manually.

    • If you check the Automatically manage signing checkbox, then you will just need to select the Team from the dropdown list.

    • If you prefer to do the code signing manually, then you will need to upload the Provisioning Profile from the local machine.

Creating the app profile

To upload the iOS app to App Store Connect, follow the steps below:

  1. Log in to App Store Connect.

  2. Go to My Apps.

  3. Click on the “+” button and select New App.

  4. Fill out the details of the form and click on Create.

This will create a profile for your app where you can upload the .ipa of the app and add all the information required for publishing the app, like name, description, privacy policy, screenshots, etc. which are required for publishing the app.

Uploading the app using Xcode

Using Xcode, you can directly upload the .ipa file of the app to App Store Connect once the code signing step is complete and the app profile is created on the App Store Connect portal.

  1. In Xcode, select Generic iOS Device from the target devices list.

  2. From the top menu, go to Product > Archive.

  3. This will open a dialog box, displaying all the archives that you've created in the past.

  4. Select the recent build which you want to upload to App Store Connect and click on Distribute App.

  5. Select App Store Connect and click on Next.

  6. By default, both checkboxes will be checked. You do not need to change anything here – just click on Next.

  7. Select the Distribution Certificate and the App name from the drop-down list. Click on Next.

  8. Verify the information and click on Upload.

  9. Once the upload process is complete, click on Done.

This will upload the build version of the app to App Store Connect. Now, you can go to the portal and fill in the required information for publishing the app to Apple App Store.

Publishing the app

  1. Go to your app on App Store Connect.

  2. Under App Information, enter the Privacy Policy URL and select an Age Rating and Category.

  3. Under Pricing and Availability, select the price of the app and click on Save.

  4. Go to the option Prepare for Submission, upload screenshots of the app for various screen sizes, and fill out all the other required information.

  5. Select the Build version of the app that you want to publish and enter the Copyright information.

  6. Click on Submit for Review.

  7. Choose the appropriate options in the final form and click on Submit.

Finally, you have successfully sent your app for publishing; it will now go through a review process before it is uploaded to Apple App Store.

Distributing using Codemagic

Since I have already covered the code signing process using codemagic.yaml in the previous article, so I will not be going over the process again.

However, we need to change the certificate because we require a Distribution Certificate for publishing to the App Store (the certificate that we had used before was just a Development Certificate). Additionally, we’ll need to change the Provisioning Profile to the one that is generated from the Apple Developer Portal using the distribution certificate.

Updating the certificate and the profile

In the codemagic.yaml file we must change the value of the following keys:

  • CM_CERTIFICATE
  • CM_CERTIFICATE_PASSWORD
  • CM_PROVISIONING_PROFILE

You can generate the encrypted version of the files/variables by following these steps:

  1. Go to the project Settings from Codemagic UI.
  2. In the right sidebar, click Encrypt environment variables.

Here, you can drag and drop the files or type in the variables to generate their encrypted version. Copy their encrypted value and paste it in the codemagic.yaml file beside their appropriate keys.

Adding the App Store publishing script

In order to upload the .ipa file to App Store Connect from Codemagic, we have to add the following to the publishing script.

publishing:
      email:
        recipients:
          - <email>
      # Add the following to the script:
      app_store_connect:
        app_id: <bundle_id>
        apple_id: <apple_id>
        password: <app_specific_password>

Replace the angle brackets and the text enclosed within them with the appropriate values for each key.

The app-specific password can be generated by following the steps below:

  1. Sign in to your Apple ID account page.

  2. Scroll down to the Security section and click on Generate Password under App-Specific Passwords.

  3. You will have to give a name to the app, and then the password will be generated.

  4. Copy and paste the password in the codemagic.yaml file, replacing <app_specific_password> next to the password key.

Now, you are ready to run the script on Codemagic CI/CD.

The complete YAML file is available here.

NOTE: Make sure that you have updated the Build Version and Build Number using Xcode before running the script.

Running the script

  1. Go to your project from the Applications dashboard on Codemagic UI.

  2. Click on Start new build.

  3. Click on Select workflow from codemagic.yaml.

  4. Select the correct workflow and click on Start new build to start the building process.

This will test, build, and upload the .ipa to App Store Connect.

Successfully run script
Successfully run script

You can now go to your app on the App Store Connect portal. Go to the Activity tab > All builds. Here you will find the new version of the app. It will take some time to finish processing and then you are good to go.

Conclusion

Distributing your iOS app using codemagic.yaml helps you to skip a lot of manual steps so that you can get your app to production as fast as possible. Though the initial setup process is a bit time consuming, you only need to do it once and from then on the YAML file will be auto-detected by Codemagic CI/CD each time you run a build. It is also very convenient to manage the YAML file, as every time you make any changes to the file, you just have to push to the VCS.

  • The official documentation of Codemagic YAML is available here.
  • Documentation for iOS code signing is here.
  • Documentation for publishing to App Store Connect is here.
  • The GitHub repo of this project is available here.

Souvik Biswas is a passionate Mobile App Developer (Android and Flutter). He has worked on a number of mobile apps throughout his journey. Loves open source contribution on GitHub. He is currently pursuing a B.Tech degree in Computer Science and Engineering from Indian Institute of Information Technology Kalyani. He also writes Flutter articles on Medium - Flutter Community.

More articles by Souvik: