Converting Obj-C code to {N}


#1

I have posted a question on Github, and got some help from NordlingArt, which has been great so far!
I posting the same question in here to see if somebody in here can contribute, because I’m approaching my deadline very fast.
Link to github question: https://github.com/NativeScript/NativeScript/issues/4011

###This is the original question:

This it not an issue but a request for help :slight_smile:

How would you rewrite this Obj-C code to {N} ?

-(NSString*)getUserFullName;{
    NSString * kConfigurationKey = @"com.apple.configuration.managed";
    NSDictionary *mdmConfig = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kConfigurationKey];
    NSString *temp = mdmConfig[@"userFullName"];
   
    if (temp && [temp isKindOfClass:[NSString class]])
    {
        return temp;
    } else {
        return @"";
    }
   
}

My first version is:

try {
	let kConfigurationKey = "com.apple.configuration.managed";
	let defs = NSUserDefaults.standardUserDefaults();
	let mdmConfig = defs.dictionaryForKey(kConfigurationKey);
    	return mdmConfig["userName"];
}catch(err) {
    	console.dir(err)
}

I can’t test this more than twice, so that’s why I ask for help, instead of just debugging my way through it :slight_smile:

Based of NordlingArts input, the current code looks like this:

let defs = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
let kConfigurationKey = "com.apple.configuration.managed";
let mdmConfig = defs.dictionaryForKey(kConfigurationKey);
userInfo["userName"] = mdmConfig["userName"];

The error from the current code is:
CFBundleShortVersionString : TypeError: null is not an object (evaluating 'mdmConfig["userName"]')


Thanks in advance for any help given :slight_smile:


#2

It seems like there is simply no com.apple.configuration.managed key in the userdefaults. You can dump and inspect them by running:

let defs = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
console.log(defs.dictionaryRepresentation());

#3

Thanks for your input, @Eddy - Sorry for not giving you all the facts.

If you distribute your app from your own hosted AirWatch AppStore, then it will upon installation append the com.apple.configuration.managed and contain all information about the user which is downloading the app.

I will diffently try the console.log you wrote, but if we imagine that the com.apple.configuration.managed exists, is my code correct, then? :slight_smile:


#4

I think that last line:

userInfo["userName"] = mdmConfig["userName"];

Should read:

userInfo["userName"] = mdmConfig.objectForKey("userName");

#5

@Eddy I have tried you console.log:

Here is my code:

function getUserInfo() {
  return new Promise(function(resolve, reject) {
    try {
      	let defs = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
		let kConfigurationKey = "com.apple.configuration.managed";
		let mdmConfig = defs.dictionaryForKey(kConfigurationKey);
		console.log(defs.dictionaryRepresentation());
		console.dir(appSettings.getString("AppleKeyboards"));
		console.dir(mdmConfig.objectForKey("AppleKeyboards"));
		userInfo["initials3"] = mdmConfig.objectForKey("userName");

    } catch (ex) {
      console.log("Error in reading userinfo: " + ex);
      reject(ex);
    }
  });
};

Result from console.log:

{
	    AddingEmojiKeybordHandled = 1;
	    AppleITunesStoreItemKinds =     (
	        "itunes-u",
	        movie,
	        album,
	        ringtone,
	        "software-update",
	        booklet,
	        tone,
	        "music-video",
	        song,
	        podcast,
	        software,
	        audiobook,
	        "podcast-episode",
	        wemix,
	        eBook,
	        mix,
	        artist,
	        document
	    );
	    AppleKeyboards =     (
	        "en_US@hw=Automatic;sw=QWERTY",
	        "emoji@sw=Emoji",
	        "da_DK@hw=Automatic;sw=QWERTY-Danish"
	    );
	    AppleKeyboardsExpanded = 1;
	    AppleLanguages =     (
	        en
	    );
	    AppleLocale = "en_US";
	    ApplePasscodeKeyboards =     (
	        "en_US@hw=Automatic;sw=QWERTY",
	        "emoji@sw=Emoji",
	        "da_DK@hw=Automatic;sw=QWERTY-Danish"
	    );
	    NSInterfaceStyle = macintosh;
	    NSLanguages =     (
	        en
	    );
	    PKKeychainVersionKey = 3;
	    stringKey = "String value";
	}

For fun, I just tried to

console.dir(appSettings.getString("AppleKeyboards"));

But that gives me “null”.
I would assume that the com.apple.configuration.managed is an array like Keyboards.

In regards to the rest of the code, the line:
console.dir(mdmConfig.objectForKey(“AppleKeyboards”));
outputs:

Error in reading userinfo3: TypeError: null is not an object (evaluating 'mdmConfig.objectForKey')

That should have been working on the AppleKeyboards, right?


#6

To answer your last question: No, but this does:

      const key = "AppleKeyboards";
      let defs = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
      let nsDictionary = defs.objectForKey(key);
      console.log(nsDictionary);

Btw, I’m not seeing com.apple.configuration.managed in your dump…


#7

This is great stuff! :smiley:

How would you extract a single value from the keyboard obj?

(
	"en_US@hw=Automatic;sw=QWERTY",
	"emoji@sw=Emoji",
	"da_DK@hw=Automatic;sw=QWERTY-Danish"
)

?

Again, thanks for all you help, @Eddy


#8

That object is a native array of strings, so one could do:

      let keyboards: NSArray<NSString> = defs.objectForKey(key);
      console.log(`Nr of keyboards: ${keyboards.count}`);

      for (let i = 0; i < keyboards.count; i++) {
        let keyboard = keyboards.objectAtIndex(i);
        console.log("keyboard: " + keyboard);
      }

#9

THANKS @Eddy

I converted it to JS:

const key = "AppleKeyboards";
let defs = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
var keyboards = defs.objectForKey(key);
console.log("Nr of keyboards: " + keyboards.count);
for (var i = 0; i < keyboards.count; i++) {
	var keyboard = keyboards.objectAtIndex(i);
	console.log("keyboard: " + keyboard);
}

And its working!! :smiley:

Thank you! I’m ready to test this from AirWatch!