我试图使用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];