dimanche 2 novembre 2014

Resume capability in download of the file in iPhone


Vote count:

0




I am using following code to resume a downloading



-(void) downloadFileAtURLPath:(NSString*)URLpath filename:(NSString *)filename
{
NSLog(@"Getting %@", URLpath);
NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

NSMutableURLRequest *req;
req = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];

if (![NSURLConnection canHandleRequest:req])
{}

if (downloadingConnection != nil)
{
[self.downloadingConnection cancel];
downloadingConnection = nil;
}

NSFileManager *manager = [NSFileManager defaultManager];
fileName = filename;//[[NSString alloc] initWithString:[URLpath lastPathComponent]];

NSString *directoryPath = [self createDirectory:APP_NAME];
NSString *dirPath = [directoryPath stringByAppendingPathComponent:fileName];

long long downloadedBytes = 0;
if ([manager fileExistsAtPath:dirPath])
{
NSError *error = nil;
NSDictionary *fileDictionary = [manager attributesOfItemAtPath:dirPath
error:&error];
if (!error && fileDictionary)
downloadedBytes = [fileDictionary fileSize];
}
else
{
[manager createFileAtPath:dirPath contents:nil attributes:nil];
}

if (downloadedBytes > 0)
{
NSString *requestRange = [NSString stringWithFormat:@"bytes=%lld-", downloadedBytes];
[req setValue:requestRange forHTTPHeaderField:@"Range"];
}

self.dataLengthOfWholeConnection = 0;
self.dataWrittenForWholeConnection = 0;

self.downloadingConnection = [NSURLConnection connectionWithRequest:req delegate:self];
[self.downloadingConnection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]])
{
// I don't know what kind of request this is!
return;
}

self.dataLengthOfWholeConnection = [response expectedContentLength];

NSString *directoryPath = [self createDirectory:APP_NAME];
NSString *dirPath = [directoryPath stringByAppendingPathComponent:fileName];

NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:dirPath];
self.fileHandle = fh;

switch (httpResponse.statusCode)
{

case 206:
{
NSString *range = [httpResponse.allHeaderFields valueForKey:@"Content-Range"];
NSError *error = nil;
NSRegularExpression *regex = nil;

// Check to see if the server returned a valid byte-range
regex = [NSRegularExpression regularExpressionWithPattern:@"bytes (\\d+)-\\d+/\\d+"
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error)
{
isValid = NO;
[fh truncateFileAtOffset:0];
break;
}

// If the regex didn't match the number of bytes, start the download from the beginning
NSTextCheckingResult *match = [regex firstMatchInString:range
options:NSMatchingAnchored
range:NSMakeRange(0, range.length)];
if (match.numberOfRanges < 2)
{
[fh truncateFileAtOffset:0];
isValid = YES;
break;
}

// Extract the byte offset the server reported to us, and truncate our
// file if it is starting us at "0". Otherwise, seek our file to the
// appropriate offset.
NSString *byteStr = [range substringWithRange:[match rangeAtIndex:1]];
NSInteger bytes = [byteStr integerValue];
if (bytes <= 0)
{
//[fh truncateFileAtOffset:0];
break;
}
else
{
isValid = YES;
self.dataWrittenForWholeConnection = bytes;
self.dataLengthOfWholeConnection += bytes;
[fh seekToFileOffset:bytes];
}
break;
}

default:
if (dataLengthOfWholeConnection <= 0)
{
isValid = NO;
}
else
{
isValid = YES;
[fh truncateFileAtOffset:0];
}
break;
}
}


Problem is its not resuming the download. With range i am STILL getting httpResponse.statusCode 200 status instead of 206. WHAT is wrong?


i AM Using a tinyurl link



asked 1 min ago







Resume capability in download of the file in iPhone

Aucun commentaire:

Enregistrer un commentaire