Asp.net iOS:如何执行NSURLConnection并在继续之前获得repsonse?
我正在尝试从我的服务器获取更新。我希望它有最新的信息,所以我正在运行一个页面,如果响应是Asp.net iOS:如何执行NSURLConnection并在继续之前获得repsonse?,asp.net,ios,xml,ios5,nsurlconnection,Asp.net,Ios,Xml,Ios5,Nsurlconnection,我正在尝试从我的服务器获取更新。我希望它有最新的信息,所以我正在运行一个页面,如果响应是“Complete”,而不是“Error”,那么我将继续使用更新方法从xml文件中获取最新的信息。我的问题是,在“Main”函数中的所有代码完成之前,不会调用委托方法。到那时,代码已经通过了我的if语句,检查responseSuccess是TRUE还是FALSE。我想这是因为NSURLConnection是异步的。。。但我不知道如何修复它。如果我需要提供更多代码/信息,请告诉我。谢谢 Main if
“Complete”
,而不是“Error”
,那么我将继续使用更新方法从xml
文件中获取最新的信息。我的问题是,在“Main”函数中的所有代码完成之前,不会调用委托方法。到那时,代码已经通过了我的if语句,检查responseSuccess
是TRUE
还是FALSE
。我想这是因为NSURLConnection是异步的。。。但我不知道如何修复它。如果我需要提供更多代码/信息,请告诉我。谢谢
Main
if (UpdatingFlag)
{
NSLog(@"Cannot update while updating is in process...");
} else {
// run updates before getting information
[self getResponse:@"http://url/path/to/file?function=myvalue"];
[self setBottomBarToUpdating:@"Processing Please Wait..."];
dispatch_queue_t queue = dispatch_queue_create("updateQueue", DISPATCH_QUEUE_CONCURRENT);
UpdatingFlag = TRUE;
if(responseSuccess)
{
dispatch_async(dispatch_get_main_queue(),^ {
[self setBottomBarToUpdating:@"Updating..."];
[self updateFromXMLFile:@"https://url/path/to/file.xml"];
});
}
UpdatingFlag = FALSE;
dispatch_barrier_async(queue,^ {
dispatch_async(dispatch_get_main_queue(), ^{
[self setBottomBarToUpdated];
});
});
}
获取响应方法
- (void) getResponse:(NSString *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:45.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
if (connection) {NSLog(@"Connecting...");
} else {NSLog(@"Didn't Connect Error...");}
}
委托方法
#pragma mark NSURLConnection methods
- (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"Did Receive Challenge");
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Invalid Username and Password");
UIAlertView * userNameAlert = [[UIAlertView alloc]initWithTitle:@"Error"
message:@"ErrorMsg"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[userNameAlert show];
}
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data {
NSLog(@"Received Data Packet...");
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error, %@", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
NSLog(@"Finished Loading");
if([response rangeOfString:@"Complete"].location == NSNotFound) {
// success
responseSuccess = FALSE;
} else {
// failed
responseSuccess = TRUE;
}
}
- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return YES;
}
在从服务器获取更新后要运行的任何代码都需要移动到(或从)connectionIDFinishLoading:方法。因此,您可以去掉这些标志,只在主代码中调用getResponse