您的位置:首页 > 理论基础 > 计算机网络

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

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: