简体   繁体   English

Cordova 插件变量

[英]Cordova plugin variables

How can I use variables in my Cordova plugin, for iOS and Android?如何在我的 Cordova 插件中使用变量,用于 iOS 和 Android?

I mean the variables that were defined in config.xml, and now live in package.json, that looked like this:我的意思是在 config.xml 中定义的变量,现在位于 package.json 中,看起来像这样:

<plugin name="cordova-plugin-device" spec="^1.1.0">
    <variable name="MY_VARIABLE" value="my_variable_value" />
</plugin>

... I know how to set these when installing my custom plugin, but I don't see any mention in the documentation about how to access them within the plugin. ...我知道在安装我的自定义插件时如何设置这些,但我在文档中没有看到任何关于如何在插件中访问它们的提及。

Variables are not accesible from code, variables are just a way of changing a value in the plugin.xml, can be used to make a SDK version configurable, or to write the value to the Info.plist or AndroidManifest.xml or strings.xml or other native files, then you can read those files from the native code as you would do on iOS/Android. Variables are not accesible from code, variables are just a way of changing a value in the plugin.xml, can be used to make a SDK version configurable, or to write the value to the Info.plist or AndroidManifest.xml or strings.xml或其他本机文件,然后您可以像在 iOS/Android 上一样从本机代码中读取这些文件。

In example to read from Info.plist you would do it like this:在从Info.plist读取的示例中,您可以这样做:

NSString *myValue = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MyValue"]];

To write in the Info.plist在 Info.plist 中写入

<config-file target="*-Info.plist" parent="MyValue">
   <string>$MY_VARIABLE</string>
</config-file>

To read from strings.xml从字符串中读取strings.xml

Activity activity = cordova.getActivity();
    String packageName = activity.getPackageName();
    int resId = activity.getResources().getIdentifier(“MyValue”, "string", packageName);
String myValue = activity.getString(resId);

To write to strings.xml写入strings.xml

<config-file target="./res/values/strings.xml" parent="/resources">
    <string name="MyValue">$MY_VARIABLE</string>
</config-file>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM