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
Copy file name to clipboardExpand all lines: docs/angular/your-first-app/5-adding-mobile.md
+62-68Lines changed: 62 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
---
2
+
title: Adding Mobile
2
3
strip_number_prefixes: false
3
4
---
4
5
@@ -10,11 +11,11 @@ Our photo gallery app won’t be complete until it runs on iOS, Android, and the
10
11
11
12
Let’s start with making some small code changes - then our app will “just work” when we deploy it to a device.
12
13
13
-
Import the Ionic [Platform API](https://ionicframework.com/docs/angular/platform) 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).
14
+
Import the Ionic [Platform API](../platform.md) 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).
14
15
15
-
Add `Platform` to the imports at the top of the file and a new property `platform` to the `PhotoService` class. We'll also need to update the constructor to set the user's platform:
16
+
Add `Platform` to the imports at the top of the file and a new property `platform` to the `PhotoService` class. We'll also need to update the constructor to set the user's platform.
@@ -24,6 +25,7 @@ import { Platform } from '@ionic/angular';
24
25
25
26
exportclassPhotoService {
26
27
public photos:UserPhoto[] = [];
28
+
27
29
private PHOTO_STORAGE:string='photos';
28
30
29
31
// CHANGE: Add a property to track the app's running platform.
@@ -34,18 +36,19 @@ export class PhotoService {
34
36
this.platform=platform;
35
37
}
36
38
37
-
//other code
39
+
//Same old code from before.
38
40
}
39
41
```
40
42
41
43
## Platform-specific Logic
42
44
43
-
First, 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.
45
+
First, 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`'s'`readFile()` method. Otherwise, use the same logic as before when running the app on the web.
44
46
45
47
Update `readAsBase64()` to look like the following:
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 on the File Protocol](https://ionicframework.com/docs/core-concepts/webview#file-protocol)). To use this method, we'll need to import Capacitor into `photo.service.ts`.
70
+
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 on the File Protocol](../../core-concepts/webview.md#file-protocol)). To use this method, we'll need to import Capacitor into `photo.service.ts`.
69
71
70
72
```tsx
71
73
import { Capacitor } from'@capacitor/core';
72
74
```
73
75
74
76
Then update `savePicture()` to look like the following:
75
77
76
-
```tsx
77
-
// Save picture to file on device
78
-
privateasyncsavePicture(photo: Photo) {
79
-
// Convert photo to base64 format, required by Filesystem API to save
80
-
const base64Data =awaitthis.readAsBase64(photo);
81
-
82
-
// Write the file to the data directory
83
-
const fileName =Date.now() +'.jpeg';
84
-
const savedFile =awaitFilesystem.writeFile({
85
-
path: fileName,
86
-
data: base64Data,
87
-
directory: Directory.Data
88
-
});
78
+
```ts
79
+
// CHANGE: Update `loadSaved` method.
80
+
publicasyncloadSaved() {
81
+
// Retrieve cached photo array data
82
+
const { value } =awaitPreferences.get({ key: this.PHOTO_STORAGE });
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:
103
+
Next, head back over to the `loadSaved()`method 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 method to add an _if statement_ around the `Filesystem` code:
110
104
111
-
```tsx
105
+
```ts
106
+
// CHANGE: Update `loadSaved` method.
112
107
publicasyncloadSaved() {
113
108
// Retrieve cached photo array data
114
109
const { value } =awaitPreferences.get({ key: this.PHOTO_STORAGE });
@@ -136,7 +131,7 @@ Our Photo Gallery now consists of one codebase that runs on the web, Android, an
0 commit comments