zoukankan      html  css  js  c++  java
  • IOS-网络(小文件下载)

     1 //
     2 //  ViewController.m
     3 //  IOS_0131_小文件下载
     4 //
     5 //  Created by ma c on 16/1/31.
     6 //  Copyright © 2016年 博文科技. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 
    11 @interface ViewController ()
    12 
    13 @end
    14 
    15 @implementation ViewController
    16 /*
    17  下载小文件的方式
    18  1.NSData dataWithContentOfURL
    19  2.NSURLConnection
    20  */
    21 
    22 - (void)viewDidLoad {
    23     [super viewDidLoad];
    24     
    25 }
    26 
    27 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    28 {
    29     [self downloadFile1];
    30     [self downloadFile2];
    31 }
    32 
    33 ///1.NSData dataWithContentOfURL
    34 - (void)downloadFile1
    35 {
    36     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    37         
    38         NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];
    39         NSData *data = [NSData dataWithContentsOfURL:url];
    40         NSLog(@"downloadFile1---%ld",data.length);
    41     });
    42 }
    43 ///2.NSURLConnection
    44 - (void)downloadFile2
    45 {
    46     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];
    47     
    48     NSURLRequest *request = [NSURLRequest requestWithURL:url];
    49     
    50     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
    51         NSLog(@"downloadFile2---%ld",data.length);
    52         
    53     }];
    54 }
    55 
    56 
    57 @end
  • 相关阅读:
    用继承和组合的知识构造一辆汽车,功能需求见注释
    应用组合的方式实现继承关系
    PL/SQL 07 触发器 trigger
    PL/SQL 05 存储过程 procedure
    PL/SQL 04 游标 cursor
    PL/SQL 03 流程控制
    PL/SQL 02 声明变量 declare
    PL/SQL 01 代码编写规则
    Oracle基础 12 对象 objects 同义词/序列/试图/索引
    Oracle基础 11 约束 constraints
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5174149.html
Copyright © 2011-2022 走看看