Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
thanhtrand
Posts: 9
Registered: ‎09-27-2012
My Device: Curve 9220

Webworks PhoneGap write file error (OS 7 + OS 6)

Hi,

 

I'm developing an Webworks PhoneGap application that needs to write text to .txt file for caching purpose. I got error code = 1 when tried to write text to .txt file(not on SD card). I'm using BlackBerry Curve 9220 OS 7. Here's my code:

 

// function write data to file, file name is specified url
var file_name = null;
var data_to_write = null;
function writeToFile(url, data){
	//alert('write');
	//alert(data);
	file_name = url.replace(/\//g, '').replace(/:/g,'');
	//alert(file_name);
	data_to_write = JSON.stringify(data);
	//alert(data_to_write);
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, failWrite);
}

 function gotFileSystem(fileSystem){
	// alert(fileSystem);
	fileSystem.root.getFile(file_name, {create: true}, getFileToWriteSuccess, failWrite);
}

function getFileToWriteSuccess(fileEntry){
	
    //alert('fileEntry : '+fileEntry);
	fileEntry.createWriter(gotFileWriter, failWrite);
}

function gotFileWriter(writer){
	//alert(data_to_write);
	writer.write(data_to_write);
	//writer.close();
}

function failWrite(evt){
	alert(evt.target.error.code);
	$.mobile.loading('hide');
}

 Here's my config.xml:

 

<feature id="blackberry.system" required="true" version="1.0.0.0" />
  <feature id="org.apache.cordova" required="true" version="1.0.0" />
  <feature id="blackberry.find" required="true" version="1.0.0.0" />
  <feature id="blackberry.identity" required="true" version="1.0.0.0" />
  <feature id="blackberry.pim.Address" required="true" version="1.0.0.0" />
  <feature id="blackberry.pim.Contact" required="true" version="1.0.0.0" />
  <feature id="blackberry.app" required="true" version="1.0.0.0" />
  <feature id="blackberry.app.event" required="true" version="1.0.0.0" />
  <feature id="blackberry.system.event" required="true" version="1.0.0.0"/>
  <feature id="blackberry.widgetcache" required="true" version="1.0.0.0"/>
  <feature id="blackberry.media.camera" />
  <feature id="blackberry.ui.dialog" />
  <feature id="blackberry.media.microphone" required="true" version="1.0.0.0"/>
  <feature id="blackberry.invoke" />
  <feature id="blackberry.invoke.MessageArguments" />
  <feature id="blackberry.message" />
  <feature id="blackberry.message.sms" />
  <feature id="blackberry.system.event" version="1.0.0"/>
  <feature id="blackberry.push" required="true" version="1.0.0.0"/>
   <feature id="blackberry.io.file" required="true" version="1.0.0.0" />
	<feature id="blackberry.utils"   required="true" version="1.0.0.0" />
	<feature id="blackberry.io.dir"  required="true" version="1.0.0.0" />
	<rim:permissions>
		<rim:permit>access_shared</rim:permit>
	</rim:permissions>

  
  <!-- Cordova API -->
  <access subdomains="true" uri="file:///store/home" />
  <access subdomains="true" uri="file:///SDCard" />
...

P/S: My app runs well on BlackBerry Storm 9520 OS 5.

Please give advise. Thank in advance!

BlackBerry Development Advisor
anzor_b
Posts: 165
Registered: ‎09-24-2012
My Device: Passport

Re: Webworks PhoneGap write file error (OS 7 + OS 6)

Hello thanhtrand,

 

In BBOS 6 and 7, we introduced WebKit, and therefore the syntax slightly differs. Please see the fixed code below:

	
function writeToFile(url, data){
	//alert('write');
	//alert(data);
	file_name = url.replace(/\//g, '').replace(/:/g,'');
	//alert(file_name);
	data_to_write = JSON.stringify(data);
	//alert(data_to_write);

        //add webkit before requestFileSystem and filesystem is now in window.PERSISTENT
	window.webkitRequestFileSystem(window.PERSISTENT, 0, gotFileSystem, failWrite);
}

function gotFileWriter(writer){
	//alert(data_to_write);

	console.log(data_to_write);

	//writer accepts a Blob, so we create one and pass in your string
        var blob = new Blob([data_to_write], {type: 'text/plain'});

	writer.write(blob);
	//writer.close();
}

 The rest of your code is correct, so implement the code above, test and report back.

New Contributor
thanhtrand
Posts: 9
Registered: ‎09-27-2012
My Device: Curve 9220

Re: Webworks PhoneGap write file error (OS 7 + OS 6)

[ Edited ]

Hi anzor_b,

 

Sorry for my late response. Thanks for your help but I've applied your code, the app still cannot write file successfully. Maybe I need more time for WebKit to use it. I'll give it a try soon.

 

Fortunately, my app worked properly after I had inserted SD card to device.

 

P/S: My app doesn't work while device is plugged into computer via cable. Unplug device and app works well.

 

Thanks again,

 

thanhtrand

BlackBerry Development Advisor
anzor_b
Posts: 165
Registered: ‎09-24-2012
My Device: Passport

Re: Webworks PhoneGap write file error (OS 7 + OS 6)

Hi thanhtrand,

 

Glad to hear your applications works. When you plug the device into the computer, if you do not select "USB Mass Storage" or "USB Drive", the SD Card should be available to the application. Let me know if that works for you.

New Contributor
thanhtrand
Posts: 9
Registered: ‎09-27-2012
My Device: Curve 9220

Re: Webworks PhoneGap write file error (OS 7 + OS 6)

[ Edited ]

Hi anzor_b,

 

I've tried your code again but my app still doesn't work. Thank you anyway.

 

thanks,

 

P/S: I need to go ahead for completing other features of my app. I think I'll learn WebKit after finishing this app.

 

thanhtrand