iOS開発——ネットワークプログラミングOC編&(六)データダウンロード

53089 ワード

ファイルのダウンロード
iOSの開発ではファイルのアップロードダウンロード機能がよく使われていますが、このファイルではaspと結びつける方法を紹介します.Netwebserviceはファイルのアップロードとダウンロードを実現します.まず、ファイルのダウンロードを見てみましょう.ここではcnblogsのzipファイルをダウンロードします.NSURLRequest+NSURLConnectionでこの機能を簡単に実現できます.asp.NetwebserviceではファイルのアドレスをiOSシステムに返すことができ、iOSシステムはこのurlを通じてファイルのダウンロードを要求します.ここでは簡単にするためにurlをコードに直接書きました.ファイルをダウンロードするには、2つの方法があります.
 
ファイルのダウンロード
 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     
 5     //         
 6     // 1.NSData dataWithContentsOfURL
 7     // 2.NSURLConnection
 8 }
 9 
10 // 1.NSData dataWithContentsOfURL
11 - (void)downloadFile
12 {
13     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
14         //        GET  
15         NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];
16         NSData *data = [NSData dataWithContentsOfURL:url];
17         NSLog(@"%d", data.length);
18     });
19 }
20 
21 // 2.NSURLConnection
22 - (void)downloadFile2
23 {
24     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];
25     NSURLRequest *request = [NSURLRequest requestWithURL:url];
26     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
27         NSLog(@"%d", data.length);
28     }];
29 }

 
ファイルのダウンロード
一:直接ファイルデータをダウンロードする
 1 - (void)download
 2 {
 3     // 1.URL
 4     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos.zip"];
 5     
 6     // 2.  
 7     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 8     
 9     // 3.  (   conn   ,           )
10     [NSURLConnection connectionWithRequest:request delegate:self];
11     
12 //    [[NSURLConnection alloc] initWithRequest:request delegate:self];
13 //    [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
14     
15 //    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
16 //    [conn start];
17 }
18 
19 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
20 {
21     [self download];
22 }
23 
24 #pragma mark - NSURLConnectionDataDelegate    
25 /**
26  *         (    、    )
27  *
28  *  @param error          
29  */
30 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
31 {
32     NSLog(@"didFailWithError");
33 }
34 
35 /**
36  *  1.             
37  *
38  *  @param response     
39  */
40 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
41 {
42     NSLog(@"didReceiveResponse");
43     
44     //      
45     self.fileData = [NSMutableData data];
46     
47     //         
48 //    NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
49 //    long long fileLength = [resp.allHeaderFields[@"Content-Length"] longLongValue];
50     self.totalLength = response.expectedContentLength;
51 }
52 
53 /**
54  *  2.                 (    ,            )
55  *
56  *  @param data              
57  */
58 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
59 {
60     //     
61     [self.fileData appendData:data];
62     
63     //      
64     // 0 ~ 1
65 //    self.progressView.progress = (double)self.fileData.length / self.totalLength;
66     self.circleView.progress = (double)self.fileData.length / self.totalLength;
67 }
68 
69 /**
70  *  3.       (             )
71  */
72 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
73 {
74     NSLog(@"connectionDidFinishLoading");
75     
76     //       
77     NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
78     NSString *file = [cache stringByAppendingPathComponent:@"videos.zip"];
79     
80     //      
81     [self.fileData writeToFile:file atomically:YES];
82 }

二:ダウンロードデータの保存
 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 2 {
 3     // 1.URL
 4     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/music.zip"];
 5     
 6     // 2.  
 7     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 8     
 9     // 3.  (   conn   ,           )
10     [NSURLConnection connectionWithRequest:request delegate:self];
11 }
12 
13 #pragma mark - NSURLConnectionDataDelegate    
14 /**
15  *         (    、    )
16  *
17  *  @param error          
18  */
19 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
20 {
21     NSLog(@"didFailWithError");
22 }
23 
24 /**
25  *  1.             
26  *
27  *  @param response     
28  */
29 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
30 {
31     //     
32     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
33     NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"];
34     
35     //               
36     NSFileManager *mgr = [NSFileManager defaultManager];
37     [mgr createFileAtPath:filepath contents:nil attributes:nil];
38     
39     //               
40     self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
41     
42     //         
43     self.totalLength = response.expectedContentLength;
44 }
45 
46 /**
47  *  2.                 (    ,            )
48  *
49  *  @param data              
50  */
51 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
52 {
53     //          
54     [self.writeHandle seekToEndOfFile];
55     
56     //        
57     [self.writeHandle writeData:data];
58     
59     //        
60     self.currentLength += data.length;
61     
62     NSLog(@"    :%f", (double)self.currentLength/ self.totalLength);
63     self.circleView.progress = (double)self.currentLength/ self.totalLength;
64 }
65 
66 /**
67  *  3.       (             )
68  */
69 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
70 {
71     self.currentLength = 0;
72     self.totalLength = 0;
73     
74     //     
75     [self.writeHandle closeFile];
76     self.writeHandle = nil;
77 }

ブレークポイントのダウンロード:
 1 #pragma mark - NSURLConnectionDataDelegate    
 2 /**
 3  *         (    、    )
 4  *
 5  *  @param error          
 6  */
 7 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 8 {
 9     NSLog(@"didFailWithError");
10 }
11 
12 /**
13  *  1.             
14  *
15  *  @param response     
16  */
17 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
18 {
19     if (self.currentLength) return;
20     
21     //     
22     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
23     NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"];
24     
25     //               
26     NSFileManager *mgr = [NSFileManager defaultManager];
27     [mgr createFileAtPath:filepath contents:nil attributes:nil];
28     
29     //               
30     self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
31     
32     //         
33     self.totalLength = response.expectedContentLength;
34 }
35 
36 /**
37  *  2.                 (    ,            )
38  *
39  *  @param data              
40  */
41 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
42 {
43     //          
44     [self.writeHandle seekToEndOfFile];
45     
46     //        
47     [self.writeHandle writeData:data];
48     
49     //        
50     self.currentLength += data.length;
51     
52     self.circleView.progress = (double)self.currentLength/ self.totalLength;
53 }
54 
55 /**
56  *  3.       (             )
57  */
58 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
59 {
60     self.currentLength = 0;
61     self.totalLength = 0;
62     
63     //     
64     [self.writeHandle closeFile];
65     self.writeHandle = nil;
66 }
67 
68 - (IBAction)download:(UIButton *)sender {
69     //     
70     sender.selected = !sender.isSelected;
71     
72     //     
73     //     
74     
75     if (sender.selected) { //   (  )  
76         // 1.URL
77         NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos.zip"];
78         
79         // 2.  
80         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
81         
82         //      
83         NSString *range = [NSString stringWithFormat:@"bytes=%lld-", self.currentLength];
84         [request setValue:range forHTTPHeaderField:@"Range"];
85         
86         // 3.  (   conn   ,           )
87         self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
88     } else { //   
89         [self.conn cancel];
90         self.conn = nil;
91     }
92 }

 
iOS開発ではファイルダウンロードは同期と非同期でダウンロードできますが、一般的に開発では良好なユーザーインタラクションのために同期を使用することはめったにありません.
ダウンロードの同期
 1 NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";  
 2         NSURL    *url = [NSURL URLWithString:urlAsString];  
 3         NSURLRequest *request = [NSURLRequest requestWithURL:url];  
 4         NSError *error = nil;  
 5         NSData   *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[NSURLConnection sendSynchronousRequest:request  
 6                                                returningResponse:nil  
 7                                                            error:&error];  
 8         /*       */  
 9         if (data != nil){  
10             NSLog(@"    ");  
11             if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {  
12                 NSLog(@"    .");  
13             }  
14             else  
15             {  
16                 NSLog(@"    .");  
17             }  
18         } else {  
19             NSLog(@"%@", error);  
20         }

 
非同期ダウンロード
  1 DownLoadingViewController.h 
  2    
  3 //  DownLoadingViewController.h  
  4 //  DownLoading  
  5 //  
  6 //  Created by skylin zhu on 11-7-30.  
  7 //  Copyright 2011  mysoft. All rights reserved.  
  8 //  
  9    
 10 #import  
 11    
 12 @interface DownLoadingViewController : UIViewController {  
 13     NSURLConnection *connection;   
 14     NSMutableData *connectionData;  
 15 }  
 16 @property (nonatomic,retain) NSURLConnection *connection;    
 17 @property (nonatomic,retain) NSMutableData *connectionData;  
 18 @end  
 19    
 20 DownLoadingViewController.m 
 21    
 22 //  DownLoadingViewController.m  
 23 //  DownLoading  
 24 //  
 25 //  Created by skylin zhu on 11-7-30.  
 26 //  Copyright 2011  mysoft. All rights reserved.  
 27 //  
 28    
 29 #import "DownLoadingViewController.h"  
 30    
 31 @implementation DownLoadingViewController  
 32 @synthesize connection,connectionData;  
 33 - (void)dealloc  
 34 {  
 35     [super dealloc];  
 36 }  
 37    
 38 - (void)didReceiveMemoryWarning  
 39 {  
 40     // Releases the view if it doesn't have a superview.  
 41     [super didReceiveMemoryWarning];  
 42        
 43     // Release any cached data, images, etc that aren't in use.  
 44 }  
 45    
 46 #pragma mark - View lifecycle  
 47    
 48    
 49 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
 50 - (void)viewDidLoad  
 51 {  
 52     [super viewDidLoad];  
 53     //      
 54     NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";  
 55     NSURL    *url = [NSURL URLWithString:urlAsString];  
 56     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
 57     NSMutableData *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[[NSMutableData alloc] init];  
 58     self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/data;  
 59     [data release];  
 60     NSURLConnection *newConnection = [[NSURLConnection alloc]  
 61                                       initWithRequest:request  
 62                                       delegate:self  
 63                                       startImmediately:YES];  
 64     self.connection = newConnection;  
 65     [newConnection release];  
 66     if (self.connection != nil){  
 67        NSLog(@"Successfully created the connection");  
 68     } else {  
 69         NSLog(@"Could not create the connection");  
 70     }  
 71 }  
 72    
 73    
 74    
 75    
 76 - (void) connection:(NSURLConnection *)connection  
 77             didFailWithError:(NSError *)error{  
 78     NSLog(@"An error happened");  
 79     NSLog(@"%@", error);  
 80 }  
 81 - (void) connection:(NSURLConnection *)connection  
 82               didReceiveData:(NSData *)data{  
 83     NSLog(@"Received data");  
 84     [self.connectionData appendData:data];  
 85 }  
 86 - (void) connectionDidFinishLoading  
 87 :(NSURLConnection *)connection{  
 88     /*       */  
 89    
 90         NSLog(@"    ");  
 91         if ([self.connectionData writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {  
 92             NSLog(@"    .");  
 93         }  
 94         else  
 95         {  
 96             NSLog(@"    .");  
 97         }  
 98      
 99     /* do something with the data here */  
100 }  
101 - (void) connection:(NSURLConnection *)connection  
102           didReceiveResponse:(NSURLResponse *)response{  
103     [self.connectionData setLength:0];  
104 }  
105    
106 - (void) viewDidUnload{  
107     [super viewDidUnload];  
108     [self.connection cancel];  
109     self.connection = nil;  
110     self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/nil;  
111 }  
112    
113 @end