iPhone开发使用NSURLConnection 的小实例
2014-10-15 14:35
309 查看
1.在实现下载的类中,需要实现协议:NSURLConnectionDataDelegate ,并且要有必要的请求链接;需要定义的属性:
(必须)
NSMutableData *ReceiveData;
NSURLConnection *Connection;
NSURLRequest *Request;
(可选)
NSString *httpUrl;(自定义内容)
NSString *fileName;(自定义内容)
2.在类的实现中:
ReceiveData=[NSMutableData
dataWithCapacity:0];
ReceiveData.length=0;
NSURL *url=[NSURL
URLWithString:[httpUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Request=[NSURLRequest
requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
Connection=[[NSURLConnection
alloc]initWithRequest:Request
delegate:self];//执行到此时,实现连接后,会自动调用NSURLConnectionDataDelegate中的方法,如下:
3.
//连接响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
fileName = [response suggestedFilename];//若如此做,会自动将文件在网络中的名称自动命名为下载完成后的文件名。
}
//接受数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[ReceiveData appendData:data];//数据累加
NSLog(@"%d",data.length);
}
//接受数据完成后
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *savePath=[NSString
stringWithFormat:@"%@/%@.jpg",[paths
objectAtIndex:0],fileName];//获得保存路径
NSLog(@"%@",[paths
objectAtIndex:0]);
[ReceiveData
writeToFile:savePath atomically:YES];//写入文件
}
(必须)
NSMutableData *ReceiveData;
NSURLConnection *Connection;
NSURLRequest *Request;
(可选)
NSString *httpUrl;(自定义内容)
NSString *fileName;(自定义内容)
2.在类的实现中:
ReceiveData=[NSMutableData
dataWithCapacity:0];
ReceiveData.length=0;
NSURL *url=[NSURL
URLWithString:[httpUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Request=[NSURLRequest
requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
Connection=[[NSURLConnection
alloc]initWithRequest:Request
delegate:self];//执行到此时,实现连接后,会自动调用NSURLConnectionDataDelegate中的方法,如下:
3.
//连接响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
fileName = [response suggestedFilename];//若如此做,会自动将文件在网络中的名称自动命名为下载完成后的文件名。
}
//接受数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[ReceiveData appendData:data];//数据累加
NSLog(@"%d",data.length);
}
//接受数据完成后
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *savePath=[NSString
stringWithFormat:@"%@/%@.jpg",[paths
objectAtIndex:0],fileName];//获得保存路径
NSLog(@"%@",[paths
objectAtIndex:0]);
[ReceiveData
writeToFile:savePath atomically:YES];//写入文件
}
相关文章推荐
- IPhone 网络开发之如何使用NSURLConnection(一)
- IPhone 网络开发之如何使用NSURLConnection(一)
- iPhone网络开发之如何使用NSURLConnection
- iOS开发人员必备App开发工具 ifunbox 支持iPhone, iPad和iPod Touch的文件及应用管理神器 使用实例
- IPhone 网络开发之如何使用NSURLConnection(一)
- IPhone 网络开发之如何使用NSURLConnection(一)
- iPhone网络开发之如何使用NSURLConnection(一)
- 更新完畢[轉]31個iPhone软件开发实例教程, 此系列教程幾乎全部使用Utility模板
- iPhone网络开发之如何使用NSURLConnection
- iPhone网络开发之如何使用NSURLConnection
- iPhone网络开发之如何使用NSURLConnection
- iPhone网络开发之如何使用NSURLConnection
- iPhone网络开发之如何使用NSURLConnection(一)
- iPhone网络开发之如何使用NSURLConnection
- iPhone网络开发之如何使用NSURLConnection
- iphone开发实例 SQLite数据库使用
- iPhone网络开发之如何使用NSURLConnection
- 使用ASP.Net 3.5 的Ajax与Web服务开发实例
- 使用Axis开发Web Service实例
- 使用 Ruby on Rails 和 Eclipse 开发 iPhone 应用程序之 2 :向客户端显示 iPhone 内容