• 源码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
  • 相关阅读:
    接口实际上是定义一个规范、标准
    类必须实现接口中的方法,否则其为一抽象类
    JAVA的核心概念:接口(interface)
    子类的方法必须覆盖父类的抽象方法
    Abstract可以将子类的共性最大限度的抽取出来,放在父类中,以提高程序的简洁性
    如果将一个类设置为abstract,则此类必须被继承使用
    在JAVA中利用public static final的组合方式对常量进行标识
    final可以修饰类、属性、方法
    覆盖不适用于静态方法
    静态方法不需要有对象,可以使用类名调用
  • 原文地址:https://www.cnblogs.com/laugh/p/6610894.html
Copyright © 2020-2023  润新知