Tuesday, May 24, 2016

iOS module for Managed Configuration through MDM server

1. Create a new iOS module which loads the dictionary (key-value) from MDM server
2. Use this module in your app code (Ex: Appcelerator)

// The Managed app configuration dictionary pushed down from an MDM server are stored in this key.
"com.apple.configuration.managed";

1. Create the iOS module

######################
/**
 * appconfig
 *
 * Created by Your Name
 * Copyright (c) 2016 Your Company. All rights reserved.
 */

#import "ComExampleMobileIosModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"

@implementation ComExampleMobileIosModule

#pragma mark Internal

// this is generated for your module, please do not change it
-(id)moduleGUID
{
return @"a505984f-5a41-4e79-9971-ff861e988301";
}

// this is generated for your module, please do not change it
-(NSString*)moduleId
{
return @"com.example.mobile.ios";
}

#pragma mark Lifecycle

-(void)startup
{
// this method is called when the module is first loaded
// you *must* call the superclass
[super startup];

NSLog(@"[INFO] %@ loaded",self);
}

-(void)shutdown:(id)sender
{
// this method is called when the module is being unloaded
// typically this is during shutdown. make sure you don't do too
// much processing here or the app will be quit forceably

// you *must* call the superclass
[super shutdown:sender];
}

#pragma mark Cleanup

-(void)dealloc
{
// release any resources that have been retained by the module
[super dealloc];
}

#pragma mark Internal Memory Management

-(void)didReceiveMemoryWarning:(NSNotification*)notification
{
// optionally release any resources that can be dynamically
// reloaded once memory is available - such as caches
[super didReceiveMemoryWarning:notification];
}

#pragma mark Listener Notifications

-(void)_listenerAdded:(NSString *)type count:(int)count
{
if (count == 1 && [type isEqualToString:@"my_event"])
{
// the first (of potentially many) listener is being added
// for event named 'my_event'
}
}

-(void)_listenerRemoved:(NSString *)type count:(int)count
{
if (count == 0 && [type isEqualToString:@"my_event"])
{
// the last listener called for event named 'my_event' has
// been removed, we can optionally clean up any resources
// since no body is listening at this point for that event
}
}

// The Managed app configuration dictionary pushed down from an MDM server are stored in this key.
static NSString * const kConfigurationKey = @"com.apple.configuration.managed";

// This sample application allows for a server url and cloud document switch to be configured via MDM
// Application developers should document feedback dictionary keys, including data types and valid value ranges.
static NSString * const kConfigurationServerURLKey = @"serverURL";


#pragma Public APIs

-(id)example:(id)args
{
// example method
return @"hello world";
}

-(id)exampleProp
{
// example property getter
return @"hello world";
}

-(void)setExampleProp:(id)value
{
// example property setter
}

// this function returns the value for the key
-(NSString*)getProperty:(id)propKey
{
    ENSURE_SINGLE_ARG(propKey, NSString);

    NSDictionary *serverConfig = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kConfigurationKey];
    NSString *propValue = serverConfig[propKey];
    
    NSLog(@"%s%@", "propkey >>>>>>>>>> : ", propValue);
   
    return propValue;
}

@end

#######################

2. 
Import this module in the app (Ex: Appcelerator code below)

var config_props = require('com.example.mobile.ios');

var serverEndpoint config_props.getProperty("server.endpoint);