科尔多瓦 3.0 插件 plist 配置
Posted
技术标签:
【中文标题】科尔多瓦 3.0 插件 plist 配置【英文标题】:cordova 3.0 plugin plist config 【发布时间】:2013-09-04 15:31:59 【问题描述】:plugin.xml的一部分
<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="MyPlugin">
<param name="ios-package" value="MyPlugin"/>
</feature>
</config-file>
<!--this need to be added to the .plist file-->
<config-file target="*-Info.plist" parent="UIBackgroundModes">
<array>
<string>location</string>
</array>
</config-file>
<header-file src="src/ios/MyPlugin.h" />
<source-file src="src/ios/MyPlugin.m" />
</platform>
左侧是安装我的插件之前,右侧是之后:
如您之前所见:
<key>NSMainNibFile</key>
<string></string>
<key>NSMainNibFile~ipad</key>
<string></string>
之后
<key>NSMainNibFile</key>
<string>
</string>
<key>NSMainNibFile~ipad</key>
<string>
</string>
差别太大了! 如果我删除那些我不知道它们来自哪里的空格,那么我在启动后就不会崩溃!
ios 6 模拟器输出(但在设备上也一样)
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/myusername/Library/Application Support/iPhone Simulator/6.0/Applications/F4FDE3C4-D7A8-440F-866D-D0DECD79E2F5/My.app> (loaded)' with name '
''
*** First throw call stack:
(0xea012 0x2848e7e 0xe9deb 0x540fac 0x54298d 0x324ceb 0x325002 0x323ed6 0x335315 0x33624b 0x327cf8 0x367adf9 0x367aad0 0x5fbf5 0x5f962 0x90bb6 0x8ff44 0x8fe1b 0x3237da 0x32565c 0x1fe3c 0x1fd9d)
libc++abi.dylib: terminate called throwing an exception
(lldb)
我认为这是 Cordova / Phonegap 中的一个错误,但这并不能让我的老板高兴。如何解决这个问题? 每次从命令行启动时都会重新生成 .plist,因此无法手动编辑。
找不到文档,只有 this,如果我只写了 1 次,我不知道为什么会添加 4 次我的位置。
编辑: 从命令行安装我的插件(但不编译或运行)后,plist 如下所示:
<key>NSMainNibFile</key>
<string>
</string>
<key>NSMainNibFile~ipad</key>
<string>
</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
$ phonegap build ios
<key>NSMainNibFile</key>
<string>
</string>
<key>NSMainNibFile~ipad</key>
<string>
</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>location</string>
</array>
-观察:有一行添加了位置!
$ phonegap run ios
[phonegap] detecting iOS SDK environment...
[phonegap] using the local environment
[phonegap] compiling iOS...
[phonegap] successfully compiled iOS app
[phonegap] trying to install app onto device
[phonegap] no device was found
[phonegap] trying to install app onto emulator
[phonegap] successfully installed onto emulator
plist 将被清除 2 次:那些将被清除 2 次,最后将再次添加。 现在 plist 看起来像这样:
<key>NSMainNibFile</key>
<string>
</string>
<key>NSMainNibFile~ipad</key>
<string>
</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>location</string>
<string>location</string>
<string>location</string>
<string>location</string>
</array>
编辑2:
cordova prepare
RANDOMLY 清除 <string></string>
空格并始终将 <string>location</string>
添加到 UIBackgroundModes 数组!
【问题讨论】:
issues.apache.org/jira/browse/CB-4731 【参考方案1】:是的,这似乎是 Cordova 处理 plist 文件的插件配置设置中的一个错误。
重复的数组条目很烦人,但不应破坏构建或影响应用程序。但是,添加到 NSMainNibFile* 设置的空格确实会导致 XCode 无法构建,并出现您看到的 NSInternalInconsistencyException
错误消息。
在解决此问题之前,我正在使用以下钩子脚本解决它 - 放置在 .cordova/hooks/after_platform_add/patch_plist.sh
:
#!/bin/bash
if pushd platforms/ios 2>/dev/null ; then # iOS-specific actions...
# Patch *-Info.plist
PROJNAME=$(echo *.xcodeproj|sed -e 's/\..*//')
sed -i '' '/<key>NSMainNibFile<\/key>/,/<\/string>/d' $PROJNAME/*-Info.plist
sed -i '' '/<key>NSMainNibFile~ipad<\/key>/,/<\/string>/d' $PROJNAME/*-Info.plist
popd
fi
这会从 plist 中完全删除这些设置,因为它们不是必需的。删除它们可防止 Cordova 在 prepare
之后将它们添加回损坏状态。
脚本需要可执行:
chmod a+x .cordova/hooks/after_platform_add/patch_plist.sh
这应该在每个platform add
命令之后运行,当 plist 生成时 - 所以你需要在之后运行以下命令来重新生成它并应用补丁:
cordova platform rm ios -d
cordova platform add ios -d
【讨论】:
有没有办法将patch_plist.sh 放在我的插件文件夹中并以这种方式分发,并在安装时将patch_plist.sh 复制到.cordova/hooks/after_platform_add 文件夹? @matheszabi 不幸的是,我还没有找到这样做的方法。我想在 Cordova 修复此问题之前需要手动执行步骤。【参考方案2】:这不是一个好的解决方案,但我发现在 plugman 中修复之前可以防止重复的方法如下:
<!-- clobber old array value in .plist -->
<config-file target="*-Info.plist" parent="UIBackgroundModes">
<string>CLOBBER</string>
</config-file>
<!-- replace clobbered value with proper array in .plist -->
<config-file target="*-Info.plist" parent="UIBackgroundModes">
<array>
<string>location</string>
</array>
</config-file>
即,用一维节点破坏节点,然后用适当的数组重新破坏它。
【讨论】:
【参考方案3】:只需将这些行添加到 config.xml
<platform name="ios">
<config-file platform="ios" target="*-Info.plist" parent="NSMainNibFile">
<string></string>
</config-file>
</platform>
<platform name="ios">
<config-file platform="ios" target="*-Info.plist" parent="NSMainNibFile~ipad">
<string></string>
</config-file>
</platform>
【讨论】:
以上是关于科尔多瓦 3.0 插件 plist 配置的主要内容,如果未能解决你的问题,请参考以下文章