繁体   English   中英

Cordova 插件变量

[英]Cordova plugin variables

如何在我的 Cordova 插件中使用变量,用于 iOS 和 Android?

我的意思是在 config.xml 中定义的变量,现在位于 package.json 中,看起来像这样:

<plugin name="cordova-plugin-device" spec="^1.1.0">
    <variable name="MY_VARIABLE" value="my_variable_value" />
</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或其他本机文件,然后您可以像在 iOS/Android 上一样从本机代码中读取这些文件。

在从Info.plist读取的示例中,您可以这样做:

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

在 Info.plist 中写入

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

从字符串中读取strings.xml

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

写入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