ARC未接收数据的NSURLConnection

时间:2012-04-10 09:33:06

标签: ios cocoa-touch automatic-ref-counting nsurlconnection nsurlrequest

我是iOS开发的新手,我正在尝试从URL获取内容。我正在使用Apple教程使用NSURLConnection,一切正常,但我没有得到数据。我环顾四周,无法找到问题的答案。我也在我的项目中使用ARC,也许这会导致问题?

以下是我正在使用的代码,从我的头文件开始:

@interface ViewController : UIViewController
@property (nonatomic, retain) NSMutableData *receivedData;
@end

实施档案:

@implementation ViewController

@synthesize receivedData;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"1. viewDidLoad");
    // Create the request
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    // Create the connection with the request and start loading the data
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        NSLog(@"2. connection succeeded");
        receivedData = [NSMutableData data];
    } else {
        NSLog(@"2. connection failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse     *)response
{
    NSLog(@"3. didReceiveResponse");
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"4. Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"4. Succeeded! Received %d bytes of data", [receivedData length]);
}

所有NSLog都在工作,但它一直说receiveData有0个字节。我无法找到问题的答案,所以我希望我能用这个问题找到答案。

3 个答案:

答案 0 :(得分:0)

我使用ARC翻译了URL Loading System Programming Guide使用NSURLConnection 部分。

//
//  MyConnection.h
//

#import <Foundation/Foundation.h>

@interface MyConnection : NSObject

- (void)start;

@property NSMutableData *receivedData;

@end


//
//  MyConnection.m
//

#import "MyConnection.h"

@implementation MyConnection

- (void)start {

    // Create the request
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    // Create the connection with the request and start loading the data
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMUtableData to fold the received data
        // ReceivedData is an instance variable declared elsewhere
        _receivedData = [NSMutableData data];

    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // Inform the user
    NSLog(@"Connectionfailed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Do something with the data
    NSLog(@"Succeeded! Recevied %d bytes of data", [_receivedData length]);

}

@end

答案 1 :(得分:0)

只是为了完成。我遇到了同样的问题,原因是线程。如果拥有NSURLConnection对象的线程与拥有delegate的线程不同,则delegate将不会收到通知。这意味着连接中的错误也将消失。

因此,请确保在您创建代理的同一个主题上创建NSURLConnection

答案 2 :(得分:-1)

您需要开始连接。

例如,您可以使用– initWithRequest:delegate:startImmediately:

对现有代码进行更小的更改只是在if分支上的连接上使用- start

NSURLConnection *connection = [[NSURLConnection alloc] 
                                initWithRequest:request 
                                       delegate:self 
                               startImmediately:NO
                               ];
if (connection) {
    NSLog(@"2. connection succeeded");
    receivedData = [NSMutableData data];
    [connection start];
} else {
    NSLog(@"2. connection failed");
}

您可以找到NSURLConnection here

的参考手册