如何在我的webservices中显示加载视图控制器 [英] How to show loading view controller in my webservices

查看:77
本文介绍了如何在我的webservices中显示加载视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在调用loginUserintoserver方法时显示加载视图控制器或活动指示器视图,但在调试时我发现该视图在此行变为非活动状态。

I want to show a loading view controller or activity indicator view when I call my loginUserintoserver method but while debugging I found the view becomes inactive at this line.

 NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 

有一段时间我得到回应。我试过显示活动指标,但没有成功。请解决此问题的任何指导原则。提前致谢。

For sometime till I get the response. I have tried showing activity indicators but did not succeed. Please any guidelines to resolve this. Thanks in advance.

-(void) loginUserintoserver

{

NSString *str_validateURL = @"callogin";

// em,password,devicereg,devicetype,flag =(e或m)

// em,password,devicereg,devicetype,flag = ("e" or "m")

NSString *str_completeURL = [NSString stringWithFormat:@"%@%@", str_global_domain, str_validateURL];
NSURL *url = [NSURL URLWithString:str_completeURL];


NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

str_global_UserPhone = [NSString stringWithFormat:@"%@%@",signupCountryCodeTextField.text,signupMobileTextField.text];

NSString *postData = [NSString stringWithFormat:@"em=%@&password=%@&devicereg=%@&devicetype=%@&flag=%@", loginEmailTextField.text, loginPasswordTextField.text, str_global_DeviceRegID, @"1", [NSString stringWithFormat:@"%@", emailphoneFlag]];

NSLog(@"==============%@",postData);
NSString *length = [NSString stringWithFormat:@"%d", [postData length]];

[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];

// here the view becomes inactive and takes time to get response from server

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSLog(@"response data is %@", responseData);

if (responseData == nil)
{
    NSLog(@"No data from server");

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"No data downloaded from server!"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
    [alertView show];

}
else
{
    NSLog(@"response data is %@", responseData);
    NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"returnString....%@",returnString);

    NSDictionary *response_dic = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

    NSString *msg;
    msg = [response_dic objectForKey:@"Result"];
    NSDictionary *loginDict=[[NSDictionary alloc]init];
    loginDict=[response_dic objectForKey:@"Result"];
    NSLog(@"msg is  : %@ ",[response_dic objectForKey:@"Result"]);

    if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"0"])
    {
        // success
        NSLog(@"Successfull Login!!!!!");

        NSString *UserId=[loginDict objectForKey:@"userid"];
        [[NSUserDefaults standardUserDefaults] setValue:UserId  forKey:@"LoginId"];

        [self initRevealViewController];


    } else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"1"]){
        NSLog(@"Invalid Password!");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"ReEnter Password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];


    }else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"3"]){
        NSLog(@"Invalid input parameters!");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Email address or Mobile number, Password, devicereg, devicetype, flag are Mandatory" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];


    }else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"6"]){
        NSLog(@"Invalid input parameters!");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"User Registered but not activated" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }
}
}


推荐答案

大多数答案都是部分答案,并未指出代码中的主要问题。

Most of the answers are partial which do not point out the main issue in your code.

主线程上的同步请求将阻止它,因此在同步请求正在进行时,您无法显示任何UI更新,如活动指示器。而是使用GCD创建异步请求(或在后台发出请求)并使用 UIActivityIndi​​catorView 或任何其他可用的开源。

Synchronous request on main thread will block it, thus you cannot show any UI updates like activity indicator while the sync request is in progress. Instead make asynchronous request (or make request in background using GCD) and use UIActivityIndicatorView or any other open source available for this.

按照此问答语言了解如何使用GCD发出asyn请求: NSURLConnection and grand central发送

Follow this Q&A to learn how to make asyn request using GCD: NSURLConnection and grand central dispatch

您可以在视图中创建并添加活动指示符。在请求启动时显示并开始显示活动,并在请求完成下载时停止活动。

You can create and add an activity indicator to the view. Present and start showing activity when request is initiated and stop the activity when request completes downloading.

希望有所帮助!

这篇关于如何在我的webservices中显示加载视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆