• 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: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不是一般的累啊,重回接口回调- -

  • 相关阅读:
    阿里云“网红"运维工程师白金:做一个平凡的圆梦人
    3235 战争
    1291 火车线路(区间修改,区间最值)
    P3183 [HAOI2016]食物链
    P1189 SEARCH(逃跑的拉尔夫)
    P1794 装备运输_NOI导刊2010提高(04)
    P2347 砝码称重
    P1332 血色先锋队
    P1772 [ZJOI2006]物流运输
    P1038 神经网络
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3440379.html
Copyright © 2020-2023  润新知