最新ニュースから
前回の掲載で、
データの永続化
今回は、
まずは、
win.addEventListener(
'close',
function(){
var text = textArea.value;
Ti.App.Properties.setString('previousText', text);
}
);
win.addEventListener(
'open',
function(){
var text = Ti.App.Properties.getString('previousText');
if ( text ) {
textArea.value = text;
}
}
);
windowがcloseすると、
ここで利用している、
win.addEventListener(
'close',
function(){
var text = textArea.value;
Ti.App.Properties.setString('previousText', text);
if (mapview.visible) {
var loc = mapview.location;
var locationList =
[loc.latitude,loc.longitude,loc.latitudeDelta,loc.longitudeDelta];
Ti.App.Properties.setList('locationList', locationList);
}
}
);
win.addEventListener(
'open',
function(){
var text = Ti.App.Properties.getString('previousText');
if ( text ) {
textArea.value = text;
}
if ( Ti.App.Properties.hasProperty('locationList') ) {
var locationList = Ti.App.Properties.getList('locationList');
mapview.show();
mapview.setLocation(
{
latitude : locationList[0],
longitude : locationList[1],
latitudeDelta : locationList[2],
longitudeDelta : locationList[3]
}
);
}
}
);
この例では、
ファイルで保存してみる
上の例ではTi.
var fileName = 'tweet.txt';
win.addEventListener(
'close',
function(){
var text = textArea.value;
var loc;
if (mapview.visible) {
loc = mapview.location;
}
var json = JSON.stringify({previousText:text,location:loc});
var file = Ti.Filesystem.getFile(
Titanium.Filesystem.resourcesDirectory + '/' + fileName
);
file.write(json);
}
);
win.addEventListener(
'open',
function(){
var file = Ti.Filesystem.getFile(
Titanium.Filesystem.resourcesDirectory + '/' + fileName
);
var json = file.read();
if ( !json || json.length <= 0) {
return;
}
var data = JSON.parse(json);
var text = data.previousText;
var location = data.location;
if ( text ) {
textArea.value = text;
}
if ( location ) {
var locationList = Ti.App.Properties.getList('locationList');
mapview.show();
mapview.setLocation(location);
}
}
);
ファイル書き込みをする方法で注意してほしい点はファイルハンドラを取得する、
Titanium.
Filesystem. resourcesDirectory + '/' + fileName
を指定しましょう。resourcesDirectoryのほかにも読み書きできるディレクトリのパスがいくつか用意されており、
また、
画像を保存する
ツイートの編集画面には、
なので、
Titanium.Media.saveToPhotoGallery(image)
このコードだけで、
var file = Ti.Filesystem.getFile(
Titanium.Filesystem.resourcesDirectory + '/' + fileName
);
file.wriete(image)
というような形で、