Like the Android app you built in the previous step, the iOS app is a simple mobile app that communicates with the backend server.
If you're following the steps in this tutorial on macOS, you can go ahead and build the sample iOS app as described below. If you are on Linux, skip ahead to the next step.
Here, you'll do the following:
BUILD
fileTo build applications for Apple devices, Bazel needs to pull the latest Apple build rules from its GitHub repository. To enable this, add the following to your WORKSPACE
file:
git_repository( name = "build_bazel_rules_apple", remote = "https://github.com/bazelbuild/rules_apple.git", tag = "0.0.1", )
Let‘s take a look at the source files for the app. These are located in $WORKSPACE/ios-app/UrlGet
. Again, you’re just looking at these files now to become familiar with the structure of the app. You don't have to edit any of the source files to complete this tutorial.
At a command-line prompt, open your new BUILD
file for editing:
vi $WORKSPACE/ios-app/BUILD
To build iOS targets, Bazel needs to load build rules from its GitHub repository whenever the build runs. To make these rules available to your project, add the following load statement to the beginning of your BUILD
file:
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
Bazel provides several build rules that you can use to build an app for the iOS platform. For this tutorial, you‘ll first use the objc_library
rule to tell Bazel how to build a static library from the app source code and Xib files. Then you’ll use the ios_application
rule to tell it how to build the application binary and the .ipa
bundle.
NOTE: This tutorial presents a minimal use case of the Objective-C rules in Bazel. For example, you have to use the ios_application
rule to build multi-architecture iOS apps.
Add the following to your BUILD
file:
objc_library( name = "UrlGetClasses", srcs = [ "UrlGet/AppDelegate.m", "UrlGet/UrlGetViewController.m", "UrlGet/main.m", ], hdrs = glob(["UrlGet/*.h"]), xibs = ["UrlGet/UrlGetViewController.xib"], )
Note the name of the rule, UrlGetClasses
.
The ios_application
rule builds the application binary and creates the .ipa
bundle file.
Add the following to your BUILD
file:
ios_application( name = "ios-app", bundle_id = "Google.UrlGet", families = [ "iphone", "ipad", ], infoplists = [":UrlGet/UrlGet-Info.plist"], visibility = ["//visibility:public"], deps = [":UrlGetClasses"], )
Note how the deps
attribute references the output of the UrlGetClasses
rule you added to the BUILD
file above.
Now, save and close the file. You can compare your BUILD
file to the completed example in the master
branch of the GitHub repo.
Make sure that your current working directory is inside your Bazel workspace:
cd $WORKSPACE
Now, enter the following to build the sample app:
bazel build //ios-app:ios-app
Bazel launches and builds the sample app. During the build process, its output will appear similar to the following:
INFO: Found 1 target... Target //ios-app:ios-app up-to-date: bazel-bin/ios-app/ios-app.ipa INFO: Elapsed time: 0.565s, Critical Path: 0.44s
The .ipa
file and other outputs are located in the $WORKSPACE/bazel-bin/ios-app
directory.
You can now run the app from Xcode using the iOS Simulator. First, generate an Xcode project using Tulsi. Then, open the project in Xcode, choose an iOS Simulator as the runtime scheme, and click Run.
Note: If you modify any project files in Xcode (for example, if you add or remove a file, or add or change a dependency), you must rebuild the app using Bazel, re-generate the Xcode project in Tulsi, and then re-open the project in Xcode.
To build your app so that it installs and launches on an iOS device, Bazel needs the appropriate provisioning profile for that device model. Do the following:
Go to your Apple Developer Account and download the appropriate provisioning profile for your device. See Apple's documentation for more information.
Move your profile into $WORKSPACE
.
(Optional) Add your profile to your .gitignore
file.
Add the following line to the ios_application
target in your BUILD
file:
provisioning_profile = "<your_profile_name>.mobileprovision",
NOTE: Ensure the profile is correct so that the app can be installed on a device.
Now build the app for your device:
bazel build //ios-app:ios-app --ios_multi_cpus=armv7,arm64
This builds the app as a fat binary. To build for a specific device architecture, designate it in the build options.
To build for a specific Xcode version, use the --xcode_version
option. To build for a specific SDK version, use the --ios_sdk_version
option. The --xcode_version
option is sufficient in most scenarios.
To specify a minimum required iOS version, add the minimum_os_version
parameter to the ios_application
build rule in your BUILD
file.
You can also use Tulsi to build your app using a GUI rather than the command line.
The easiest way to install the app on the device is to launch Xcode and use the Windows > Devices
command. Select your plugged-in device from the list on the left, then add the app by clicking on the “plus” sign under installed apps and selecting the .ipa
that you built.
If your app fails to install on your device, ensure that you are specifying the correct provisioning profile in your BUILD
file (step 4 in the previous section).
If your app fails to launch, make sure that your device is part of your provisioning profile. The View Device Logs
button on the Devices
screen in Xcode may provide other information as to what has gone wrong.
The next step is to build a backend server for the two mobile apps you built in this tutorial.