Adding Mobile
Our photo gallery app won’t be complete until it runs on iOS, Android, and the web - all using one codebase. All it takes is some small logic changes to support mobile platforms, installing some native tooling, then running the app on a device. Let’s go!
#
Import Platform APILet’s start with making some small code changes - then our app will “just work” when we deploy it to a device.
Import the Ionic Platform API into photo.service.ts
, which is used to retrieve information about the current device. In this case, it’s useful for selecting which code to execute based on the platform the app is running on (web or mobile):
#
Platform-specific LogicFirst, we’ll update the photo saving functionality to support mobile. In the readAsBase64()
function, check which platform the app is running on. If it’s “hybrid” (Capacitor or Cordova, two native runtimes), then read the photo file into base64 format using the Filesystem readFile()
method. Otherwise, use the same logic as before when running the app on the web:
Next, update the savePicture()
method. When running on mobile, set filepath
to the result of the writeFile()
operation - savedFile.uri
. When setting the webviewPath
, use the special Capacitor.convertFileSrc()
method (details here).
Next, head back over to the loadSaved()
function we implemented for the web earlier. On mobile, we can directly set the source of an image tag - <img src="x" />
- to each photo file on the Filesystem, displaying them automatically. Thus, only the web requires reading each image from the Filesystem into base64 format. Update this function to add an if statement around the Filesystem code:
Our Photo Gallery now consists of one codebase that runs on the web, Android, and iOS. Next up, the part you’ve been waiting for - deploying the app to a device.