Вы можете использовать NSURLConnection
для выполнения этой задачи.Чтобы реализовать это, вам нужно реализовать методы делегата NSURLConnection.
В вашем m вам нужно реализовать несколько методов класса делегата
//NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[progressView setProgress:receivedData.length/contentLength];
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
contentLength = [response expectedContentLength];
[progressView setProgress:0];
[receivedData setLength:0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (receivedData.length > 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"WriteZip");
[receivedData writeToFile:[NSString stringWithFormat:@"%@/file.zip",documentsDirectory] atomically:YES];
[self finishedUpdating];
[theConnection release];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
// receivedData is declared as a method instance elsewhere
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
[self finishedUpdating];
}
Вот пример того, что мойH выглядит как
#import <UIKit/UIKit.h>
@interface UpdateViewController : UIViewController <NSURLConnectionDelegate> {
NSURLConnection *theConnection;
NSMutableData *receivedData;
float contentLength;
}
@property (nonatomic, retain) IBOutlet UIView *progressContainer;
@property (nonatomic, retain) IBOutlet UIProgressView *progressView;
@property (nonatomic, retain) IBOutlet UILabel *progressLabel;
-(void)requestUpdates;
-(void)finishedUpdating;
@end
Некоторые документы для чтения
Справочник по классам NSURLConnection
Руководство по системному URL-адресу