Android, iOS, and the Camera - Oh My!
Previously, we got an Ionic app up and running locally in a web browser. Now, let’s get it onto your iOS or Android device, then start building the photo gallery feature. Fortunately, Ionic provides a way to skip the frustration of dealing with native SDK installations: Ionic DevApp!
The Ionic DevApp is a free app that makes it easy to run your Ionic app directly on your iOS or Android device. Download it here, then open on your device:
Afterwards, open a terminal and navigate to your Ionic project. Execute the following:
ionic serve -c
In DevApp, you should now see the app appear. If it doesn't, or you have any issues throughout creating this app, see here.
Much better! Now we can add the camera functionality. By the way, you can find reference code for this in the “part 1” folder on GitHub.
Back in about.html
, add the following:
<ion-content>
<img />
<ion-fab center bottom>
<button ion-fab>
<ion-icon name="camera"></ion-icon>
</button>
</ion-fab>
</ion-content>
Save the file and watch - a camera button appears! Tap on it and notice that it doesn’t do anything. Let’s fix that next.
Add the Camera Dependencies via the CLI
In order to use the Camera, we need to bring in its JavaScript and native library dependencies. Back over in your Terminal window, run the following command, which adds the JavaScript library to the project, thus exposing the Camera API in TypeScript code:
npm install --save @awesome-cordova-plugins/camera
In package.json
, you’ll notice a new JavaScript dependency has been added:
"@awesome-cordova-plugins/camera": "^4.12.0"
Next, run this command to add the native iOS and Android code, effectively allowing the Camera to work on a mobile device:
ionic cordova plugin add cordova-plugin-camera
In config.xml
, a new plugin entry is created:
<plugin name="cordova-plugin-camera" spec="^4.0.3" />
The next step is only required for iOS users. As of iOS 10, developers must provide a reason for why the app wishes to access the device camera. Add this to the bottom of config.xml
:
<!-- Required for iOS 10: Camera permission prompt -->
<edit-config file="*-Info.plist" mode="merge" target="NSCameraUsageDescription">
<string>Used to take pictures</string>
</edit-config>
Add Camera plugin to Angular App Module
There’s one more step we need to do since this is an Angular project: register the Camera in the App Module (src/app/app.module.ts
). First, import the Camera module:
import { Camera } from '@awesome-cordova-plugins/camera';
Then, add it as a Provider:
providers: [
StatusBar,
SplashScreen,
Camera,
{provide: ErrorHandler, useClass: IonicErrorHandler}
],
It can now be used on any of our App pages.
Add the Camera to the About page
Our camera button doesn’t do anything yet. Over in about.html
, add a click handler to the button:
<button ion-fab (click)="takePicture()"></button>
Then, update the image placeholder. The following binds the “currentImage” variable (which we’ll work on next) to the image to display to the user.
<img [src]="currentImage" *ngIf="currentImage" />
Open about.ts
next and import the Camera library:
import { Camera, CameraOptions } from '@awesome-cordova-plugins/camera';