[英]NSURLConnection with ARC not receiving Data
我是 iOS 开发的新手,我正在尝试从 URL 获取内容。我正在使用 Apple 教程来使用 NSURLConnection,一切正常,但我没有获取数据。 我环顾四周,找不到我的问题的答案。 我也在我的项目中使用 ARC,也许这会导致问题?
这是我正在使用的代码,从我的 header 文件开始:
@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 个字节。 我找不到我的问题的答案,所以我希望我能找到这个问题的答案。
我已经翻译了URL Loading System Programming Guide using ARC 的Using 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
只是为了完成。 我遇到了同样的问题,原因是threading 。 如果拥有NSURLConnection
object 的线程与拥有delegate
的线程不同, delegate
将不会收到通知。 这意味着连接错误也将消失。
因此,请确保在创建委托的同一线程上创建NSURLConnection
。
您需要启动连接。
例如,您可以使用– initWithRequest:delegate:startImmediately:
。
对现有代码的一个更小的更改就是使用- start
if 分支上的连接开始:
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 的参考手册
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.