2

我对iOS开发相当新,我试图从URL中获取内容。我使用Apple教程使用NSURLConnection,一切正常,但我没有获取数据。我环顾四周,无法找到我的问题的答案。我也在我的项目中使用ARC,可能会导致问题?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正在努力,但它口口声声说receivedData有0字节。我找不到我的问题的答案,所以我希望我能找到我的答案与这个问题。

+0

ARC不应该用于自动引用计数,请阅读标记wiki。 – 2012-04-10 21:14:33

+0

对不起, – 2012-04-11 11:56:25

+0

Lars,对于ARC代码,你的'@ property'应该使用'strong'而不是'retain'。另外,为什么在'-connection:didReceiveData:'方法中没有记录语句?安德鲁 – adonoho 2012-04-11 13:30:17

回答

-1

您需要开始连接。例如,可以使用– initWithRequest:delegate:startImmediately:

的更小,以现有的代码变化将只使用- 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

+0

不,这是不正确的,没有必要调用开始。正如您链接到的手册中所述:“这相当于调用initWithRequest:delegate:startImmediately:并立即传递YES以启动。” – 2012-06-26 21:20:25

+0

我同意,当且仅当您使用初始化程序的长版本并立即传递YES时,不需要调用start。为了对现有代码做一些小改动,我提供了第二个选项。事实上,我在这个例子中有我的错字。编辑修复它。 – 2012-06-27 07:48:22

+0

没有我说的是,当你使用'[[NSURLConnection alloc] initWithRequest:request delegate:self];'因为他是,没有必要调用'start'。使用较短的初始化程序自动以YES作为startIm立即自动调用较长的版本。我在我的代码中广泛使用NSURLConnection,总是使用较短的初始化器,并且从不调用start。它以这种方式工作,并在您链接到的文档中得到确认。同样按照他的问题,他的代表方法正在被调用,所以这种联系显然是开始的。 – 2012-06-27 19:54:48

0

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

// 
// 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 
0

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

因此,请确保您在创建委托的同一个线程上创建NSURLConnection