asi沒法用了,蛋疼了,在af和mk之間糾結,感覺af不適合我的口味,解析和網絡耦合相對似乎重了點
mk似乎默認的不支持下載的斷點續傳,這里參考網上的代碼處理了下,0修改mk的庫,下面是實現的代碼
// // RootViewController.m // webTest // // Created by mmc on 13-11-24. // Copyright (c) 2013年 mmc. All rights reserved. // #import "RootViewController.h" #import "MKNetworkEngine.h" @implementation RootViewController - (IBAction) getTest:(id)sender { MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.1.105:8080"]; [engine useCache]; NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0]; [params setObject:@"get數據1" forKey:@"arg1"]; [params setObject:@"get數據2" forKey:@"arg2"]; //最后的斜杠不能丟掉,不然會出問題 MKNetworkOperation *operation = [engine operationWithPath:@"/yii/testApp/index.php?r=httpTest/getTest/" params:params httpMethod:@"GET"]; [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) { NSString *responseString = [completedOperation responseString]; NSLog(@"%@", responseString); if([completedOperation isCachedResponse]) { NSLog(@"Data from cache %@", [completedOperation responseString]); } else { NSLog(@"Data from server %@", [completedOperation responseString]); } }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) { NSLog(@"%@",error); }]; [engine enqueueOperation:operation]; } - (IBAction) postTest:(id)sender { MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.1.105:8080"]; [engine useCache]; NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0]; [params setObject:@"post數據1" forKey:@"arg1"]; [params setObject:@"post數據2" forKey:@"arg2"]; MKNetworkOperation *operation = [engine operationWithPath:@"/yii/testApp/index.php?r=httpTest/postTest/" params:params httpMethod:@"POST"]; [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) { NSString *responseString = [completedOperation responseString]; NSLog(@"%@", responseString); if([completedOperation isCachedResponse]) { NSLog(@"Data from cache %@", [completedOperation responseString]); } else { NSLog(@"Data from server %@", [completedOperation responseString]); } }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) { NSLog(@"%@",error); }]; [engine enqueueOperation:operation]; } - (IBAction) downloadTest:(id)sender { MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"127.0.0.1"]; [engine useCache]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDirectory = paths[0]; NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:@"x.iso"]; //判斷之前是否下載過 如果有下載重新構造Header NSMutableDictionary *newHeadersDict = [[NSMutableDictionary alloc] init]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:downloadPath]) { NSError *error = nil; unsigned long long fileSize = [[fileManager attributesOfItemAtPath:downloadPath error:&error] fileSize]; NSString *headerRange = [NSString stringWithFormat:@"bytes=%llu-", fileSize]; [newHeadersDict setObject:headerRange forKey:@"Range"]; } MKNetworkOperation *operation = [engine operationWithURLString:@"http://192.168.1.105:8080/2.iso"]; [operation addDownloadStream:[NSOutputStream outputStreamToFileAtPath:downloadPath append:YES]]; [operation addHeaders:newHeadersDict]; [engine enqueueOperation:operation]; //進度回調 [operation onDownloadProgressChanged:^(double progress) { NSLog(@"download %.2f", progress*100.0); }]; //結束回調 [operation addCompletionHandler:^(MKNetworkOperation* completedRequest) { NSLog(@"download complete %@", completedRequest); }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) { NSLog(@"%@", error); }]; } @end
下一步,實現xxxBegin,xxxEnd,xxxFail,然后弄個delegates,做響應鏈傳遞,神奇的block,實在受不了,看起來真tmd不是一般的累啊,重回接口回調- -