[ios] AFNetworking 을 이용하여 File download start, pause, resume, stop 시키기
static NSString* const kZipURL = @"http://download.thinkbroadband.com/200MB.zip";
@interface ViewController (){
AFHTTPRequestOperation* _operation;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initHTTPRequestOperation]; // 더 적합한 position 으로 옮겨야 하지만, 개념만 보여주기 위해.
}
- (IBAction)onStartClicked:(id)sender {
[_operation start];
}
- (IBAction)onPauseClicked:(id)sender {
[_operation pause];
}
- (IBAction)onResumeClicked:(id)sender {
[_operation resume];
}
- (IBAction)onStopClicked:(id)sender {
[_operation cancel];
}
-(void)initHTTPRequestOperation{
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[docPaths lastObject] stringByAppendingPathComponent:@"test.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kZipURL]];
_operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
_operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[_operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"MMM %lld / %lld", totalBytesRead, totalBytesExpectedToRead);
}];
[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@“MMM Success”);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@“MMM Fail %@”, error);
}];
}
@end
'프로그래밍 놀이터 > iOS' 카테고리의 다른 글
[Xcode tip] how to drag selected code to make custom snippet? (0) | 2018.04.07 |
---|---|
[iOS] UILocalNotification 권한 요청하는 코드 (0) | 2018.04.05 |
[iOS] 앱이 지원하는 File Type 등록하고 처리하기 (0) | 2018.02.20 |
[ios] 32bit, 64bit 이야기 (0) | 2018.02.18 |
[ios] NSInteger, NSUInteger on 32bit / 64bit (0) | 2018.02.17 |
댓글