我目前正在开发一个使用基本登录页面来检查用户是否有权访问该应用程序的应用程序。我在本教程的帮助下制作了登录部分,该教程使用此框架从一个简单的 Web 脚本中使用用户名和密码。
我希望其他人可能已经使用它或者可以帮助我解决我的问题。我不想显示活动指示器,我使用MBProgressHUD作为活动指示器。
因此,我已经对其进行了试验,但无法在应用程序连接到 URL 时显示活动指示器。我用登录过程模拟了一些错误的连接,但是当应用程序连接到 URL 时,活动指示器不会显示。它只显示错误,唯一显示任何类型加载成功的是登录按钮按下状态为“活动”(蓝色突出显示),直到加载完成。
所以这是我的代码,当用户输入用户名和密码并点击登录按钮时运行:
// Login button
- (IBAction)loginBtnClicked:(id)sender
{
// Show the activity indicator
[HUD showUIBlockingIndicatorWithText:@"Loggar in..."];
@try {
if([[userNameTxtField text] isEqualToString:@""] || [[passwordTxtField text] isEqualToString:@""] ) {
// No username or password entered
[self alertStatus:@"Du måste ange användarnamn och lösenord" :@"Något gick fel!"];
// Hide activity indicator
[HUD hideUIBlockingIndicator];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[userNameTxtField text],[passwordTxtField text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://www.nebulon.se/json/sendus/jsonlogin.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300){
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%d",success);
if(success == 1){
// Login success, grant user access to app
NSLog(@"Login SUCCESS");
[self loginSuccess];
// Hide activity indicator
[HUD hideUIBlockingIndicator];
// Store username
NSString *userName = [userNameTxtField text];
NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
[UserDefaults setObject:userName forKey:@"userName"];
[UserDefaults synchronize];
[self dismissViewControllerAnimated:NO completion:nil];
} else {
// Login error
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Inloggningen misslyckades"];
[self loginFailed];
// Hide activity indicator
[HUD hideUIBlockingIndicator];
}
} else {
// Login error
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Ingen nätverksuppkoppling hittades." :@"Ett fel har inträffat!"];
[self loginFailed];
// Hide activity indicator
[HUD hideUIBlockingIndicator];
}
}
}
@catch (NSException * e) {
// Login error
NSLog(@"Exception: %@", e);
[self alertStatus:@"Inloggningen misslyckades." :@"Ett fel har inträffat!"];
[self loginFailed];
// Hide activity indicator
[HUD hideUIBlockingIndicator];
}
}