iPhone 使用Http下载文件及Protocol的使用
2012-07-17 15:14
260 查看
文章下面有一个Demo,由于本人刚接触iPhone,所学的东西还很少,有错误和不足的地方还请各位大虾,大牛们多多指正。谢谢
本文的有些代码是在网上爬的,参考文章
http://blog.sina.com.cn/s/blog_5ccfd2d50100u04g.html Iphone文件读写操作
/article/2742861.html iphone http下载文件
好了,开始本文的征程吧
1,新建一个protocol文件ShowMsgProtocol.h
2,添加一个HttpDownHelper的类
先看HttpDownHelper.h文件
在看HttpDownHelper.m文件
3,在界面上调用,SecondViewController.h
SecondViewController.m
Ok, Demo下载处: HttpDownDemo
本文的有些代码是在网上爬的,参考文章
http://blog.sina.com.cn/s/blog_5ccfd2d50100u04g.html Iphone文件读写操作
/article/2742861.html iphone http下载文件
好了,开始本文的征程吧
1,新建一个protocol文件ShowMsgProtocol.h
#import <Foundation/Foundation.h> @protocol ShowMsgProtocol <NSObject> -(void)ShowMsg:(NSString *)msg; @end
2,添加一个HttpDownHelper的类
先看HttpDownHelper.h文件
#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import "ShowMsgProtocol.h" @interface HttpDownHelper : NSObject { //url NSString *urlString; //down data NSMutableData *dataNote; NSObject<ShowMsgProtocol> *showMsgDelegate; } @property (nonatomic, retain) NSObject<ShowMsgProtocol> *showMsgDelegate; @property (nonatomic, retain) NSString *urlString; @property (nonatomic, retain) NSMutableData *dataNote; -(void) doDownFile:(NSString *)fileName; @end
在看HttpDownHelper.m文件
#import "HttpDownHelper.h" @implementation HttpDownHelper @synthesize urlString,dataNote; @synthesize showMsgDelegate; -(void) doDownFile:(NSString *)fileName{ urlString = [[NSString alloc]init]; dataNote = [[NSMutableData alloc]init]; urlString = @"http://120.172.58.15:80/test.txt"; NSLog(@"%@",urlString); NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url]; NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self]; [connect release]; [request release]; } //接受数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [self.dataNote appendData:data]; } //接受数据完成 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ //获取路径 //参数NSDocumentDirectory要获取哪种路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //除去需要的路径 NSString *documentDirectory = [paths objectAtIndex:0]; //更改到带操作的目录下 NSString *path = [documentDirectory stringByAppendingPathComponent:@"test.txt"]; NSLog(@"Path == %@",path); NSLog(@"dataNote:%@",[[NSString alloc]initWithData:dataNote encoding:NSUTF8StringEncoding]); [[NSFileManager defaultManager]createFileAtPath:path contents:dataNote attributes:nil]; //将下载到的数据写入文件 [dataNote writeToFile:path atomically:YES]; NSData *reader = [NSData dataWithContentsOfFile:path]; NSLog(@"length == %d",[reader length]); //读取刚才写入的文件内容 NSString *gData2 = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [reader length])] encoding:NSUTF8StringEncoding]; NSLog(@"gData2 == %@",gData2); [showMsgDelegate ShowMsg:gData2];//调用协议 // lblText.text = gData2; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [errorAlert show]; [error release]; } @end
3,在界面上调用,SecondViewController.h
#import <UIKit/UIKit.h> #import "showMsgProtocol.h" #import "HttpDownHelper.h" @interface SecondViewController : UIViewController<ShowMsgProtocol> - (IBAction)btnDown:(id)sender; @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
相关文章推荐
- android开发步步为营之67:使用android开源项目android-async-http异步下载文件
- iphone http下载文件
- 使用一般处理程序HTTPHandler下载文件
- windows winhttp的编程例子,但是头文件和库文件,我的机子环境上没有,只有下载了头文件和库文件后才可以使用。
- 使用java实现http多线程断点下载文件(一)
- 使用HttpURLConnection下载文件
- 使用CURL实现http文件下载加一个多任务的封装.
- 一个简单的使用wininet的http/ftp文件下载程序
- 开源安卓Http文件下载框架file-downloader的使用
- 安卓使用http下载文件
- C#中网页文件下载——HttpWebRequest与HttpWebResponse的使用方法
- iphone开发--使用ASIHTTPRequest实现下载,断点续传功能
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- 使用 HttpWebRequest 下载任意类型的文件
- 使用URLDownloadToFile http 下载文件
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- libcurl使用HTTP的get请求来下载文件
- Flex与.NET互操作 使用FileReference+HttpHandler实现文件上传/下载
- 使用HttpDownLoadHelper下载文件