Flex:如何检测用户是否阻止共享对象写入

时间:2011-08-31 06:41:32

标签: flex actionscript-3 local-shared-object

简单的问题是,如果用户阻止将数据写入共享对象,我如何在actionscript中检测到?

sharedObj = SharedObject.getLocal("rememberme");

这个返回始终是共享对象,但它的大小为0,即使我已阻止共享对象。

当我尝试将数据保存到共享对象并刷新它时,它会引发错误,因为写入被阻止。那么检查写入共享对象是否被禁用是正确的方法是什么?

Error: Error #2130: Unable to flush SharedObject.

1 个答案:

答案 0 :(得分:10)

var my_so:SharedObject = SharedObject.getLocal("mySpace");
var flushStatus:String = null;
try {
    flushStatus = my_so.flush();
} catch (error:Error) {
    trace("Error...Could not write SharedObject to disk\n");
}
if (flushStatus != null) {
    switch (flushStatus) {
        case SharedObjectFlushStatus.PENDING :
            trace("Requesting permission to save object...\n");
            my_so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
            break;
        case SharedObjectFlushStatus.FLUSHED :
            trace("Value flushed to disk.\n");
            break;
    }
}
function onFlushStatus(event:NetStatusEvent):void {
    trace("User closed permission dialog...\n");
    switch (event.info.code) {
        case "SharedObject.Flush.Success" :
            trace("User granted permission -- value saved.\n");
            break;
        case "SharedObject.Flush.Failed" :
            trace("User denied permission -- value not saved.\n");
            break;
    }
my_so.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
}

如果共享对象被阻止,你可以catch错误报告,如果为0则转到SharedObjectFlushStatus.PENDING

SOURCE