URL に接続してその URL のコンテンツ (この場合は単純な xml ドキュメント) を取得する非常に単純なアプリを作成しようとしています。私の問題は、リクエストが送信されないように見えることです。
メインファイルから実行する新しいプロジェクト (Foundation ツール) を作成しました。NSURLConnection のデリゲートを自分自身に設定し、必要なデリゲート メソッドを実装しました。問題は、これらのメソッドが呼び出されないことです。URL を偽の URL に変更しても問題ありません (以下の例のように)。アプリケーションは、応答を送信または待機せずに起動を終了するように思えます。アプリケーションがリクエストのスレッドを作成し、レスポンスを待たずに終了する可能性がありますか?? 「作成された接続」のような NSLOG ステートメントを取得するため、クラスが実行されていることはわかっていますが、デリゲート メソッド内から NSLOG ステートメントはありません。
私は何が欠けていますか?
#import "NetworkController.h"
@implementation NetworkController
- (id)init
{
if(self = [super init])
{
}
return self;
}
- (void)dealloc
{
NSLog(@"Calling dealloc");
[super dealloc];
}
- (void) performConnection
{
NSMutableData *receivedData;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
@"http://www.test.php?some variables"]];
NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[url release];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self startImmediately:YES];
if (!theConnection)
{
NSLog(@"Failed to submit request");
}
if(theConnection)
{
receivedData=[[NSMutableData data] retain];
NSLog(@"Created connection.");
NSLog(@"--------- Request submitted ---------");
NSLog(@"receivedData: %@", receivedData);
}
[connectionRequest release];
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Received response: %@", response);
NSLog(@"Received response, connection retain count is %d",[connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"finished connection retain count: %d", [connection retainCount]);}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error receiving response: %@", error);
[connection release];
}
@end
#import <Foundation/Foundation.h>
#import "NetworkController.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NetworkController *n = [[NetworkController alloc] init];
n.performConnection;
[n release];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}