[转]as3的cookies访问

[url]http://www.imemment.com/post/86.html[/url]



FLASH 有2种 cookies
一种是传统浏览器中的cookies
一种是AS3 自带的
下面就把基于以上两种的Cookies类发布一下 很有用

浏览器COOKIES


package
{
import flash.external.ExternalInterface;

public class CookieJar
{
private static const FUNCTION_SETCOOKIE:String =
"document.insertScript = function ()" +
"{ " +
"if (document.snw_setCookie==null)" +
"{" +
"snw_setCookie = function (name, value, days)" +
"{" +
"if (days) {"+
"var date = new Date();"+
"date.setTime(date.getTime()+(days*24*60*60*1000));"+
"var expires = '; expires='+date.toGMTString();"+
"}" +
"else var expires = '';"+
"document.cookie = name+'='+value+expires+'; path=/';" +
"}" +
"}" +
"}";

private static const FUNCTION_GETCOOKIE:String =
"document.insertScript = function ()" +
"{ " +
"if (document.snw_getCookie==null)" +
"{" +
"snw_getCookie = function (name)" +
"{" +
"var nameEQ = name + '=';"+
"var ca = document.cookie.split(';');"+
"for(var i=0;i < ca.length;i++) {"+
"var c = ca[i];"+
"while (c.charAt(0)==' ') c = c.substring(1,c.length);"+
"if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);"+
"}"+
"return null;" +
"}" +
"}" +
"}";
private static var INITIALIZED:Boolean = false;

public static function init():void{
ExternalInterface.call(FUNCTION_GETCOOKIE);
ExternalInterface.call(FUNCTION_SETCOOKIE);
INITIALIZED = true;
}

public static function setCookie(name:String, value:Object, days:int):void{
if(!INITIALIZED)
init();

ExternalInterface.call("snw_setCookie", name, value, days);
}

public static function getCookie(name:String):Object{
if(!INITIALIZED)
init();

return ExternalInterface.call("snw_getCookie", name);
}

public static function deleteCookie(name:String):void{
if(!INITIALIZED)
init();

ExternalInterface.call("snw_setCookie", name, "", -1);
}

}
}

AS3自带的COOKIES 好像占用空间不超过1M


package emment.cookies {

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
import flash.ui.Keyboard;

public class FlashCookie {

private var mCookieName : String = '_eat_more_cookies';
private var _SharedObject : SharedObject;

// SharedObject version num - useful if we need to change formats in future versions
// upping this number will reset the shared object in all clients
private var _version : Number = 1.0;

private static var instance:FlashCookie;
private static var allowInstantiation:Boolean;
private var stage:Stage;

public function FlashCookie ():void {
if (!allowInstantiation) {
throw new Error("Error: Instantiation failed: Use FlashCookie.getInstance() instead of new.");
}
}

public static function getInstance():FlashCookie {
if (instance == null) {
allowInstantiation = true;
instance = new FlashCookie ();
allowInstantiation = false;

FlashCookie.getInstance().init();
}
return instance;
}


/**
* Initialize cookie
*/
private function init():void
{
//trace( "FlashCookie.init" );

_SharedObject = SharedObject.getLocal(mCookieName);
// make sure we are not dealing with an older version of our flash cookie
// if old version or not defined, set it up
if(_SharedObject.data.version == undefined || _SharedObject.data.version < _version) {
reset();
}

}

// secret shortcut key
public function CONTROL_ALT_SHIFT (inStage:Stage)
{
trace( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
trace( "+ FlashCookie.CONTROL_ALT_SHIFT > inStage : " + inStage );
trace( "+ CONTROL/APPLE + ALT + SHIFT :: ARROW-LEFT >> clear cookie" );
trace( "+ CONTROL/APPLE + ALT + SHIFT :: ARROW-RIGHT >> set cookie" );
trace( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

inStage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}


private function reportKeyDown(e:KeyboardEvent):void
{
//trace( "FlashCookie.reportKeyDown > e : " + e );

if ((e.keyCode == Keyboard.LEFT) && (e.altKey == true) && (e.shiftKey == true) && (e.ctrlKey == true)) {
trace ('+ FlashCookie :: CLEAR sharedObject');
reset();
//traceData();
}
if ((e.keyCode == Keyboard.RIGHT) && (e.altKey == true) && (e.shiftKey == true) && (e.ctrlKey == true)) {
trace ('+ FlashCookie :: SET sharedObject');
FlashCookie.getInstance().saveValue(true, 'firstTime');
traceData();
}


}


public function saveValue (inValue:* , inNameValue:String = 'savedValue'):void
{
// trace( "FlashCookie.saveValue > inNameValue : " + inNameValue + ", inValue : " + inValue );

_SharedObject.data[inNameValue] = inValue;

var flushStatus:String = null;
try {
flushStatus = _SharedObject.flush(10000);
} catch (error:Error) {
trace ("Error...Could not write SharedObject to disk");
}
if (flushStatus != null) {
switch (flushStatus) {
case SharedObjectFlushStatus.PENDING:
trace("Requesting permission to save object...");
_SharedObject.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
trace("Value flushed to disk.");
break;
}
}
}

/**
* Reset cookie var
*
* Override this and add necessary default values here
*/
public function reset()
{
//trace( "FlashCookie.reset" );
_SharedObject.clear();
_SharedObject.data.version = _version;
}

/**
* Flush the cookies to disk
* // writes changes to disk
*/
public function flush()
{
return _SharedObject.flush(10000);
}

/**
* return cookie data
*
* @return
*/
public function get data () : Object {
return _SharedObject.data;
}


public function traceData ():void
{
trace( " - FlashCookie.traceData // start" );
for ( var i:String in _SharedObject.data ) {
trace( "\tkey : " + i + ", value : " + _SharedObject.data[ i ] );
}
trace( " - FlashCookie.traceData // end" );
}

public function getSize ():void
{
trace( "FlashCookie.getSize: " + _SharedObject.size);
}

Listeners

private function onFlushStatus(event:NetStatusEvent):void {
trace ("User closed permission dialog...");
switch (event.info.code) {
case "SharedObject.Flush.Success":
trace ("User granted permission -- value saved.");
break;
case "SharedObject.Flush.Failed":
trace ("User denied permission -- value not saved.");
break;
}
_SharedObject.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
}


} // end class

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值