IOS文件下载

简介: IOS文件下载

iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下如何结合asp.net webservice实现文件上传下载。

首先,让我们看下文件下载。


这里我们下载cnblogs上的一个zip文件。使用NSURLRequest+NSURLConnection可以很方便的实现这个功能。在asp.net webservice中可以将文件的地址返回到iOS系统,iOS系统再通过这个url去请求下载该文件。这里为了简单起见,直接将url写道代码里面了。我们可以使用两种方式去下载文件。


1、同步下载文件:

NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip"; 
        NSURL    *url = [NSURL URLWithString:urlAsString]; 
        NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
        NSError *error = nil; 
        NSData   *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[NSURLConnection sendSynchronousRequest:request 
                                               returningResponse:nil 
                                                           error:&error]; 
        /* 下载的数据 */ 
        if (data != nil){ 
            NSLog(@"下载成功"); 
            if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) { 
                NSLog(@"保存成功."); 
            } 
            else 
            { 
                NSLog(@"保存失败."); 
            } 
        } else { 
            NSLog(@"%@", error); 
        } 

2.异步下载

DownLoadingViewController.h
//  DownLoadingViewController.h 
//  DownLoading 
// 
//  Created by skylin zhu on 11-7-30. 
//  Copyright 2011年 mysoft. All rights reserved. 
// 
#import 
@interface DownLoadingViewController : UIViewController { 
    NSURLConnection *connection;  
    NSMutableData *connectionData; 
} 
@property (nonatomic,retain) NSURLConnection *connection;   
@property (nonatomic,retain) NSMutableData *connectionData; 
@end 
DownLoadingViewController.m
//  DownLoadingViewController.m 
//  DownLoading 
// 
//  Created by skylin zhu on 11-7-30. 
//  Copyright 2011年 mysoft. All rights reserved. 
// 
#import "DownLoadingViewController.h" 
@implementation DownLoadingViewController 
@synthesize connection,connectionData; 
- (void)dealloc 
{ 
    [super dealloc]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 
#pragma mark - View lifecycle 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //文件地址 
    NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip"; 
    NSURL    *url = [NSURL URLWithString:urlAsString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSMutableData *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[[NSMutableData alloc] init]; 
    self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/data; 
    [data release]; 
    NSURLConnection *newConnection = [[NSURLConnection alloc] 
                                      initWithRequest:request 
                                      delegate:self 
                                      startImmediately:YES]; 
    self.connection = newConnection; 
    [newConnection release]; 
    if (self.connection != nil){ 
       NSLog(@"Successfully created the connection"); 
    } else { 
        NSLog(@"Could not create the connection"); 
    } 
} 
- (void) connection:(NSURLConnection *)connection 
            didFailWithError:(NSError *)error{ 
    NSLog(@"An error happened"); 
    NSLog(@"%@", error); 
} 
- (void) connection:(NSURLConnection *)connection 
              didReceiveData:(NSData *)data{ 
    NSLog(@"Received data"); 
    [self.connectionData appendData:data]; 
} 
- (void) connectionDidFinishLoading 
:(NSURLConnection *)connection{ 
    /* 下载的数据 */ 
        NSLog(@"下载成功"); 
        if ([self.connectionData writeToFile:@"UIWebViewDemo.zip" atomically:YES]) { 
            NSLog(@"保存成功."); 
        } 
        else 
        { 
            NSLog(@"保存失败."); 
        } 
    /* do something with the data here */ 
} 
- (void) connection:(NSURLConnection *)connection 
          didReceiveResponse:(NSURLResponse *)response{ 
    [self.connectionData setLength:0]; 
} 
- (void) viewDidUnload{ 
    [super viewDidUnload]; 
    [self.connection cancel]; 
    self.connection = nil; 
    self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/nil; 
} 
@end 
相关文章
|
iOS开发
iOS开发网络篇—文件下载(一·不合理)
iOS开发网络篇—文件下载(一·不合理) 一、小文件下载 如果文件比较小,下载方式会比较多 直接用NSData的+ (id)dataWithContentsOfURL:(NSURL *)url; 利⽤NSURLConnection发送一个HTTP请求去下载 如果是下载图片,还可以利用SDWebImage框架  二、沙盒   1.在finder中,系统的一些文件(资源库)是隐藏的,可以通过在终端运行下图的代码,显示隐藏的文件。
968 0
|
6天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
116 3
|
6天前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
128 2
|
6天前
|
API 开发工具 Android开发
iOS 和 Android 平台的开发有哪些主要区别?
iOS与Android开发区别:iOS用Objective-C/Swift,App Store唯一下载渠道;Android用Java/Kotlin,多商店发布(如Google Play、华为市场)。设计上,iOS简洁一致,Android灵活可定制。开发工具,iOS用Xcode,Android用Android Studio。硬件和系统多样性,iOS统一,Android复杂。权限管理、审核流程及API各有特点,开发者需依据目标平台特性进行选择。
43 3