【发布时间】:2014-02-13 11:26:00
【问题描述】:
-(IBAction)onButtonClick:(id)sender
{
DowloadFilesManager* downManager1=[[DowloadFilesManager alloc] init];
DowloadFilesManager* downManager2=[[DowloadFilesManager alloc] init];
DowloadFilesManager* downManager3=[[DowloadFilesManager alloc] init];
[downManager1 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat.jpg"];
[downManager2 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat1.jpg"];
[downManager3 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat2.jpg"];
NSLog(@"Finished Succesfully");
}
1)以上代码完美运行
2)它在触发以下事件函数后下载jpgs。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[receivedData writeToFile:toFile atomically:YES];
theConnection = nil;
receivedData = nil;
}
但是当我在导入“DowloadFilesManager.h”和“DowloadFilesManager.m”后尝试另一个程序时,它不会触发任何事件方法,这两个程序在同一台 Mac 机器上的同一 XCode 上运行,服务器 URL 可达。任何人都可以提出解决方案?我错过了什么吗?我试过清洁等......但没有用。以下是我使用的 DowloadFilesManager 类:
#import "DowloadFilesManager.h"
@implementation DowloadFilesManager
@synthesize toFile;
@synthesize toURL;
@synthesize theConnection;
-(void) downloadURL:(NSString *) urlStr destPath:(NSString *) destPath
{
toURL=urlStr;
toFile=destPath;
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:toURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
receivedData = [[NSMutableData alloc] init];
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
// [receivedData dataWithCapacity: 0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection) {
// Release the receivedData object.
receivedData = nil;
// Inform the user that the connection failed.
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
theConnection = nil;
receivedData = nil;
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[receivedData writeToFile:toFile atomically:YES];
theConnection = nil;
receivedData = nil;
}
@end
【问题讨论】:
标签: ios nsurl nsurlrequest