他のクラスのすべての 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」は呼び出されません。何が問題なのかについてのアイデアはありますか?