网络总结

多线程

  • NSThread
  • GCD
    • 队列
      • 并发队列
        • 全局队列
        • 自己创建
      • 串行队列
        • 自己创建
        • 主队列
    • 任务:block
    • 函数
      • sync:同步函数
      • async:异步函数
    • 单例模式
  • NSOperation
  • RunLoop
    • 同一时间只能选择一个运行模式
    • 常用模式
      • Default:默认
      • Tracking:拖拽UIScrollView

网络

HTTP请求

  • GET请求

    
    // 将URL转换成UTF-8
    NSString * urlString = @"你好";
    NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];
    
    NSURL * url = [NSURL URLWithString:urlString];
    
    // 创建一个GET请求
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
  • post

      // 将URL转换成UTF-8
      NSString * urlString = @"你好";
      NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
      urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];
    
      NSURL * url = [NSURL URLWithString:urlString];
    
      // 创建一个GET请求
      NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
      request.HTTPMethod = @"POST";
    

    NSURLConnection(iOS9已经过期)

  • 同步方法
  • 异步方法(block)
  • 异步方法- 代理

    NSURLSession

  • 发送一般的GET\POST请求

    // 将URL转换成UTF-8
      NSString * urlString = @"你好";
      NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
      urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];
    
      NSURL * url = [NSURL URLWithString:urlString];
    
      // 创建一个GET请求
      NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
      request.HTTPMethod = @"POST";
    
      NSURLSessionDataTask * task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
      }];
      [task resume];
    
  • 下载文件- 不需要离线断点

      // 将URL转换成UTF-8
      NSString * urlString = @"你好";
      NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
      urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];
    
      NSURL * url = [NSURL URLWithString:urlString];
    
      // 创建一个GET请求
      NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
      request.HTTPMethod = @"POST";
    
      NSURLSessionDownloadTask * task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
      }];
      [task resume];
    
  • 下载文件- 需要离线断点
  // 将URL转换成UTF-8
    NSString * urlString = @"你好";
    NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSURL * url = [NSURL URLWithString:urlString];

    // 创建一个GET请求
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";

    // 创建session
    NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];

    // 设置请求头(例如告诉服务器从第1024个字节开始下载)
    [request setValue:@"bytes=1024-" forHTTPHeaderField:@"Range"];

    // 创建任务
    NSURLSessionDataTask * task = [session dataTaskWithRequest:request];

    // 开启任务
    [task resume];
#pragma mark - NSURLSessionDataDelegate
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {

    // 打开流

    // 获得文件的总长度

    // 存储文件的总长度

    // 回调

    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    // 利用NSOutputStream写入数据

    // 计算下载进度
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

    // 关闭并且清空NSOutputStream

    // 清空任务
}
  • 文件上传 <可以用POST请求>
    • 设置请求头
    • 拼接请求体
    • 其他参数(非文件参数)
    // 将URL转换成UTF-8
    NSString * urlString = @"你好";
    NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:urlString];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSURL * url = [NSURL URLWithString:urlString];

    // 创建一个GET请求
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";


    NSURLSessionUploadTask * task = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:body completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    }];

    [task resume];

AFNetWorking

UIWebView

results matching ""

    No results matching ""