ios之mknetworkkit笔记
生活随笔
收集整理的這篇文章主要介紹了
ios之mknetworkkit笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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:paramshttpMethod:@"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:paramshttpMethod:@"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"];//判斷之前是否下載過 如果有下載重新構造HeaderNSMutableDictionary *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:downloadPathappend: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不是一般的累啊,重回接口回調- -
總結
以上是生活随笔為你收集整理的ios之mknetworkkit笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 扩展方法奇思妙用高级篇五:ToSt
- 下一篇: php递归无限极分类