簡體   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