zoukankan      html  css  js  c++  java
  • 源码0603-03-掌握-NSURLSession

    //
    //  ViewController.m
    //  03-掌握-NSURLSession
    
    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSLog(@"%@", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]);
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self download];
    }
    
    - (void)download
    {
        // 获得NSURLSession对象
        NSURLSession *session = [NSURLSession sharedSession];
        
        // 获得下载任务
        NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
            // 文件将来存放的真实路径
            NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
            
            // 剪切location的临时文件到真实路径
            NSFileManager *mgr = [NSFileManager defaultManager];
            [mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
        }];
        
        // 启动任务
        [task resume];
    }
    
    - (void)post
    {
        // 获得NSURLSession对象
        NSURLSession *session = [NSURLSession sharedSession];
        
        // 创建请求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login"]];
        request.HTTPMethod = @"POST"; // 请求方法
        request.HTTPBody = [@"username=520it&pwd=520it" dataUsingEncoding:NSUTF8StringEncoding]; // 请求体
        
        // 创建任务
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        }];
        
        // 启动任务
        [task resume];
    }
    
    - (void)get2
    {
        // 获得NSURLSession对象
        NSURLSession *session = [NSURLSession sharedSession];
        
        // 创建任务
        NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=4324"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        }];
        
        // 启动任务
        [task resume];
    }
    
    - (void)get
    {
        // 获得NSURLSession对象
        NSURLSession *session = [NSURLSession sharedSession];
        
        // 创建任务
        NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=4324"]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        }];
        
        // 启动任务
        [task resume];
    }
    
    @end

     

    04-掌握-NSURLSession-代理方法

    //
    //  ViewController.m
    //  04-掌握-NSURLSession-代理方法
    #import "ViewController.h"
    
    @interface ViewController () <NSURLSessionDataDelegate, NSURLConnectionDataDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        // 获得NSURLSession对象
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
        
        // 创建任务
        NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=4324"]]];
        
        // 启动任务
        [task resume];
    }
    
    #pragma mark - <NSURLSessionDataDelegate>
    /**
     * 1.接收到服务器的响应
     */
    - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
    {
        NSLog(@"%s", __func__);
        
        // 允许处理服务器的响应,才会继续接收服务器返回的数据
        completionHandler(NSURLSessionResponseAllow);
        
        // void (^)(NSURLSessionResponseDisposition)
    }
    
    /**
     * 2.接收到服务器的数据(可能会被调用多次)
     */
    - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
    {
        NSLog(@"%s", __func__);
    }
    
    /**
     * 3.请求成功或者失败(如果失败,error有值)
     */
    - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
    {
        NSLog(@"%s", __func__);
    }
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    一道网易面试题
    OC的引用计数
    ReplayKit2 采集音视频回调格式分析
    《剑指offer3- 从末尾到头打印链表》
    《剑指offer
    《剑指offer
    ReplayKit2:声音回调时间戳问题
    UILable在Autolayout模式下面自动调节字体大小
    建表手写语句
    oracle创建主键序列和在ibatis中应用
  • 原文地址:https://www.cnblogs.com/laugh/p/6610894.html
Copyright © 2011-2022 走看看