Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v1.3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 200 lines (151 sloc) 4.847 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
//
// Downloader.m
// iOS-URLDownloader
//
// Created by Kristijan Sedlak on 7/21/11.
// Copyright 2011 AppStrides. All rights reserved.
//

#import "URLDownloader.h"


#pragma mark -

@interface URLDownloader()

@property (retain) NSURLConnection *urlConnection;
@property (retain) NSURLResponse *urlResponse;
@property (retain) NSMutableData *urlData;
@property (retain) URLCredential *urlCredential;

@end


#pragma mark -

@implementation URLDownloader

@synthesize delegate;

@synthesize urlConnection;
@synthesize urlResponse;
@synthesize urlData;
@synthesize urlCredential;

#pragma mark General

- (void)dealloc
{
    [urlConnection cancel];
    
[delegate release];
    
[urlConnection release];
    [urlResponse release];
[urlData release];
    [urlCredential release];

    [super dealloc];
}

+ (id)downloaderWithDelegate:(id)obj
{
    URLDownloader *downloader = [[URLDownloader alloc] init];
    downloader.delegate = obj;
    
    return [downloader autorelease];
}

- (void)reset
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

#pragma mark Actions

- (void)download:(NSURLRequest *)request withCredential:(URLCredential *)credential
{
    self.urlCredential = credential;
    self.urlResponse = nil;
self.urlData = [[[NSMutableData alloc] initWithData:nil] autorelease];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];

    [self.urlConnection start];

NSLog(@"[URLDownloader] Download started");
}

- (void)cancel
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
[urlConnection cancel];

NSLog(@"[URLDownloader] Download canceled");
    if ([self.delegate respondsToSelector:@selector(urlDownloaderDidCancelDownloading:)])
    {
        [self.delegate urlDownloaderDidCancelDownloading:self];
    }
}

#pragma mark Information

- (long)fullContentSize
{
    @try
    {
        return [self.urlResponse expectedContentLength];
    }
    @catch (NSException * e)
    {
        return 0.0;
    }
}

- (long)downloadedContentSize
{
    @try
    {
        return (long)[self.urlData length];
    }
    @catch (NSException * e)
    {
        return 0.0;
    }
}

- (float)downloadCompleteProcent
{
    float contentSize = (float)[self fullContentSize];
    float downloadedSize = (float)[self downloadedContentSize];
    
    return contentSize > 0.0 ? downloadedSize / contentSize : 0.0;
}

#pragma mark Connection

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
NSLog(@"[URLDownloader] Authentication challenge received");

NSURLCredential *credential = [NSURLCredential credentialWithUser:self.urlCredential.username
password:self.urlCredential.password
persistence:self.urlCredential.persistance];
        
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

NSLog(@"[URLDownloader] Credentials sent");
}
else
{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
NSLog(@"[URLDownloader] Authentication failed");
        [self.delegate urlDownloader:self didFailOnAuthenticationChallenge:challenge];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.urlResponse = response;
    [self.urlData setLength:0]; // in case of 302

    NSLog(@"[URLDownloader] Downloading %@ ...", [[response URL] absoluteString]);
    if ([self.delegate respondsToSelector:@selector(urlDownloaderDidStart:)])
    {
        [self.delegate urlDownloaderDidStart:self];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.urlData appendData:data];
    
    if ([self.delegate respondsToSelector:@selector(urlDownloader:didReceiveData:)])
    {
        [self.delegate urlDownloader:self didReceiveData:data];
    }

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

NSLog(@"[URLDownloader] Error: %@, %d", error, [error code]);
switch ([error code])
{
case NSURLErrorNotConnectedToInternet:
[self.delegate urlDownloader:self didFailWithNotConnectedToInternetError:error];
break;
default:
            [self.delegate urlDownloader:self didFailWithError:error];;
break;
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
    NSLog(@"[URLDownloader] Download finished");

    NSData *data = [NSData dataWithData:self.urlData];
    [self.delegate urlDownloader:self didFinishWithData:data];
}

@end
Something went wrong with that request. Please try again.