gpt4 book ai didi

ios - 如何使用事件指示器进行 Web 服务登录

转载 作者:行者123 更新时间:2023-11-29 02:19:42 27 4
gpt4 key购买 nike

我刚刚创建了一个使用 Web 服务的 iOS 应用程序,我的问题是当我点击登录时, View Controller 处于空闲状态,不显示事件指示器,但是当从 Web 服务获取响应和数据时,下一个 View Controller 是加载;事件指示器很快出现。我希望在点击登录按钮时可以看到事件指示器


代码示例:

NSString *username = TextUsername.text; 
NSString *password = Textpassword.text;
NSString *strLogInUrl=[NSString stringWithFormat:@""];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSLog(@"Url %@",strLogInUrl);
NSURL *URLGet = [NSURL URLWithString:strLogInUrl];
NSData *data = [NSData dataWithContentsOfURL:URLGet];
NSError* error;
DicForAllData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&error];

最佳答案

正如其他人所指出的,要让 UI 更新,您需要异步执行此操作。在我向您介绍一些细节之前,让我们考虑一下您问题中代码的高级抽象:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSData *data = [NSData dataWithContentsOfURL:URLGet];
// do something with data
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

您确实想要发出异步请求,而不是同步请求,如下所示:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSMutableURLRequest *request = ...;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// handle the data or connectionError here, inside this block, and remember to turn off activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}];

问题是,您的代码片段没有正确创建请求。你把这个过程过于简单化了。您的流程确实还应该考虑:

  1. 如果您要提供凭据,那通常是 POST 请求,而不是 dataWithContentsOfURL 执行的简单 GET

  2. 您确实应该构造一个正确的 application/x-www-form-urlencoded 请求。

  3. 您必须对传递给服务器的数据进行百分比转义(否则,如果密码恰好包含某些保留字符,例如 +&,登录会失败)。

  4. 您确实应该处理错误。

将所有这些放在一起,看起来更像是:

// tell the user we're going to do this request for them
// maybe show a `UIActivityIndicatorView`, too

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

// construct the body of the request; replace the keys `username` and `password` with whatever your web service requires

NSString *username = textUsername.text;
NSString *password = textPassword.text;
NSString *parameterString = [NSString stringWithFormat:@"username=%@&password=%@", [self percentEscapeString:username], [self percentEscapeString:password]];
NSData *httpBody = [parameterString dataUsingEncoding:NSUTF8StringEncoding];

// now create the request itself

NSString *strLogInUrl = @"http://example.com";
NSURL *URLGet = [NSURL URLWithString:strLogInUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URLGet];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:httpBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

// now send the request

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!data) {
NSLog(@"connectionError: %@", connectionError);
} else {
NSError* error;
dicForAllData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

// do what you want with the data/error here, e.g. `[self.tableView reloadData]` or whatever
}

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}];

并且假设您有一定比例的转义例程,例如:

- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

早些时候,UnicoRahul 询问您是否在使用 AFNetworking。这是值得考虑的,因为它极大地简化了您必须编写的代码:

NSDictionary *params = @{@"username": username, @"password": password};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager POST:@"http://example.com" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
dicForAllData = responseObject;

// do what you want with the data here, e.g. `[self.tableView reloadData]` or whatever
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];

关于ios - 如何使用事件指示器进行 Web 服务登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28298772/

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