Building a project for iOS

While there are some similarities to working with Android, there are some differences that are very important to note, so keep that in mind while reading this section. Let's build our project for the iOS device using the following steps: 

  1. At this point, we will dive into Unity and then move into our Build Settings menu once again by going to File | Build Settings.
  1. Click on the iOS option from the Platform list and then click on the Switch Platform button to make the change:

Note that this will make Unity reimport all of the assets in our game, so this may be time-consuming as you build larger and larger projects. This now also means that when we build our project, it will create an Xcode project instead of just an app, which we will need to open and work with once it's built.

  1. If we didn't do so earlier when building for iOS, we must set the bundle identifier for our game at this point, which is a string that identifies the app. It's written like a URL in reverse, for example, com.yourCompanyName.
    yourGameName
    . To modify this, we'll need to open up the Player Settings menu, which we can get to by clicking on the Player Settings... button in the Build Settings menu or by going to Edit | Project Settings | Player.
  2. Open up the Other Settings section, and then put in a value that you'd like under Package Name. (As mentioned in the previous section, I used com.JohnPDoran.MobileDev.)
Note that if you have already changed this property when building for Android, it will already be done; there's no need to do this again.
  1. Now, we can try to build the project by clicking on the Build button and saving it in the same Export folder we created earlier—in this case, I named it MobileDev_iOS:
You can press the down arrow button to search for folders in the Finder window that pops up.
  1. You may be asked to give permission for the Unity Hub to perform actions to Xcode. Allow it by pressing OK:
  1. Once the project has been built, we will be taken to a Finder window at the location where we created the project. From there, we can double-click on the .xcodeproj file to open the project inside Xcode:
  1. In Xcode, after waiting for everything to load in, you'll notice a yellow triangle with a ! in the center of it in the top-center console. If you click on it, you'll see some information appear on the left-hand side. 
  2. Double-click on the Update to recommended settings option on the left-hand side of the screen and then click on the Perform Changes button in the window that pops up:
  1. Then, go to the drop-down in the middle of the window and select the Unity-iPhone option under Targets:
  1. Afterward, under the Signing & Capabilities section, check the Automatically manage signing option, clicking on Enable Automatic when the popup comes up. Then, in the window that pops up, assign your team to your profile.
  1. If you have an iOS development account or if no errors show up, you can continue to Step 13; however, it is also possible that you may see an error stating that you need to set the code signing identity value to iOS Developer. If that is the case, from the top-center area go to the Build Settings section and under the Signing section, change all of the Code Signing Identity items to iOS Developer

Afterward, select the Product | Clean command from the top toolbar. You should then be able to proceed to Step 11.

  1. Once all the preceding steps are done, plug in your phone via USB. After loading all of the symbols it needs (wait until the top-middle section says Ready); at the top right, instead of Generic iOS Device, change it to the device you've connected.
  2. When you click on the Play button, the computer will ask whether you want to enable developer mode; to do so, go ahead and select Enable and enter your password when it asks for it.
  3. Your phone may be busy, so you may need to wait a bit before you're able to build to the device. You may get a window asking you to access the key access in your keychain. Go ahead and click Allow. You'll also need to unlock your phone at some point as well so that it can make the install.
  1. The app will now be on your iOS device, as you can see in the following screenshot:

Right now, it has a generic Unity icon, just like in Android, and we will customize it later on in this book, but for right now, we have an issue: the game won't run.

In order to run the app, you must verify that you want the device to be able to run the app to prevent security issues. To let you know that this is an issue, Xcode will give you a warning that it couldn't launch it, so we'll need to say we want to be able to run it.

  1. From your iOS device, open up the Settings app. From there, go to General | Profiles or Profiles & Device Management. Then, from the menu that pops up, you'll need to say Trust [Developer Name], with [Developer Name] being your Apple ID account. You'll need to verify again that you wish to trust apps created by this account, so go ahead and agree, and all of the steps are done.
  2. With that, exit out of settings by clicking on the home button and then go over to the location where the app was installed and tap on it to run:

With that, we have the game running on the iOS side as well.

Note that when building in the following manner, without the paid license, apps will only work for a limited time, possibly up to a week. If your game crashes immediately and it worked correctly beforehand, this is most likely the culprit. Redeploy to the device again to check whether that is the issue before modifying your actual project.