Search code examples
j2objc

How can I access Android string resources from ObjC


I am looking for a function in ObjC to get strings from Android string resources.

I have res/values/strings.xml (Android project):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="something">Something</string>
<resources>

which can be accessed on Android using

getContext().getString(R.string.something)

I need a tool to convert Android resources to something which can be used with ObjC and a function in ObjC which takes an integer resource id and returns the string.


Solution

  • There isn't any such function, since Android resources are tightly bound to the Android application model, which is huge (android/content/Context.java has almost 4800 lines, for example) and very different from the iOS application model.

    However, string resources are easily extracted and converted to Java resource files, which are supported by j2objc. A simple XSLT script, for example, could be used to correctly convert <string name="something">Something</string> into something = Something (for a properties file) or Object[][] resources = { \"something\", \"Something\" }; for a ListResourceBundle.