forked from halool/PixFileDownload-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPixFileDownload.m
executable file
·120 lines (92 loc) · 3.61 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
//
// PixFileDownload.m
// FileDownLoadApp
//
// Created by Aaron Saunders on 9/8/10.
// Copyright 2010 clearly innovative llc. All rights reserved.
//
#import "PixFileDownload.h"
@implementation PixFileDownload
-(PGPlugin*) 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",nil);
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",nil);
[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",nil);
NSString * sourceUrl = [paramArray objectAtIndex:0];
NSString * fileName = [paramArray objectAtIndex:1];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData* theData = [NSData dataWithContentsOfURL: [NSURL URLWithString:sourceUrl] ];
// 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);
//NSFileManager *fileManager=[NSFileManager defaultManager];
NSError *error=[[[NSError alloc]init] autorelease];
BOOL response = [theData writeToFile:newFilePath options:NSDataWritingFileProtectionNone error:&error];
if ( response == NO ) {
NSLog(@"file save result %@", [error description]);
// send our results back to the main thread
[self performSelectorOnMainThread:@selector(downloadCompleteWithError:)
withObject:[error description] waitUntilDone:YES];
} else {
NSLog(@"No Error, file saved successfully", nil);
// 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",nil);
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",nil);
NSString * jsCallBack = [NSString stringWithFormat:@"pixFileDownloadCompleteWithError('%@');",errorStr];
[self writeJavascript: jsCallBack];
}
- (void)dealloc
{
if (params) {
[params release];
}
[super dealloc];
}
@end