ios23- 文件下载(同步和异步)
1.第一步:创建一个单例视图
#import <UIKit/UIKit.h>
@inte易做图ce 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 *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)shouldAutorotateToInte易做图ceOrientation:(UIInte易做图ceOrientation)inte易做图ceOrientation
{
// Return YES for supported orientations
return (inte易做图ceOrientation != UIInte易做图ceOrientationPortraitUpsideDown);
}
@end
补充:移动开发 , IOS ,