NSURLConnection のデリゲート メソッドを使用しようとしています。次のメソッドは現在呼び出されていません。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
//I also want to be able to use self-signed https urls
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
//I also want to be able to use self-signed https urls
私は現在同期呼び出しを使用していますが、コード ベースを完成させた後、それを iPhone アプリケーションに実装する予定であり、UI をフリーズさせることができないため、非同期の方が優れているようです。
以下の方法でresponseData = [NSMutableData dataWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];
必要なデータを取得しますが、非同期を使用すると、デリゲート メソッドを使用してデータを取得する必要があるようです。を使用してデリゲートを追加しようとしました@interface myClass : NSObject<NSURLConnectionDelegate>
次のようにメソッドを呼び出しています。
-(void)grabData{
NSArray* array = [NSArray arrayWithObjects:@"auth.login",@"user",@"pass", nil];
NSData* packed_array = [array messagePack];
NSURL* url = [NSURL URLWithString:@"https://192.168.1.115:3790/"];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]initWithURL:url]retain];
[request setHTTPMethod:@"POST"];
[request setValue:@"RPC Server" forHTTPHeaderField:@"Host"];
[request setValue:@"binary/message-pack" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[packed_array length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:packed_array];
//NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSLog(@"connecting");
NSURLConnection* connection = [[[NSURLConnection alloc]initWithRequest:request delegate:self]retain];
if (connection) {
NSLog(@"connection exists");
self.responseData = [[NSMutableData data]retain];
}
else {
NSLog(@"Connection doesn't exist?");
}
NSLog(@"response data: %@",[responseData messagePackParse]);
NSLog(@"error: %@",error);
}
私は次のことを試みました:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];