• 源码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
  • 相关阅读:
    PHP之html~01
    常用的html标签大全
    PHP从PHP5.0到PHP7.1的性能全评测
    PHP的性能演进(从PHP5.0到PHP7.1的性能全评测)
    Unix及类Unix系统文本编辑器的介绍
    keychain 的学习
    开发中遇到的一些问题
    IOS 照片浏览器总结(思想步骤)
    IOS 应用管理(九宫格) 总结笔记
    IOS 集成友盟分享
  • 原文地址:https://www.cnblogs.com/laugh/p/6610894.html
Copyright © 2020-2023  润新知