ios實現登陸驗證
我們可以使用第三架構AFNetworking,用它的post方法進行與伺服器進行通訊。若伺服器返回的userId為10則成功,返回的為11則表示使用者名稱或者密碼錯誤。
-(void)login:(id)sender{ AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager]; NSString *url=@"http://172.18.144.248:8080/upload_file_service/LoginServlet"; manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",nil]; NSDictionary *namepwd=@{@"user":@"admin",@"pass":@"123"}; [manager POST:url parameters:namepwd success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"post-->%@",responseObject); NSDictionary *datare=[[NSDictionary alloc] init]; datare=responseObject; re=[datare objectForKey:@"userId"]; appStatus *transvalue=[appStatus shareInstance]; mainViewController *mainView=[self.storyboard instantiateViewControllerWithIdentifier:@"second"]; mainView.delegate=self; transvalue.contextStr=__name.text; NSNumber *fre=[[NSNumber alloc]initWithInt:10]; BOOL flag=[fre isEqualToNumber:re]; if(flag){ [self presentViewController:mainView animated:YES completion:nil]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@",@”使用者名稱或者密碼錯誤”); }]; }