博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iPhone 使用Http下载文件及Protocol的使用
阅读量:6906 次
发布时间:2019-06-27

本文共 3809 字,大约阅读时间需要 12 分钟。

文章下面有一个Demo,由于本人刚接触iPhone,所学的东西还很少,有错误和不足的地方还请各位大虾,大牛们多多指正。谢谢

本文的有些代码是在网上爬的,参考文章
  Iphone文件读写操作
 

好了,开始本文的征程吧

1,新建一个protocol文件ShowMsgProtocol.h

1 #import 
2 3 @protocol ShowMsgProtocol
4 5 -(void)ShowMsg:(NSString *)msg;6 7 @end

2,添加一个HttpDownHelper的类

  先看HttpDownHelper.h文件

1 #import 
2 #import
3 #import "ShowMsgProtocol.h" 4 @interface HttpDownHelper : NSObject 5 { 6 //url 7 NSString *urlString; 8 //down data 9 NSMutableData *dataNote;10 NSObject
*showMsgDelegate;11 }12 13 @property (nonatomic, retain) NSObject
*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 
2 #import "showMsgProtocol.h"3 #import "HttpDownHelper.h"4 5 @interface SecondViewController : UIViewController
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下载处

转载地址:http://alrdl.baihongyu.com/

你可能感兴趣的文章
【第40题】2019年OCP认证12C题库062考试最新考试原题
查看>>
怎样在linux下查找一个结构体的原始定义
查看>>
OpenApi开源项目以及总结
查看>>
UNIX-LINUX C语言编程->实验室->多次打开文件实验
查看>>
8.8
查看>>
http://www.cnblogs.com/kenshincui/p/3840294.html
查看>>
更改SQL SERVER默认端口
查看>>
ajax的traditional属性
查看>>
网络安全重点总结
查看>>
git &github 快速入门
查看>>
windows下mysql忘记root密码的解决方法
查看>>
hdu 1907 John / 2509 Be the Winner 博弈 最后取完者为输
查看>>
原来生活如此精彩
查看>>
〖Linux〗Ubuntu13.04 安装KCOPSE过程笔录
查看>>
C/C++ %s %d %u 基本概念与用法
查看>>
2012.7.18工作总结
查看>>
elasticsearch 2.4.0安装说明
查看>>
Java泛型:泛型的定义(类、接口、对象)、使用、继承
查看>>
正则表达式匹配
查看>>
C#向excel中写入数据的三种方式
查看>>