题
我正在用仪表盘上写一个仪表板小部件,我想添加某种校验更高的功能。我已经在研究Sparkle,但是Afaict不适用于这样的小部件。是否有一个常用的库可以进行更新检查,还是我必须开发自己的系统?
我只需要一个非常简单的设置...自动检查新版本将是一个加号,但是如果用户必须单击一个按钮才能检查我可以。
解决方案
因为“是否有一个函数会...”,那么我就没有遇到过它。
我所做的如下
在PLIST中,有小部件的版本,您将数字放入其中,可以说1.0。您应该能够访问和使用。 (请参阅代码)由于我没有的原因,并添加了此全局var widget_version =“ 1.4”;然后在更新窗口小部件时对其进行更新。
然后,在网络上可以访问的服务器上,您可以创建一个具有窗口小部件当前版本数量的PHP(或其他)文件。再次说1.1。
然后,您编写一个JavaScript函数,而不是将对服务器版本检查此当前窗口版本,并显示图形或消息以告诉用户。最好让用户决定是否要升级而不是使其自动。
以下是我使用的代码。请按照您的意愿复制和 /或黑客攻击。
function getSoftwareUpdate() {
// so use the built in CURL to do a REST call n.b. in widget preference you will need to check 'allow network access'
var softwareUpdate = widget.system("/usr/bin/curl 'http://api.yourserver.com/widget/wfccupdate.php'", null).outputString;
//alert(softwareUpdate); // tells you the function has been called
//alert("the update number from the REST " + softwareUpdate); // for debugging will show the key
// in main.js add this line
// var widget_version = "1.4"; // this is changed when you update the widget code for new release
// yes it's a global variable and bad but i was in a hurry
// the following line should get the widget number but for some reason i didn't do it
// localVersion = widget.preferenceForKey(preferenceForKey);
//alert("the internal preference key " + widget_version);
// then check to see if they match
if(softwareUpdate == widget_version)
{ hide_update('softwareupdate')
}
else
{show_update('softwareupdate')
}
}
function hide_update(el) { // hide the update graphic
if(document.getElementById(el))
{
if(document.getElementById(el).style.display != "none")
document.getElementById(el).style.display = "none";
}
}
function show_update(el) { // show the update graphic
if(document.getElementById(el)) {
if(document.getElementById(el).style.display == "none")
document.getElementById(el).style.display = "block";
}
}
// this is the php that is called by curl and acts as REST
<?php
// data
$UPDATE_database = <<<_UPDATE_
<?xml version="1.0" encoding="utf-8" ?>
<update>
<widgetversion>1.1</widgetversion>
</update>
_UPDATE_;
// load data
$xml = simplexml_load_string($UPDATE_database);
$result = $xml->xpath("widgetversion");
print $result[0];
?>
希望这可以帮助
不隶属于 StackOverflow