flash cookie 主要利用了ShareObject类实现。flash cookie的好处是用户不易删除并且可以写入大量数据,但缺点是如果用户屏蔽掉了flash那就over了。
主类主要给外部js提供了方法。
用途:可以将swf设置成1x1像素,隐藏掉。利用js写入。
cookie类
代码如下:
package com.tools {
  import  flash.net.SharedObject;
  //
  public class Cookie {
    private var _$so:SharedObject;
    ///
    function Cookie() {
      init();
    }
    private function init():void {
    }
    //--------------------------------------------//
    public function setCookie(key:String, val:String, name:String = "sina_cookie"):void {
      //trace("setCookie");
      _$so = SharedObject.getLocal(name);
      //_$so.data["old"] = "26";
      _$so.data[key] = val;
    }
    public function getCookie(key:String, name:String = "sina_cookie"):String {
      _$so = SharedObject.getLocal(name);
      //trace("key:" + key + "--data:" + _$so.data.key)
      if(_$so.size > 0 && _$so.data[key]) {
        return _$so.data[key];
      } else {
        return null;
      }
    }
    public function clearKey(key:String, name:String = "sina_cookie"):void {
      _$so = SharedObject.getLocal(name);
      _$so.data[key]= null;
    }
    public function clearCookie():void {
      _$so.clear();
    }
    public function flush():void {
      _$so.flush();
    }
  }//end class
}
实现类(主类)
package com.sina.testCookie {
  import flash.display.Sprite;
  import flash.external.ExternalInterface;
  import com.tools.Cookie;
  //
  public class MainCookie extends Sprite{
    private var _$co:Cookie;
    //
    function MainCookie() {
      init();
    }
    private function init():void {
      _$co = new Cookie();
      //------test---------
      setCookie("name", "antz");
      //flush();
      //clearCookie();
      //clearKey("name")
      //trace(getCookie("old"))
      addCallBack();
    }
    private function addCallBack():void {
      ExternalInterface.addCallback("setCookie", setCookie);
      ExternalInterface.addCallback("getCookie", getCookie);
      ExternalInterface.addCallback("flush", flush);
      ExternalInterface.addCallback("clearCookie", clearCookie);
      ExternalInterface.addCallback("clearKey", clearKey);
    }
    private function setCookie(key:String, value:String) {
      _$co.setCookie(key, value);
      //return getCookie(key)
    }
    private function getCookie(key:String):String {
      var val = _$co.getCookie(key);
      return val;
    }
    private function flush():void {
      _$co.flush();
    }
    private function clearCookie():void {
      _$co.clearCookie();
    }
    private function clearKey(key:String):void {
      _$co.clearKey(key)
    }
  }//end class;
}