Reputation: 647
i have a video app which downloads and save many videos using NSURLConnection.
I want to know if there is any code of checking if there is enough memory for saving all these videos.
Below is the code which im using for downloading the video:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:videoUrl]cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
expectedLength = [response expectedContentLength];
currentLength = 0;
HUD.mode = MBProgressHUDModeDeterminate;
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
currentLength += [data length];
HUD.progress = currentLength / (float)expectedLength;
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[HUD hide:YES];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"The Internet connection appears to be offline." message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}
NSString *file2 = [NSString stringWithFormat:@"%@/%@.mp4",documentsDirectory,VideoName];
[receivedData writeToFile:file2 atomically:YES];
NSLog(@" %@ file 2 ",file2);
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:2];
// release the connection, and the data object
[connection release];
[receivedData release];
}
Can anyone help me please.Thanks in advance.
Upvotes: 0
Views: 506
Reputation: 57179
Yes you can use NSFileManager
to get the free space available.
NSError *err;
NSFileManager *fman = [NSFileManager defaultManager];
NSString *path = (NSString *)[[[fman URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSDictionary *fattr = [fman attributesOfFileSystemForPath:path error:&err];
//Error checking
NSUInteger freeSize = [[fattr objectForKey:NSFileSystemFreeSize] unsignedIntegerValue];
NSLog(@"Free Space: %f Megabytes", ((float)freeSize)/1048576.00000000f);
Upvotes: 1
Reputation: 8830
You could open a file called file2 in advance of downloading. Seek into the file by the expected file size + a bit extra and save/close it.
Then when you do the
[receivedData writeToFile:file2 atomically:YES];
the space should be preallocated. If it fails originally because there is not enough space you can do so with out downloading.
Upvotes: 0