public
Description: Varies useful libs, classes, and methods for iPhone development
Homepage: http://www.stepcase.com
Clone URL: git://github.com/leonho/iphone-libs.git
iphone-libs / MultipleDownload.m
100644 122 lines (95 sloc) 3.21 kb
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
//
// MultipleDownload.m
//
// Copyright 2008 Stepcase Limited.
//
 
#import "MultipleDownload.h"
 
 
@implementation MultipleDownload
 
@synthesize urls, requests, receivedDatas, finishCount;
 
- init {
    if ((self = [super init])) {
self.finishCount = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
self.receivedDatas = array;
[array release];
 
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
self.requests = dict;
[dict release];
 
    }
    return self;
}
 
- (void)dealloc {
[urls release];
[requests release];
[receivedDatas autorelease];
    [super dealloc];
}
 
- (void)setDelegate:(id)val
{
    delegate = val;
}
 
- (id)delegate
{
    return delegate;
}
 
 
#pragma mark Methods
- (id)initWithUrls:(NSArray *)aUrls {
    if ((self = [self init]))
[self requestWithUrls:aUrls];
return self;
}
 
- (void)requestWithUrls:(NSArray *)aUrls {
 
[receivedDatas removeAllObjects];
[requests removeAllObjects];
[urls autorelease];
urls = [aUrls copy];
 
for(NSInteger i=0; i< [urls count]; i++){
NSMutableData *aData = [[NSMutableData alloc] init];
[receivedDatas addObject: aData];
[aData release];
 
NSURLRequest *request = [[NSURLRequest alloc]
initWithURL: [NSURL URLWithString: [urls objectAtIndex:i]]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10
];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];
 
[requests setObject: [NSNumber numberWithInt: i] forKey: [NSValue valueWithNonretainedObject:connection]];
[connection release];
[request release];
}
}
 
- (NSData *)dataAtIndex:(NSInteger)idx {
return [receivedDatas objectAtIndex:idx];
}
 
- (NSString *)dataAsStringAtIndex:(NSInteger)idx {
return [[[NSString alloc] initWithData:[receivedDatas objectAtIndex:idx] encoding:NSUTF8StringEncoding] autorelease];
}
 
#pragma mark NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue];
    [[receivedDatas objectAtIndex:i] setLength:0];
}
 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue];
    [[receivedDatas objectAtIndex:i] appendData:data];
}
 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue];
finishCount++;
 
if ([delegate respondsToSelector:@selector(didFinishDownload:)])
        [delegate performSelector:@selector(didFinishDownload:) withObject: [NSNumber numberWithInt: i]];
 
if(finishCount >= [urls count]){
if ([delegate respondsToSelector:@selector(didFinishAllDownload)])
[delegate didFinishAllDownload];
}
}
 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if ([delegate respondsToSelector:@selector(didFailDownloadWithError:)])
        [delegate performSelector:@selector(didFailDownloadWithError:) withObject: error];
}
 
@end