gpt4 book ai didi

ios - json解析中事件指示器的使用

转载 作者:行者123 更新时间:2023-11-29 00:57:30 25 4
gpt4 key购买 nike

我正在处理应用程序,并且在登录过程中我必须使用 activityindicator,但我没有在下面的代码中使用 activityindicator 的代码:-

- (IBAction)Login:(id)sender
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:BaseUrl@"login"]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

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


[request addValue:@"*/*" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];

NSString *mapData = [NSString stringWithFormat:@"userName=g&userPassword=123456&api_key=ZWZ&api_password=1" ];

NSData *postData = [mapData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if(error == nil)
{

NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"text= %@",text);

NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if(error!=nil)
{
NSLog(@"error = %@",error);

}

dispatch_async(dispatch_get_main_queue(), ^{
[self checkUserSuccessfulLogin:json];
});
}
else{

NSLog(@"Error : %@",error.description);

}


}];


[postDataTask resume];




}

登录后转到其他页面需要时间。请帮助我。

最佳答案

使用下面的代码

//main thread
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/2);

//if you want to add to window, use this below one
[appdelegate.window addSubview:spinner];

//or if you want to add to view, use below one
[self.view addSubView:spinner];

[spinner startAnimating];


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),^{

dispatch_async(dispatch_get_main_queue(), ^{

//after your server call or parsing or something you can call this to stop animating

[spinner stopAnimating];
});
});

关于ios - json解析中事件指示器的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37476682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com