Objective c 使用POST方法时,iphone MBProgreeHUD不工作
当我试图将一些图像或文本从应用程序发布到服务器时,这种情况是不起作用的。但没有张贴方法,它的工作是完美的。我使用下面的代码。请任何人帮助我。谢谢Objective c 使用POST方法时,iphone MBProgreeHUD不工作,objective-c,iphone,ios7,xcode5,Objective C,Iphone,Ios7,Xcode5,当我试图将一些图像或文本从应用程序发布到服务器时,这种情况是不起作用的。但没有张贴方法,它的工作是完美的。我使用下面的代码。请任何人帮助我。谢谢 HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.dimBackground = YES; HUD.delegate = self; NSString *urlRequest =[NSString stringWithFormat:@"URL"]; NSString *p
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.delegate = self;
NSString *urlRequest =[NSString stringWithFormat:@"URL"];
NSString *pStrLegalURLString =[urlRequestn stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:pStrLegalURLString];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setHTTPMethod:@"POST"];
NSData *returnData1 = [ NSURLConnection sendSynchronousRequest: request1 returningResponse: nil error: nil ];
NSString *returnString1 = [[NSString alloc] initWithData:returnData1 encoding: NSUTF8StringEncoding];
试着这样做:
它可以工作,但我想您的问题是因为sendSynchronousRequest
是一个阻塞调用,因此如果主线程挂起,您将不会在UI中看到任何更新。所以试着在后台打这样的电话
尝试我看到的两种方法之一:
方法1:(使用选择器在后台发送请求,并在主线程中进行UI更新)
方法2:使用Dispatch\u Async和Dispatch\u Sync
谢谢,现在工作很好。sendSynchronousRequest,这是一个阻止调用。
-(void)method1 {
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.delegate = self;
NSString *urlRequest =[NSString stringWithFormat:@"URL"];
NSString *pStrLegalURLString =[urlRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:pStrLegalURLString];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setHTTPMethod:@"POST"];
// send request in background.
[self performSelectorInBackground:@selector(method2) withObject:nil];
}
-(void)method2 {
NSData *returnData1 = [ NSURLConnection sendSynchronousRequest: request1 returningResponse: nil error: nil ];
NSString *returnString1 = [[NSString alloc] initWithData:returnData1 encoding: NSUTF8StringEncoding];
[self performSelectorOnMainThread:@selector(method3) withObject:nil waitUntilDone:NO];
}
-(void)method3
{
// update ui in main thread.
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
// Do background work here
dispatch_sync(dispatch_get_main_queue(), ^(void){
// Update UI here
});
});