我在使用 NSURLConnection 时遇到了一些非常奇怪的问题,所以我希望你能帮助我。
我想做的是使用 NSURLConnection 从给定的 URL 下载一些数据。
我创建了自己的帮助类,它接收数据路径,下载它,并在下载完成时通过委托通知调用者。
这东西在我的装有 iOS 4.3 的 iPhone 上完美运行。但是,在 iOS 5 或 iOS6 上进行测试时,该connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
方法从未被调用,我也没有得到想要的结果。
类 .h 文件包含:
#import <Foundation/Foundation.h>
@protocol NIAsyncDownloaderDelegate
@required
- (void) asyncDownloaderDataDownloadComplete:(NSData *)data withError:(bool) error;
@end
@interface NIAsyncImageDownloader : NSObject <NSURLConnectionDataDelegate>
{
NSURLConnection *theConnection;
NSMutableData* myData;
NSURL *downloadURL;
id delegate;
}
-(id) initWithDataDownloadString:(NSString *) stringAddress;
@property (nonatomic, retain) id delegate;
@end
.m 文件如下所示:
#import "NIAsyncDownloader.h"
@implementation NIAsyncImageDownloader
@synthesize delegate;
-(id) initWithDataDownloadString:(NSString *)stringAddress
{
if (self = [super init])
{
[self loadDataFromURL:[NSURL URLWithString:stringAddress]];
}
return self;
}
- (void)loadDataFromURL:(NSURL*)url
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
downloadURL = url;
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
NSLog(@"The response is: %@, status code %i, url %@", response.description, ((NSHTTPURLResponse*)response).statusCode, ((NSHTTPURLResponse*)response).URL.description);
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
if (myData == nil)
{
myData = [[NSMutableData alloc] initWithCapacity:2048];
}
[myData appendData:data];
}
//CALLED ON iOS 4.3
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
//so self data now has the complete image
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
[self handleDownloadSuccess];
}
//CALLED ON iOS 5, iOS 6
-(void) connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
[self handleDownloadSuccess];
}
-(void) handleDownloadSuccess
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
[theConnection release];
theConnection = nil;
[delegate asyncDownloaderDataDownloadComplete:myData withError:NO];
[myData release];
myData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Called: %@", NSStringFromSelector(_cmd));
[delegate asyncDownloaderDataDownloadComplete:nil withError:YES];
}
@end
以下是一些屏幕截图,向您展示我在说什么:
这就是我在 iOS5 或 iOS6 上运行应用程序时发生的情况,请求会自行初始化,收到响应并connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
立即
调用
但是,当我在 iOS 4.3 上运行相同的应用程序时,一切正常,正如您从下面的屏幕截图中看到的那样:
我还注意到 iOS 5 和 iOS 6 不像 iOS 4.3 那样调用相同的“完成”方法,但我认为这与我当前的问题没有任何关系。
最后,这里的文档说,从 iOS 4.3 开始,有问题的方法 (connection:didReceiveData) 实际上已被弃用:http: //developer.apple.com/library/ios/#documentation/Foundation/Reference/ NSURLConnectionDelegate_Protocol/DeprecationAppendix/AppendixADeprecatedAPI.html
但是,另一个参考指出它是 NSURLConnectionDataDelegate 协议的一部分,并且从 iOS 2 开始可用:http: //developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html
XCode 似乎同意它已被弃用:
万一有人想知道我是如何使用下载器的,这真的很简单:
在.h中:
#import <UIKit/UIKit.h>
#import "NIAsyncDownloader.h"
@interface DTViewController : UIViewController <NIAsyncDownloaderDelegate>
{
NIAsyncImageDownloader *downloader;
}
@end
在 .m 中:
#import "DTViewController.h"
@interface DTViewController ()
@end
@implementation DTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
downloader = [[NIAsyncImageDownloader alloc] initWithDataDownloadString:@"http://www.freeimageslive.com/galleries/sports/sportsgames/pics/whitedice1.jpg"];
downloader.delegate = self;
}
-(void) asyncDownloaderDataDownloadComplete:(NSData *)data withError:(bool)error
{
if (data == nil || error)
{
NSLog(@"DOWNLOAD FAILED");
}
else
{
NSLog(@"DOWNLOAD SUCCEEDED");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
所以,总结一下,为什么我会遇到问题,该怎么办?
提前致谢 :)