zoukankan      html  css  js  c++  java
  • ios23- 文件下载(同步和异步)

    1.第一步:创建一个单例视图

     

    #import <UIKit/UIKit.h>


    @interface ios23_downViewController : UIViewController<NSURLConnectionDelegate,NSURLConnectionDataDelegate>{

        NSMutableData *connectionData;

        

    }

    -(IBAction)tongbu;

    -(IBAction)yibu;

    @property (nonatomic,retainNSMutableData *connectionData;

    @end

    2

    //

    //  ios23_downViewController.m

    //  ios23-down

    //

    //  Created by  on 13-6-17.

    //  Copyright 2013 __MyCompanyName__. All rights reserved.

    //


    #import "ios23_downViewController.h"


    @implementation ios23_downViewController

    @synthesize connectionData;


    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.

    }


    #pragma mark - View lifecycle

    -(IBAction)tongbu{

        NSLog(@"同步");

        NSError *err;

        //定义url

        NSString *url=@"http://172.22.65.38/new/1.doc";

        //构建NSURL

        NSURL *fileUrl=[NSURL URLWithString:url];

        //构建nsurlrequest

        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

        //建立连接

        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

        if (data.length>0) {

            NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

            //当数据写入的时候

            if ([data writeToFile:savePath atomically:YES]) {

                NSLog(@"保存成功");

            }else{

                NSLog(@"保存失败");

            }

        }

                    

               

    }

    -(IBAction)yibu{

        NSLog(@"异步");

        NSError *err;

        //定义url

        NSString *url=@"http://172.22.65.38/new/1.doc";

        //构建NSURL

        NSURL *fileUrl=[NSURL URLWithString:url];

        //构建nsurlrequest

        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

        //建立连接

        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

        NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];

        //初始化connectionData;

        connectionData=[[NSMutableData alloc]init ];

        

    }

    //接受数据

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

        //获取服务器传递的数据

        [connectionData appendData:data];

    }

    //接收数据成功

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{

        if (connectionData.length>0) {

            NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

            //当数据写入的时候

            if ([connectionData writeToFile:savePath atomically:YES]) {

                NSLog(@"保存成功");

            }else{

                NSLog(@"保存失败");

            }

        }}

    //接收数据失败

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    }


    - (void)viewDidUnload

    {

        [super viewDidUnload];

        // Release any retained subviews of the main view.

        // e.g. self.myOutlet = nil;

    }


    - (void)viewWillAppear:(BOOL)animated

    {

        [super viewWillAppear:animated];

    }


    - (void)viewDidAppear:(BOOL)animated

    {

        [super viewDidAppear:animated];

    }


    - (void)viewWillDisappear:(BOOL)animated

    {

    [super viewWillDisappear:animated];

    }


    - (void)viewDidDisappear:(BOOL)animated

    {

    [super viewDidDisappear:animated];

    }


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        // Return YES for supported orientations

        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

    }


    @end


  • 相关阅读:
    flutter 计算时间日期 在当年的第几周
    Flutter Future 异步 FutureBuilder 获取数据后 加载Widget
    macOS server 中描述文件管理器开启提示“发生错误,代码为-1”
    VS Code 添加chrome调试及localhost 拒绝连接请求问题
    Finished with error: Error running pod install (Android Studio 运行flutter项目)
    flutter moudle 项目编译生成 .ios 和 .android 更改.xx项目代码不被覆盖 flutter make-host-app-editable
    flutter项目 通道Channel封装及使用案例
    react-native 调用 TouchableOpacity (触摸透明) 时报了一个警告
    webstorm2016.2 for mac 安装
    Computed read-only property vs function in Swift
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3141054.html
Copyright © 2011-2022 走看看