日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios之mknetworkkit笔记

發布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。