Skip to main content
Version: 7.0.0

Application Permission Management

Why permissions are required ?

Users must grant permission for an app to access sensitive information, including the current location, calendar, contact information, reminders and photos. Although people appreciate the convenience of using an app that has access to this information, they also expect to have control over their privacy.

Implementation Requirement

Permission management requires OS-specific development since permission structures are totally separate in iOS and Android.

IOS

Smartface iOS Framework handles permissions automatically for the user. Developer does not need to handle permission management.

TypeScript
import Location from "@smartface/native/device/location";

Location.start(Location.Android.Priority.HIGH_ACCURACY, 1000);
Location.onLocationChanged = (event: {
latitude: number;
longitude: number;
}): void => {
console.log(
"Location latitude: " + event.latitude + " Longitude: " + event.longitude
);
};

In addition, there are also some convenient functions that check the status of related permission. Location permission status example like below;

TypeScript
import Location from "@smartface/native/device/location";

//@ts-ignore
switch (Location.ios.getAuthorizationStatus()) {
//@ts-ignore
case Location.ios.authorizationStatus.Authorized:
/*code block*/
break;
//@ts-ignore
case Location.ios.authorizationStatus.Denied:
/*code block*/
break;
//@ts-ignore
case Location.ios.authorizationStatus.NotDetermined:
/*code block*/
break;
default:
/*code block*/
}

Android

In the JavaScript side, there are some methods which are necessary for managing permissions.

TypeScript
import Application from "@smartface/native/application";

const CAMERA_PERMISSION_CODE: number = 1002;
const CAMERA_PERMISSION = Application.Android.Permissions.CAMERA;

function getPermission(permission: string, code: number): void {
let prevPermissionRationale =
Application.Android.shouldShowRequestPermissionRationale(permission);
Application.android.onRequestPermissionsResult = function (e) {
if (e.requestCode != code) return;

let currentPermissionRationale =
Application.Android.shouldShowRequestPermissionRationale(permission);
if (e.result) {
console.log("GRANTED");
} else if (!currentPermissionRationale) {
if (prevPermissionRationale) {
console.info("NEVER ASK AGAIN");
} else {
console.info("COULD NOT ASK");
}
} else {
console.log("DENIED");
}
};

if (Application.android.checkPermission(permission)) {
console.log("ALREADY GRANTED");
} else {
Application.android.requestPermissions(code, permission);
}
}

PermissionUtil Extension

Used for permission operations. It works for android and ios. To install the extension into your workspace, visit sf-extension-utils page.

PermissionUtil.getPermission returns true always on IOS devices.

Permission Request Result

PermissionUtil.getPermission takes two arguments. First argument is a permission, second argument is a callback when triggers on permission result. To handle permission request result, pass the second parameter as a function.

TypeScript
import PermissionUtil from "@smartface/extension-utils/lib/permission";
import Application from "@smartface/native/application";

PermissionUtil.getPermission(
Application.Android.Permissions.READ_CONTACTS,
(result) => {
alert("READ_CONTACTS permission result: " + result);
}
);

module.exports = pagePermission;
Permission Prompts

The permission prompt dialogs cannot be customized or localized. They are fully managed by the operating system for security purposes.

Publishing Binaries from Smartface IDE

For required permission operations, you must add permissions to config/AndroidManifest.xml on Smartface IDE.

Which permission you should use with Smartface Native Framework?

Some features requires specific permission both iOS and Android. For example, when you want to access user's location, you need Location permission. In this section, you can find which Smartface Native Framework feature requires which permission.

FeatureAndroid ManifestiOS Info.plistAndroid Run-time
Application.byteReceivedREAD_PHONE_STATEREAD_PHONE_STATE
Application.byteSentREAD_PHONE_STATEREAD_PHONE_STATE
Application.checkUpdateWRITE_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE
Device.Contacts.addWRITE_CONTACTS

NSContactsUsageDescription
${PRODUCT_NAME} Contact fetch

WRITE_CONTACTS
Device.Contacts.getAllREAD_CONTACTS

NSContactsUsageDescription
${PRODUCT_NAME} Contact fetch

READ_CONTACTS
Device.Hardware.IMEIREAD_PHONE_STATEREAD_PHONE_STATE
Device.Hardware.UIDREAD_PHONE_STATEREAD_PHONE_STATE
Device.Location.start() 1ACCESS_COARSE_LOCATION || ACCESS_FINE_LOCATION

NSLocationWhenInUseUsageDescription
${PRODUCT_NAME} WhenInUse Location

ACCESS_COARSE_LOCATION || ACCESS_FINE_LOCATION
Device.Multimedia.getAllGalleryItems()READ_EXTERNAL_STORAGE

NSPhotoLibraryUsageDescription
${PRODUCT_NAME} PhotoLibrary Usage

NSCameraUsageDescription
${PRODUCT_NAME} Camera Usage

READ_EXTERNAL_STORAGE
Device.System.fingerPrintAvailableUSE_FINGERPRINT-
IO.File 2READ_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE
Net.Http 3ACCESS_NETWORK_STATE

NSAppTransportSecurity
NSAllowsArbitraryLoads

-
SpeechRecognizer.startRECORD_AUDIO

NSSpeechRecognitionUsageDescription
${PRODUCT_NAME} Speech Recognition Usage

NSMicrophoneUsageDescription
${PRODUCT_NAME} Microphone Usage

RECORD_AUDIO
Device.Multimedia.startCameraCAMERA

NSCameraUsageDescription
$(PRODUCT_NAME) shoots contact picture

CAMERA
sf-extension-smsreceiver

RECEIVE_SMS
READ_SMS

RECEIVE_SMS
READ_SMS

Application.callCALL_PHONECALL_PHONE
Device.System.VibrateVIBRATEVIBRATE
UI.MapViewMAPS_RECEIVEMAPS_RECEIVE

1 For Android, based on Provider; you can use ACCESS_COARSE_LOCATION for Location.Android.Provider.NETWORK, ACCESS_FINE_LOCATION for Location.Android.Provider.GPS and Location.Android.Provider.AUTO
2 For Android, all methods and properties under IO.File requires READ_EXTERNAL_STORAGE permission if the file is not under Path.DataDirectory.
3 For Android, all methods under Net.Http requires ACCESS_NETWORK_STATE permission.