Salesforce

Using Native OS Code in Mobile Applications (Magic xpa 3.x)

« Go Back

Information

 
Created ByKnowledge Migration User
Approval Process StatusPublished
Objective
Description

Using Native OS Code in Mobile Applications (Magic xpa 3.x)

Calling native code

Sometimes, you need to use native OS code in your application; for example, when using custom hardware or when you need to access information from the device, such as the contacts list.

You can do this in your application by evaluating the ClientNativeCodeExecute function.

Since version: 2.5

See also: The Mobile Native Code section in the Connectivity and Misc. Samples

Raising a user event from the native code

Sometimes, you need to raise an event from the native OS code that will be handled in your Magic application.

You can raise a user event from your code by writing:

Android:

  1. Add the declaration: import com.magicsoftware.core.CoreApplication;

  2. Raise the event: CoreApplication.getInstance().invokeUserEvent(event_name,param1,param2); where event_name is the user event name and param1 and param2 are the values that will be passed to the user event handler.

iOS:

  1. Add the declaration: #import "Magicxpa.h"

  2. Add an array that will hold all of the parameters' values, ending with a nil value: NSArray *params = [NSArray arrayWithObjects:param1, param2, nil]; where param1 and param2 are the values that will be passed to the user event handler.

  3. Raise the event: [Magicxpa invokeUserEvent:event_name Params:params]; where event_name is the user event name.

Windows 10 Mobile:

Raise the event: com.magicsoftware.richclient.CoreMethodInvoker.InvokeUserEvent(event_name, param1, param2); where event_name is the user event name and param1 and param2 are the values that will be passed to the user event handler.


Note:

  • An unlimited number of parameters can be used.

  • The parameters must be of string type.

Since version: 2.5

See also: The Rich Client Samples project (program RNC08)

Using native controls

Sometimes, you need to add a native control to the Magic forms. This can be done by attaching the native control to a Magic control using native code. To do this, you first need to get the reference to the Magic control.

You can get the reference to the Magic control from your code as follows:

Android:

  1. Add the declaration: import com.magicsoftware.core.CoreApplication;

  2. Add a variable to store the Magic control reference: View myCustomView = CoreApplication.getInstance().getControlByName(ctrlname,generation);

    where ctrlname is the Magic control name and generation is the task generation (use 0 for the current task).

iOS:

  1. Add the declaration: #import "Magicxpa.h"

  2. Add a variable to store the Magic control reference: UIView *myCustomView = [Magicxpa getControlByName:ctrlname TaskGeneration:generation];

    where ctrlname is the Magic control name and generation is the task generation (use 0 for the current task).

Since version: 2.5

See also: The Rich Client Samples project (program RNC09)

Asking the user to approve a permission (Android)

Sometimes, a user needs to approve specific permissions when running native code, such as for opening a device’s camera or for creating calendar entries.

You can ask the user to give approval by adding the following line to the native code, as in the example below:

boolean requestResult = CoreApplication.getInstance().requestPermission(Manifest.permission.RECORD_AUDIO);

Here, the user is requested to approve the RECORD_AUDIO permission, and the result is stored in the requestResult variable.

Since version: 3.2a

See also: The Rich Client Samples project (program RNC14)

Backward compatibility

Up until version 2.5, you could:

  • Call native code by evaluating the ClientOSEnvGet function with the value of device_udf|my_string.

  • Raising only Magic xpa’s ExternalEvent by writing invokeExternalEvent(“my_string”); for Android devices or [self invokeExternalEvent:@“my_string”]; for iOS devices.

This is still supported but deprecated and no longer recommended.

Reference
Attachment 
Attachment