我想创建一个类来处理其他类的所有http连接工作(以避免重复编写代码)。
我将其称为connectioncenter(nsobject的子类),并向其添加以下代码:
-(void)connect:(NSString *)strURL obj:(ConnectCenter *)objConnect
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:objConnect];
if (theConnection)
{
// receivedData is declared as a method instance elsewhere
receivedData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[receivedData appendData:data];
}
其他类通过传递url和connectioncenter对象来调用它。但未调用connectionCenter中的“didReceiveData”方法。有什么问题吗?
最佳答案
在设置连接之后,您需要调用[theConnection setDelegate:self]
,因为connection:didReceiveData:
是一个委托方法。
阅读documentation了解更多信息。