iPhone 使用Http下载文件及Protocol的使用

文章下面有一个Demo,由于本人刚接触iPhone,所学的东西还很少,有错误和不足的地方还请各位大虾,大牛们多多指正。谢谢
本文的有些代码是在网上爬的,参考文章
http://blog.sina.com.cn/s/blog_5ccfd2d50100u04g.html  Iphone文件读写操作
http://blog.csdn.net/rhljiayou/article/details/7616365 iphone http下载文件

好了,开始本文的征程吧
1,新建一个protocol文件ShowMsgProtocol.h

1 #import <Foundation/Foundation.h>
2 
3 @protocol ShowMsgProtocol <NSObject>
4 
5 -(void)ShowMsg:(NSString *)msg;
6 
7 @end

2,添加一个HttpDownHelper的类
  先看HttpDownHelper.h文件

 1 #import <UIKit/UIKit.h>
 2 #import <Foundation/Foundation.h>
 3 #import "ShowMsgProtocol.h"
 4 @interface HttpDownHelper : NSObject
 5 {
 6     //url
 7     NSString *urlString;
 8     //down data
 9     NSMutableData  *dataNote;
10     NSObject<ShowMsgProtocol>   *showMsgDelegate;
11 }
12 
13 @property (nonatomic, retain)  NSObject<ShowMsgProtocol> *showMsgDelegate;
14 @property (nonatomic, retain) NSString *urlString;
15 @property (nonatomic, retain) NSMutableData *dataNote;
16 
17 -(void) doDownFile:(NSString *)fileName;
18 
19 @end

  在看HttpDownHelper.m文件

 1 #import "HttpDownHelper.h"
 2 
 3 @implementation HttpDownHelper
 4 
 5 @synthesize urlString,dataNote;
 6 @synthesize showMsgDelegate;
 7 
 8 -(void) doDownFile:(NSString *)fileName{
 9     
10     urlString = [[NSString alloc]init];
11     dataNote = [[NSMutableData alloc]init];
12  urlString = @"http://120.172.58.15:80/test.txt";
13      NSLog(@"%@",urlString);
14      NSURL *url = [NSURL URLWithString:urlString];
15      NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
16      NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self];
17      [connect release];
18      [request release];
19 
20 }
21 
22 //接受数据
23  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
24      [self.dataNote appendData:data];
25  }
26  
27  //接受数据完成
28  -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
29      //获取路径
30      //参数NSDocumentDirectory要获取哪种路径
31      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
32      //除去需要的路径
33      NSString *documentDirectory = [paths objectAtIndex:0];
34      //更改到带操作的目录下
35      NSString *path = [documentDirectory stringByAppendingPathComponent:@"test.txt"];
36      NSLog(@"Path == %@",path);
37      
38      
39      NSLog(@"dataNote:%@",[[NSString alloc]initWithData:dataNote encoding:NSUTF8StringEncoding]);
40      
41      [[NSFileManager defaultManager]createFileAtPath:path contents:dataNote attributes:nil];
42      //将下载到的数据写入文件
43      [dataNote writeToFile:path atomically:YES];
44      
45      NSData *reader = [NSData dataWithContentsOfFile:path];
46      
47      NSLog(@"length == %d",[reader length]);
48      
49      //读取刚才写入的文件内容
50     NSString *gData2 = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [reader length])] encoding:NSUTF8StringEncoding];
51               
52      NSLog(@"gData2 == %@",gData2);
53      [showMsgDelegate ShowMsg:gData2];//调用协议
54     // lblText.text = gData2;
55      
56  }
57  
58  -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
59      UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
60      [errorAlert show];
61      [error release];
62  }
63 @end

3,在界面上调用,SecondViewController.h

1 #import <UIKit/UIKit.h>
2 #import "showMsgProtocol.h"
3 #import "HttpDownHelper.h"
4 
5 @interface SecondViewController : UIViewController<ShowMsgProtocol>
6 - (IBAction)btnDown:(id)sender;
7 
8 @end

  SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController ()

@end
@implementation SecondViewController

-(void)ShowMsg:(NSString *)msg{
    NSLog(@"ShowMsg=%@",msg);
}
-(IBAction)btnDown:(id)sender{
     HttpDownHelper *httpHepler = [[HttpDownHelper alloc]init];
     [httpHelper doDownFile:@"test.txt"];
httpHelper.showMsgDelegate = self; }
@end

 Ok, Demo下载处HttpDownDemo



原文地址:https://www.cnblogs.com/cpcpc/p/2595269.html