19 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. Make sure you're in the environment where you want to set the app policy.
  2. Click icon to open the side menu to open the side menu and select Applications > Mobile Backends.
  3. Open the backend. (Select it and click Open.)
  4. Click the App Policies tab.
  5. 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 MCS user interface. You can’t do this programmatically from app code.

Retrieving App Policies in App Code

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.

REST

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.

Android SDK

To fetch app policies 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
                         .MobileBackendManager.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.MobileBackendManager
                       .getMobileBackend().getAppConfig().getString(myPolicyName, "No policy configured");

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

Boolean myPolicyValue = oracle.cloud.mobile.mobilebackend.MobileBackendManager
                        .getMobileBackend().getAppConfig().getBoolean(myPolicyName, false);

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

Integer myPolicyValue = oracle.cloud.mobile.mobilebackend.MobileBackendManager
                        .getMobileBackend().getAppConfig().getInt(myPolicyName, 0);

iOS SDK

To fetch app policies 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 = [[OMCMobileBackendManager sharedManager] defaultMobileBackend];

__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];

Cordova SDK and JavaScript SDK

To fetch app policies, call loadAppConfig() on your mobile backend object, e.g.

backend = mcs.MobileBackendManager.getMobileBackend("JSBackend");
...
backend.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. Make sure you're in the environment where you want to update the app policy.
  2. Click icon to open the side menu to open the side menu and select Applications > Mobile Backends.
  3. Open the mobile backend. (Select it and click Open.)
  4. Click the App Policies tab.
  5. In the table of app policies, select the policy and click Edit.
  6. Edit the value and click Save.