forked from halool/PixFileDownload-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PixFileDownload.m
executable file
·125 lines (95 loc) · 3.74 KB
/
PixFileDownload.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// PixFileDownload.m
// FileDownLoadApp
//
// Original created by Aaron Saunders on 9/8/10.
// Copyright 2010 clearly innovative llc. All rights reserved.
//
// Modified by Johnathan Iannotti on 04/20/2012
// Twitter: @notticode
#import "PixFileDownload.h"
@implementation PixFileDownload
-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
self = (PixFileDownload*)[super initWithWebView:theWebView];
return self;
}
//
// entry point to the javascript plugin for PhoneGap
//
-(void) downloadFile:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options {
NSLog(@"in PixFileDownload.downloadFile");
NSString * sourceUrl = [paramArray objectAtIndex:0];
NSString * fileName = [paramArray objectAtIndex:1];
//NSString * completionCallback = [paramArray objectAtIndex:2];
params = [[NSMutableArray alloc] initWithCapacity:2];
[params addObject:sourceUrl];
[params addObject:fileName];
[self downloadFileFromUrl:params];
}
//
// call to excute the download in a background thread
//
-(void) downloadFileFromUrl:(NSMutableArray*)paramArray
{
NSLog(@"in PixFileDownload.downloadFileFromUrl");
[self performSelectorInBackground:@selector(downloadFileFromUrlInBackgroundTask:) withObject:paramArray];
}
//
// downloads the file in the background and saves it to the local documents
// directory for the application
//
-(void) downloadFileFromUrlInBackgroundTask:(NSMutableArray*)paramArray
{
NSLog(@"in PixFileDownload.downloadFileFromUrlInBackgroundTask");
NSString * sourceUrl = [paramArray objectAtIndex:0];
NSString * fileName = [paramArray objectAtIndex:1];
NSString *fullURI = [NSString stringWithFormat:@"%@/%@", sourceUrl, fileName];
NSLog(@"%@",fullURI);
NSError* error=[[[NSError alloc]init] autorelease];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData* theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:fullURI]];
// save file in documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newFilePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat: @"/%@", fileName]];
//NSString *newFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:fileName];
NSLog(@"Writing file to path %@", newFilePath);
BOOL response = [theData writeToFile:newFilePath options:NSDataWritingFileProtectionNone error:&error];
if ( response == NO ) {
NSLog(@"file save result %@", [error localizedDescription]);
// send our results back to the main thread
[self performSelectorOnMainThread:@selector(downloadCompleteWithError:)
withObject:[error localizedDescription] waitUntilDone:YES];
} else {
NSLog(@"No Error, file saved successfully");
// send our results back to the main thread
[self performSelectorOnMainThread:@selector(downloadComplete:)
withObject:newFilePath waitUntilDone:YES];
}
[pool drain];
}
//
// calls the predefined callback in the ui to indicate completion
//
-(void) downloadComplete:(NSString *)filePath {
NSLog(@"in PixFileDownload.downloadComplete");
NSString * jsCallBack = [NSString stringWithFormat:@"pixFileDownloadComplete('%@');",filePath];
[self writeJavascript: jsCallBack];
}
//
// calls the predefined callback in the ui to indicate completion with error
//
-(void) downloadCompleteWithError:(NSString *)errorStr {
NSLog(@"in PixFileDownload.downloadCompleteWithError");
NSString * jsCallBack = [NSString stringWithFormat:@"pixFileDownloadCompleteWithError('%@');",errorStr];
[self writeJavascript: jsCallBack];
}
- (void)dealloc
{
if (params) {
[params release];
}
[super dealloc];
}
@end