NSURLConnection在iOS 4.3中完美运行,但在iOS 5 / iOS 6中运行不佳

时间:2013-06-02 16:02:09

标签: ios objective-c nsurlconnection

我在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 ios5 ios6

然而,当我在iOS 4.3上运行相同的应用程序时,一切都很完美,如下面的屏幕截图所示:

ios4.3 1 iOS4.3 2

我还注意到iOS 5和iOS 6没有像iOS 4.3那样调用相同的“完成”方法,但我认为这与我当前的问题没有任何关系。

最后,这里的文档说明有问题的方法(connection:didReceiveData)从iOS 4.3开始实际上已被弃用: 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似乎同意它已被弃用:

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

所以,总结一下,为什么我有问题和做什么?

提前致谢:)

1 个答案:

答案 0 :(得分:1)

这可能是也可能不是问题,但是当我设置做类似的事情时,我实现了NSURLConnectionDelegate和NSURLConnectionDataDelegate协议。我还没有在5之前的iOS版本上进行过测试,但它对我来说在5和6都有效:

@interface NIAsyncImageDownloader : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate> {

}