欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios23- 文件下载(同步和异步)

程序员文章站 2022-05-18 10:09:21
1.第一步:创建一个单例视图 #import   @interface ios23_downviewcontroll...

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


#import <uikit/uikit.h>

 

@interface ios23_downviewcontroller : uiviewcontroller<nsurlconnectiondelegate,nsurlconnectiondatadelegate>{

    nsmutabledata *connectiondata;

   
}

-(ibaction)tongbu;

-(ibaction)yibu;

@property (nonatomic,retain) nsmutabledata *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 ";

    //构建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 ";

    //构建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