TideSDK How to save a cookie's information to be accessed in different file?

别来无恙 提交于 2020-01-16 18:42:10

问题


I am trying to use TideSDK's Ti.Network to set the name and value of my cookie. But how do I get this cookie's value from my other pages?

            var httpcli;
            httpcli = Ti.Network.createHTTPCookie();
            httpcli.setName(cname); //cname is my cookie name
            httpcli.setValue(cvalue);  //cvalue is the value that I am going to give my cookie
            alert("COOKIE value is: "+httpcli.getValue()); 

How would I retrieve this cookie value from my next page? Thank you in advance!


回答1:


ok, there are a lot of ways to create storage content on tidesdk. cookies could be one of them, but not necessary mandatory.

In my personal oppinion, cookies are too limited to store information, so I suggest you to store user information in a JSON File, so you can store from single pieces of information to large structures (depending of the project). Supposing you have a project in which the client have to store the app configuration like 'preferred path' to store files or saving strings (such first name, last name) you can use Ti.FileSystem to store and read such information.:

in the following example, I use jQuery to read a stored json string in a file:

File Contents (conf.json):

{
    "fname" : "erick",
    "lname" : "rodriguez",
    "customFolder" : "c:\\myApp\\userConfig\\"
}

Note : For some reason, Tidesdk cannot parse a json structure like because it interprets conf.json as a textfile, so the parsing will work if you remove all the tabs and spaces:

{"fname":"erick","lname":"rodriguez","customFolder":"c:\\myApp\\userConfig\\"}

now let's read it.... (myappfolder is the path of your storage folder)

readfi = Ti.Filesystem.getFile(myappfolder,"conf.json");
Stream = Ti.Filesystem.getFileStream(readfi);
Stream.open(Ti.Filesystem.MODE_READ);
contents = Stream.read();
contents = JSON.parse(contents.toString);
console.log(contents);

now let's store it....

function saveFile(pathToFile) {
    var readfi,Stream,contents;
    readfi = Ti.Filesystem.getFile(pathToFile);
    Stream = Ti.Filesystem.getFileStream(readfi);
    Stream.open(Ti.Filesystem.MODE_READ);
    contents = Stream.read();
    return contents.toString();
};

//if a JSON var is defined into js, there is no problem
var jsonObject = {
   "fname":"joe",
   "lname":"doe",
   "customFolder" : "c:\\joe\\folder\\"
}

var file = pathToMyAppStorage + "\\" + "conf.json";
var saved = writeTextFile(file,JSON.stringify(jsonObject));
console.log(saved);


来源:https://stackoverflow.com/questions/26043957/tidesdk-how-to-save-a-cookies-information-to-be-accessed-in-different-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!