setlocale works in iOS Simulator but fails on device

I'm attempting to make use of a component written in C++ in my iPad application that uses setlocale() from the standard C library. When running via the simulator (either i386 or x86_64), I'm able to set a locale successfully and get an expected return value:


(lldb) p (const char *)setlocale(2, "ja_JP")

(const char *) $1 = 0x000000010e776bd0 "ja_JP"


However, when running on a device (on either iOS8 or iOS9, on both armv7 and arm64 devices), this call always seems to fail and return null.


(lldb) p (const char *)setlocale(2, "ja_JP")

(const char *) $0 = 0x00000000


Actually setting the region and language on the iOS device doens't seem to make a difference either.


Is this an example of something like iOS9 preventing the use of sysctl(), or am I missing something bigger here?

Accepted Reply

The problem here is not

setlocale
per se, but rather in reading the locale in the first place. Notably,
newlocale
also returns NULL when you call it with similar parameters.

This fails because no C-style locales are available to you on iOS.

On the simulator this works because the locale code picks up the locale data from OS X (in

/usr/share/locale
). I’m not sure if this directory is populated on iOS (I suspect not) but that doesn’t matter because the sandbox won’t let your process get to it.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The problem here is not

setlocale
per se, but rather in reading the locale in the first place. Notably,
newlocale
also returns NULL when you call it with similar parameters.

This fails because no C-style locales are available to you on iOS.

On the simulator this works because the locale code picks up the locale data from OS X (in

/usr/share/locale
). I’m not sure if this directory is populated on iOS (I suspect not) but that doesn’t matter because the sandbox won’t let your process get to it.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

OK, that makes sense, thanks for pointing that out.