iOS开发之MKNetworkKit笔记
1 // 2 // RootViewController.m 3 // webTest 4 // 5 // Created by mmc on 13-11-24. 6 // Copyright (c) 2013年 mmc. All rights reserved. 7 // 8 9 #import "RootViewController.h" 10 #import "MKNetworkEngine.h" 11 12 @implementation RootViewController 13 14 - (IBAction) getTest:(id)sender 15 { 16 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.1.105:8080"]; 17 [engine useCache]; 18 19 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0]; 20 [params setObject:@"get数据1" forKey:@"arg1"]; 21 [params setObject:@"get数据2" forKey:@"arg2"]; 22 23 //最后的斜杠不能丢掉,不然会出问题 24 MKNetworkOperation *operation = [engine operationWithPath:@"/yii/testApp/index.php?r=httpTest/getTest/" 25 params:params 26 httpMethod:@"GET"]; 27 28 [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) 29 { 30 NSString *responseString = [completedOperation responseString]; 31 NSLog(@"%@", responseString); 32 33 if([completedOperation isCachedResponse]) { 34 NSLog(@"Data from cache %@", [completedOperation responseString]); 35 } 36 else { 37 NSLog(@"Data from server %@", [completedOperation responseString]); 38 } 39 40 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) { 41 42 NSLog(@"%@",error); 43 }]; 44 45 [engine enqueueOperation:operation]; 46 47 } 48 49 - (IBAction) postTest:(id)sender 50 { 51 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.1.105:8080"]; 52 [engine useCache]; 53 54 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0]; 55 [params setObject:@"post数据1" forKey:@"arg1"]; 56 [params setObject:@"post数据2" forKey:@"arg2"]; 57 58 MKNetworkOperation *operation = [engine operationWithPath:@"/yii/testApp/index.php?r=httpTest/postTest/" 59 params:params 60 httpMethod:@"POST"]; 61 62 [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) 63 { 64 NSString *responseString = [completedOperation responseString]; 65 NSLog(@"%@", responseString); 66 67 if([completedOperation isCachedResponse]) { 68 NSLog(@"Data from cache %@", [completedOperation responseString]); 69 } 70 else { 71 NSLog(@"Data from server %@", [completedOperation responseString]); 72 } 73 74 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) { 75 76 NSLog(@"%@",error); 77 }]; 78 79 [engine enqueueOperation:operation]; 80 } 81 82 - (IBAction) downloadTest:(id)sender 83 { 84 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@"127.0.0.1"]; 85 [engine useCache]; 86 87 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 88 NSString *cachesDirectory = paths[0]; 89 NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:@"x.iso"]; 90 91 92 //判断之前是否下载过 如果有下载重新构造Header 93 NSMutableDictionary *newHeadersDict = [[NSMutableDictionary alloc] init]; 94 95 NSFileManager *fileManager = [NSFileManager defaultManager]; 96 97 98 if ([fileManager fileExistsAtPath:downloadPath]) 99 { 100 NSError *error = nil; 101 unsigned long long fileSize = [[fileManager attributesOfItemAtPath:downloadPath error:&error] fileSize]; 102 103 NSString *headerRange = [NSString stringWithFormat:@"bytes=%llu-", fileSize]; 104 [newHeadersDict setObject:headerRange forKey:@"Range"]; 105 } 106 107 MKNetworkOperation *operation = [engine operationWithURLString:@"http://192.168.1.105:8080/2.iso"]; 108 109 [operation addDownloadStream:[NSOutputStream outputStreamToFileAtPath:downloadPath 110 append:YES]]; 111 112 [operation addHeaders:newHeadersDict]; 113 [engine enqueueOperation:operation]; 114 115 //进度回调 116 [operation onDownloadProgressChanged:^(double progress) 117 { 118 NSLog(@"download %.2f", progress*100.0); 119 }]; 120 121 //结束回调 122 [operation addCompletionHandler:^(MKNetworkOperation* completedRequest) 123 { 124 NSLog(@"download complete %@", completedRequest); 125 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) 126 { 127 NSLog(@"%@", error); 128 }]; 129 } 130 131 @end
激情为梦想而生