aflx / pg_downloader_plugin_ios Goto Github PK
View Code? Open in Web Editor NEWDownload files with PhoneGap
Download files with PhoneGap
Hi Alexander,
Thanks for this plugin. This is very useful and should be in the Phonegap core.
I have a few problems getting it to work. Which version of XCode / Phonegap are you using?
I have XCode 4.0.2, Phonegap 0.9.6, Targeting iOS 4.3, and have a problem here:
self.callbackID = [paramArray pop];
Downloader.m:34: warning: 'NSMutableArray' may not respond to '-pop'
Downloader.m: warning: Semantic Issue: Method '-pop' not found (return type defaults to 'id')
Presumably I need to upgrade XCode?
Cheers,
Stewart
Hello Alex,
I´m using your plugin to download some images in parallel to cache them. I've realised that only fires the last image callback. I've put some debug in your code and i've seen that the callbackID is always the same for all the calls, the last image callbackID.
For fix that I made some changes in your code, hope this helps you to fix that problem, I only write the changes:
Downloader.h:
Line 28:
(-) -(void) success:(NSMutableString_)filePath;
(+) -(void) success:(NSMutableArray_)paramArray;
Line 29:
(-) -(void) fail:(NSMutableString_)errorStr;
(+) -(void) fail:(NSMutableArray_)paramArray;
Downloader.m:
Line 34:
(-) self.callbackID = [paramArray pop];
(+) self.callbackID = [paramArray objectAtIndex:0];
Line 47-49:
(-)
NSString * sourceUrl = [paramArray objectAtIndex:0];
NSString * dirName = [paramArray objectAtIndex:1];
NSString * fileName = [paramArray objectAtIndex:2];
(+)
NSString * cid = [paramArray objectAtIndex;0];
NSString * sourceUrl = [paramArray objectAtIndex:1];
NSString * dirName = [paramArray objectAtIndex:2];
NSString * fileName = [paramArray objectAtIndex:3];
Line 66:
(-) [self performSelectorOnMainThread:@selector(fail:) withObject:[error description] waitUntilDone:YES];
(+) [self performSelectorOnMainThread:@selector(fail:) withObject:[NSMutableArray arrayWithObjects:[error description], cid, nil] waitUntilDone:YES];
Line 69:
(-) [self performSelectorOnMainThread:@selector(success:) withObject:filePath waitUntilDone:YES];
(+) [self performSelectorOnMainThread:@selector(success:) withObject:[NSMutableArray arrayWithObjects:filePath, cid, nil] waitUntilDone:YES];
Line 76:
(-) [self performSelectorOnMainThread:@selector(fail:) withObject:[error description] waitUntilDone:YES];
(+) [self performSelectorOnMainThread:@selector(fail:) withObject:[NSMutableArray arrayWithObjects:[error description], cid, nil] waitUntilDone:YES];
Line 85:
(-) -(void) success:(NSMutableString *)filePath {
(+) -(void) success:(NSMutableArray *)paramArray {
Line 87:
(+)
NSString * filePath = [paramArray objectAtIndex:0];
NSString * cid = [paramArray objectAtIndex:1];
Line 90:
(-) [self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
(+) [self writeJavascript: [pluginResult toSuccessCallbackString:cid]];
Line 96:
(-) -(void) fail:(NSMutableString *)errorStr {
(+) -(void) fail:(NSMutableArray *)paramArray {
Line 98:
(+)
NSString * errorStr = [paramArray objectAtIndex:0];
NSString * cid = [paramArray objectAtIndex:1];
Line 101:
(-) [self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
(+) [self writeJavascript: [pluginResult toErrorCallbackString:cid]];
Once again, thanks for your plugin.
Best regards,
Javray
Hello Alex,
I've discovered some bug when you try to download inexistent URL. The problem is in the method dataWithContentsOfURL which return NULL if the URL doesn´t exists.
I think you have to check this before doing nothing.
I've changed your code like this:
if (data == NULL) {
results = [NSArray arrayWithObjects: callbackId, @"404", nil];
[self performSelectorOnMainThread:@selector(downloadFail:) withObject:results waitUntilDone:YES];
}
else {
@try
....
}
Hope this help you.
Hello,
I've started to use your plugin and I get this warning:
Object 0x84be00 of class NSCFString autoreleased with no pool in place - just leaking
The plugin works fine, but that error makes me weak, and I change your code to resolve it, here is the code:
-(void) download:(NSMutableArray*)paramArray {
NSString * sourceUrl = [paramArray objectAtIndex:0];
NSString * dirName = [paramArray objectAtIndex:1];
NSString * fileName = [paramArray objectAtIndex:2];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError *error=[[[NSError alloc]init] autorelease];
@try {
NSString * filePath = [dirName stringByAppendingString:fileName ];
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:sourceUrl] ];
NSLog(@"Write file %@", filePath);
BOOL response = [data writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error];
if ( response == NO ) {
// send our results back to the main thread
NSLog(@"Fail %@", error);
[self performSelectorOnMainThread:@selector(fail:) withObject:[error description] waitUntilDone:YES];
} else {
// jump back to main thread
[self performSelectorOnMainThread:@selector(success:) withObject:filePath waitUntilDone:YES];
}
[pool drain];
}
@catch (id exception) {
NSLog(@"Exception %@", [error description]);
// jump back to main thread
[self performSelectorOnMainThread:@selector(fail:) withObject:[error description] waitUntilDone:YES];
[pool drain];
}
}
I've put inside the try block this three lines:
NSString * filePath = [dirName stringByAppendingString:fileName ];
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:sourceUrl] ];
NSLog(@"Write file %@", filePath);
And the warning's gone.
Hope this help you.
Thanks for the plugin it saves my life.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.