13 App Policies

As a mobile app developer, you can use the App Policies API to create read-only custom properties in a mobile backend and access them in your application with REST calls.

What Are App Policies and What Can I Do With Them?

App policies are custom properties that you can define and adjust in a mobile backend and then reference from your apps through a simple REST call. Once you have defined an app policy, you can update its value anytime, even after you have published the mobile backend. This lets you make changes to the appearance and behavior of a deployed app without having to update the app itself.

Here are some of the things that you might use app policies for:

  • Determining when a given feature is enabled in the app. For example, an app for a retailer might have a feature to display a section for holiday sales that should only be displayed when there is a current sale.

  • Fonts, colors, names of images to use, and other things that are typically stored as part of an app’s configuration.

  • Timeout values for network calls. Having an app policy for this can allow your mobile cloud administrator to tune app responsiveness based on prevailing network performance.

Setting an App Policy

  1. Click icon to open the side menu to open the side menu and select Mobile Apps > Backends.
  2. Open the backend. (Select it and click Open.)
  3. Click the App Policies tab.
  4. Click New Policy, fill in the property name, type, value, and description, and then click Create.
The new app policy appears in a table on the page.
You can later use the Edit and Delete buttons in the table to edit the policy or remove it entirely. After the mobile backend has been published, you can still change a policy’s value, but you can not add, delete, or rename policies or change the policy type.

Note:

You can only set app policies and change their values from within the OMCe user interface. You can’t do this programmatically from app code.

Android

Retrieving App Policies

You can retrieve information on the app policies associated with a mobile backend using the REST API or any of the client SDKs. The REST API enables you to retrieve an array of all of the policies for the mobile backend. The SDKs also enable you to retrieve information on specific policies.

To fetch app policies for your Android apps for the first time, you use the MobileBackend object’s getAppConfig() method to return all app policies as a JSONObject:

JSONObject appPolicies = oracle.cloud.mobile.mobilebackend.MobileManager
                         .getManager().getMobileBackend().getAppConfig();

Once you have fetched the app policies, you can query the app config for the values of individual properties.

To return the value of a specific app policy of type String, where myPolicyName is the name of the policy and “No policy configured” is the string returned if myPolicyName doesn’t exist:

String myPolicyValue = oracle.cloud.mobile.mobilebackend.MobileManager.getManager()
                       .getMobileBackend().getAppConfig().getString(myPolicyName, "No policy configured");

To load a new app policy asynchronously and make a network call:

mobileBackend.loadAppConfig(new AppConfigCallback() {
    @Override
    public void onResult(McsError error, AppConfig config) {

To return the value of a specific app policy of type string where test_string is the name of the policy if exists and returns the value of null if test string doesn’t exist:

    String testString = config.getString("test_string", null);

To return the value of a specific app policy of type Integer, where test_int is the name of the policy and 0 is the value returned if Test_int doesn’t exist:

     int testInt = config.getInt("test_int", 0);

To return the value of a specific app policy of type Boolean, where test_bool is the name of the policy and false is the value returned if test_bool doesn’t exist:

Boolean testBool = config.getBoolean("test_bool", false);

To return the value of a specific app policy of type Double, where test_double is the name of the policy and 0.0 is the value returned if test_double doesn’t exist:

 double testDouble = config.getDouble("test_double", 0.0);

To return the value of a specific app policy of type Number, where test_number is the name of the policy and 0.0 is the double value returned if test_number doesn’t exist. Works for both double and single integer values. Returns exact value with which it is initialized:

Number testNumber = config.getNumber("test_number", 0.0);

To return a local copy of the app policy, and returns an empty app policy object if the app policy doesn't exist:

AppConfig config = mobileBackend.getAppConfig();

iOS

Retrieving App Policies

You can retrieve information on the app policies associated with a mobile backend using the REST API or any of the client SDKs. The REST API enables you to retrieve an array of all of the policies for the mobile backend. The SDKs also enable you to retrieve information on specific policies.

To fetch app policies for your iOS apps for the first time, you use an ansynchronous callback. Here’s some code that will fetch the app config from the mobile backend and loop until the network call returns with either the app config or an error:

OMCMobileBackend* mbe = [[OMCMobileManager sharedManager] mobileBackend];

__block OMCAppConfig* appConfig = nil;
__block NSError* error = nil;
__block BOOL executing = YES;
[_mbe appConfigWithCompletionHandler:^(OMCAppConfig* appConfig_, NSError* error_) {
    appConfig = appConfig_;
    error = error_;
    executing = NO;
}];

while (executing) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]]];
}

if (error != nil) {
    return;
}

Once you have fetched the app policies, you can query the app config for the values of individual properties. You can also insert an optional parameter to return a value if the policy is not found.

NSString* welcome = [appConfig stringForProperty:@"welcome" default:@"bogus"];
int timeout = [appConfig integerForProperty:@"TIMEOUT" default:42];
boolean enabled = [appConfig booleanForProperty:@"enableLocation" default:NO];

REST

Retrieving App Policies

You can retrieve information on the app policies associated with a mobile backend using the REST API or any of the client SDKs. The REST API enables you to retrieve an array of all of the policies for the mobile backend. The SDKs also enable you to retrieve information on specific policies.

Using the following call, you can retrieve all of the app policies associated with a mobile backend.

GET {BaseURL}/mobile/platform/appconfig/client

The response body is a JSON object containing all of the app policies configured for that mobile backend. For example, if the mobile backend contains fifTechReqTimeout, fifTechWelcomeMsg, and fifTechBgImage policies, the response might look something like this:

{
    "fifTechReqTimeout":100000,
    "fifTechWelcomeMsg":"Hello",
    "fifTechBgImage":"/mobile/platform/storage/collections/appObjects/objects/bgImage42"
}

From there, you can process them in your app code.

Cordova

Retrieving App Policies

You can retrieve information on the app policies associated with a mobile backend using the REST API or any of the client SDKs. The REST API enables you to retrieve an array of all of the policies for the mobile backend. The SDKs also enable you to retrieve information on specific policies.

To fetch app policies for your Cordova apps, call loadAppConfig() on your mobile backend object, e.g.
mcs.mobileBackend.loadAppConfig(success, error);

JavaScript

Retrieving App Policies

You can retrieve information on the app policies associated with a mobile backend using the REST API or any of the client SDKs. The REST API enables you to retrieve an array of all of the policies for the mobile backend. The SDKs also enable you to retrieve information on specific policies.

To fetch app policies , call loadAppConfig() on your mobile backend object:
mcs.mobileBackend.loadAppConfig(success, error);

Updating an App Policy Value in a Published Mobile Backend

Even after a mobile backend has been published, you can still change the value of an app policy. However, you can not change its name or type.
  1. Click icon to open the side menu to open the side menu and select Mobile Apps > Backends.
  2. Open the mobile backend. (Select it and click Open.)
  3. Click the App Policies tab.
  4. In the table of app policies, select the policy and click Edit.
  5. Edit the value and click Save.