FILE I/O with iPhone using the branch of Jesse MacFadyen

149 views
Skip to first unread message

stevben

unread,
Jan 18, 2010, 6:02:02 PM1/18/10
to phonegap, al...@dourfestival.be
Hi guys,

First, I would like to apologize but my english is not perfect... I'll
do my best.

I need to store data for an iPhone Application and I have an
interesting result.

I already tried to use localStorage from HTML5 and Lawnchair created
by Brian LeRoux.
Both work as excepted (thx for that great work Brian LeRoux !) but i
was stuck with the 5MB WebKit Limit :'(

In fact, in this application, I would like to store :
- A twitter feed with at least 30 tweets and images.
- A Flicke Gallery feed, and images of course.
- A series of biography with images, three languages.
- Some user's preferences.

All data are converted to the type String :
- Images are converted to string using the canvasToUrl method which
comes with the CANVAS element from HTML5.
- JSON are converted to string using the JSON in JavaScript library :
http://www.json.org/js.html

Then I looked at the fork of Jese MacFadyen who has implemented
functions concerning the FILE API for phonegap.

You can find this excellent work at :
http://github.com/purplecabbage/phonegap-iphone/blob/master/PhoneGapLib/javascripts/core/file.js

Unfortunately, I got some problems for writing and reading file using
implemented functions.
The two functions were :
- (int) writeToFile:(NSString*)fileName withData:(NSString*)data
append:(BOOL)shouldAppend;
- (void) readFile:(NSMutableArray*)arguments withDict:
(NSMutableDictionary*)options;

The problem was that the DATA were always TRUNCATED even if the amount
of data was not high (less than 25Ko).

I don't know Objective-C but I decided to look at some tutorials for
FILE I/O in Objective-C and tried to use (naively) two functions
proposed by the API :
- writeToFile (not the writeToFile from PhoneGap of course ;))
- initWithContentsOfFile

I don't know if i'm lucky but i solved all my problems. I don't have
tested it using huge amount of data but it works like a charm
actually.
Here the two modified functions. I Hope it will be useful.

Note :
1. I don't use the shouldAppend parameter at this time....
2. writeToFile (from phonegap) returns now a BOOLEAN instead of an
INTEGER.

- (BOOL) writeToFile:(NSString*)fileName withData:(NSString*)data
append:(BOOL)shouldAppend
{
NSString *appFile = [ [ self appDocsPath ]
stringByAppendingPathComponent:fileName];
return [data writeToFile:appFile atomically:YES];

}

- (void) readFile:(NSMutableArray*)arguments withDict:
(NSMutableDictionary*)options
{

NSString* argPath = [arguments objectAtIndex:0];
// send back a load start event
NSString * jsCallBack = [NSString
stringWithFormat:@"navigator.fileMgr.reader_onloadstart(\"%@
\");",argPath];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
// TODO: possibly add user permissions, prompt the user to allow
access
// if(!userHasAllowed && ! [ self promptUser ])
// {
//
// }
NSString *appFile = [ [ self appDocsPath ]
stringByAppendingPathComponent:argPath];
NSString *str = [[NSString alloc] initWithContentsOfFile:appFile];
// write back the result
jsCallBack = [NSString
stringWithFormat:@"navigator.fileMgr.reader_onload(\"%@\",\"%@
\");",argPath,[ str
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}

Here is my code for testing :

navigator.fileMgr = new FileMgr();
var file = navigator.fileMgr;
/* WRITE FILE */
var fileWriter = new FileWriter();
fileWriter.fileName = "file.txt";
navigator.fileMgr.addFileWriter("file.txt",fileWriter);
fileWriter.readyState = 0; // EMPTY
fileWriter.result = null;
fileWriter.oncomplete = function (result) {
/* READ FILE */
alert("result" + result);
var fileReader = new FileReader();
fileReader.onload = function(content) {
var data = JSON.parse(content);
$.each(data, function(i,obj){
alert(obj.text);
});
}
fileReader.fileName = "file.txt";
navigator.fileMgr.addFileReader("file.txt",fileReader);
PhoneGap.exec("File.readFile","file.txt");
}

$.getJSON("http://twitter.com/status/user_timeline/
Dourfestival.json?count=30&callback=?",
function(data){
var dataSTR = JSON.stringify(data);
PhoneGap.exec("File.write","file.txt",dataSTR,false);
}
);

Thank for you great job, again.

Benjamin Stévens

Jesse MacFadyen

unread,
Jan 18, 2010, 7:28:10 PM1/18/10
to stevben, phonegap, al...@dourfestival.be
Thanks Ben
I will revisit / retest my check-in and patch if needed.
Cheers
Jesse


Sent from my iPhone

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Reply all
Reply to author
Forward
0 new messages