You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<title>Adding Mobile Support with React | Ionic Capacitor Camera</title>
8
+
<meta
9
+
name="description"
10
+
content="Learn how to add mobile support to your Ionic Capacitor photo gallery app, enabling it to run on iOS, Android, and the web using one codebase."
11
+
/>
12
+
</head>
13
+
5
14
# Adding Mobile
6
15
7
16
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!
8
17
18
+
## Import Platform API
19
+
9
20
Let’s start with making some small code changes - then our app will “just work” when we deploy it to a device.
10
21
11
-
## Platform-specific Logic
22
+
Import the Ionic [Platform API](../platform.md) into `usePhotoGallery.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).
23
+
24
+
Add `isPlatform` to the imports at the top of the file to use the `isPlatform` method. `Capacitor` is also imported to help with file paths on mobile devices.
12
25
13
-
First, we’ll update the photo saving functionality to support mobile. In the `savePicture` function, check which platform the app is running on. If it’s “hybrid” (Capacitor or Cordova, the two native runtimes), then read the photo file into base64 format using the `readFile` method. Also, return the complete file path to the photo using the Filesystem API. When setting the `webviewPath`, use the special `Capacitor.convertFileSrc` method ([details here](https://ionicframework.com/docs/core-concepts/webview#file-protocol)). Otherwise, use the same logic as before when running the app on the web.
First, we’ll update the photo saving functionality to support mobile. In the `savePicture` method, check which platform the app is running on. If it’s “hybrid” (Capacitor, the native runtime), then read the photo file into base64 format using the `Filesystem`'s' `readFile()` method. Otherwise, use the same logic as before when running the app on the web.
// Use webPath to display the new image instead of base64 since it's
77
+
// already loaded into memory
78
+
return {
79
+
filepath: fileName,
80
+
webviewPath: photo.webPath,
81
+
};
82
+
}
83
+
};
84
+
```
85
+
86
+
Next, add a new bit of logic in the `loadSaved()` method. On mobile, we can directly point to each photo file on the Filesystem and display them automatically. On the web, however, we must read each image from the Filesystem into base64 format. This is because the Filesystem API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Update the `loadSaved()` method:
// Use webPath to display the new image instead of base64 since it's
47
-
// already loaded into memory
48
-
return {
49
-
filepath: fileName,
50
-
webviewPath: photo.webPath,
51
-
};
52
-
}
53
-
};
106
+
setPhotos(photosInPreferences);
107
+
};
108
+
```
54
109
55
-
// Same old code from before.
56
-
}
110
+
Our Photo Gallery now consists of one codebase that runs on the web, Android, and iOS.
57
111
58
-
// Same old code from before.
59
-
```
112
+
`usePhotoGallery.ts` should now look like this:
60
113
61
-
Next, add a new bit of logic in the `loadSaved` function. On mobile, we can directly point to each photo file on the Filesystem and display them automatically. On the web, however, we must read each image from the Filesystem into base64 format. This is because the Filesystem API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Update the `loadSaved` function inside of `useEffect` to:
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.
149
+
const addNewToGallery =async () => {
150
+
// Take a photo
151
+
const capturedPhoto =awaitCamera.getPhoto({
152
+
resultType: CameraResultType.Uri,
153
+
source: CameraSource.Camera,
154
+
quality: 100,
155
+
});
96
156
97
-
`usePhotoGallery.ts` should now look like this:
157
+
const fileName =Date.now() +'.jpeg';
158
+
// Save the picture and add it to photo collection
0 commit comments