If you have been working on iOS applications, you probably have pulled your hair out at some point. The signing of the application binary can be a real brain teaser. While there is no way around it, Microsoft is on a mission to automate it as much as possible. Enter Fastlane.
Out with the old...
To be able to run your app on a physical device, Apple needs you to sign your binary with certificates and provisioning profiles. Especially the first time, this can be daunting to say the least. In my opinion the process around it is hard and confusing. I know why it needs to be done, I see why it is important, but each time I forget how to do it and it takes me too long to set it up for my new app.
Luckily, Microsoft has seen this burden as well and has decided to do something about this. Since Apple is unlikely to respond to any of the requests of Microsoft, we can't get around to how Apple has arranged all of this. But what can be done is automate as much as possible in a way that we, as developers, do not have to be bothered with any of this anymore.
Microsoft has incorporated this now into Visual Studio for Mac and in this post I will show you how to set it up. Before we go and see there is one thing I need to note. I will assume that the Apple Developer account you're using is your own, or at least that you will have the Agent role, which gives you all the necessary rights to generate certificates and provisioning profiles. If you are working for a company or are part of a team, check to see if you have the required permissions. If not, this will not work.
At the time of writing, this isn't available in Visual Studio for Windows or automatic build pipelines, but I'm confident this will follow soon.
Automating with Fastlane
A tool that has been around for some while now is Fastlane, find more info on it on their website. Fastlane actually consists of multiple solutions, but one of them is code signing and that is what we will focus on here. Besides code signing, you can also use it to generate app screenshots, distribute beta versions and also deploy to the actual app stores. Since the tools are open-sourced on Github, you can actually see what is going on or maybe even contribute. First let's have a look at how to set things up on your Mac.
Visual Studio for Mac
The first thing we need to do is install Fastlane on our machine. A prerequisite is to have Xcode installed, but since we're all Xamarin developers here, I will assume you already did. There is two ways to install Fastlane. One is through Homebrew, the other one is to download the tools through the Fastlane website. I will show you the latter. If you know what Homebrew is and have set it up on your machine, simply run brew cask install fastlane.
Install the tooling
Go to https://download.fastlane.tools/ and a zip with the tools will start downloading immediately. Unpack it and run the install file. You will probably get a warning from Mac OS that you can't run this code. Go to your Mac OS system preferences, then Privacy & Security and click the Open Anyway button to run the install file. After you did, a Terminal window will pop up and start installing as seen in the screenshot below.
After copying a bunch of files, the installer will ask: Do you want fastlane to add itself to the PATH by updating your profile? (y/n). Affirm with Y.
Watch closely what happens next. Depending on your user account, you might see something like access denied. In either case, run the which fastlane command. The output should look like /Users/[user]/.fastlane/bin. If there is no output, run export PATH="$HOME/.fastlane/bin:$PATH" and check the which command again.
Configuring Visual Studio
Next up is to configure Fastlane in Visual Studio for Mac. Start Visual Studio, and open the preferences. Under the Publishing node, find the Apple Developer Accounts. Here is where you add your Apple account(s).
In the middle column, find the plus (+) button near the bottom of this screen. Through this button, you can add your Apple developer account to Visual Studio, and ultimately Fastlane. Enter your credentials and hit the Sign In button to add your account. The account details should pop up on the right side, after it was successfully added. Check that you have enough right. If it is your private account, you should be listed as Agent. If not, you will receive errors stating that your account does not have enough rights to generate certificates.
Now, for every iOS project, you can go into the info.plist file, and select the right team for this particular app. You can then tick the box to provision the app automatically and from there, you're life should be a lot easier.
Please note that, when enabling this option, for all build configurations the CodesignProvision and CodesignKey nodes will be changed/added in the iOS csproj file.
With this in place, there is one less thing you need to worry about when building your apps. This means there is more time to focus on implementing actual, awesome features! As mentioned; this is only available in Visual Studio for Mac at this time, but I'm sure the smart people at Microsoft will bring this to Windows in the near future.