用户输入用户名和密码,点击登录。。。我们把用户名和密码(用post方式或者get方式,get方式多用于测试看你需要)传给服务器,服务器进行判断,然后返回一个接口给我们(这里服务器返回的json接口,正确就返回一个正确的接口给我们,错误就返回一个错误的接口给我们)。我们拿到接口去解析,判断是否登录成功做相应的操作。
关于json解析,可以网上找找资料先看看,我就只知道[]是数组,{}是字典,iOS有自带的json解析的。
以前一直说接口文档,接口什么,可自己从来都是模糊的,比如说一个类的接口,我就认为是这个类的方法。。可是,我的boss给我的登录接口的文档的时候,我才知道,哦,接口文档原来是这样子的呀。。嘻嘻。。。
class="dp-objc" start="1">
- [
- {
- "result": {
- "RUrl": "http://localhost/pad_4078_B7DA_676D3FE52763",
- "errMsg": "",
- "result": "OK",
- "sid": "B67D78_B7DA_676D3FE52763"
- }
- }
- ]
-
- ? jsondata[0][‘result’].result:
- ? OK,登录成功,打开RUrl
- ? FAIL,登录失败,取:errMsg
(1)
- #import <UIKit/UIKit.h>
- #import "ByValueUrl.h"
- @interface LogInViewController : UIViewController <UITextFieldDelegate>{
-
- UIActivityIndicatorView *activityIndicatorView;
- UITextField *userNameTF;
- UITextField *passWordTF;
- NSDictionary *resultDic2;
-
- UIView *view1;
- }
-
-
- @property (weak, nonatomic) IBOutlet UIButton *loginButton;
- @property (strong,nonatomic) NSString *homeURL;
-
-
- @end
(2)
- - (IBAction)login:(id)sender {
-
- [self initaAtivityIndicatorView];
- [activityIndicatorView startAnimating];
-
-
-
-
-
- NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.9.1/pb/l?user=%@&pwd=%@",userNameTF.text,passWordTF.text]];//这里的url输入自己的就可以,这里用的get方式,get方式都是明文,用post方式好一点
- NSLog(@"userNameTF:%@-",userNameTF.text);
- NSLog(@"passWordTF:%@-",passWordTF.text);
- NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
-
- NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"str-%@",dataStr);
-
-
- NSError *error;
- NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
- NSDictionary *resultDic1 = [array objectAtIndex:0];
- resultDic2 = [resultDic1 objectForKey:@"result"];
- NSString *str = [resultDic2 objectForKey:@"result"];
- NSLog(@"str -%@",str);
-
- if ([str isEqualToString:@"FAIL"] ) {
- NSLog(@"shibai-%@",str);
-
- [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logInFail) userInfo:nil repeats:NO];
-
- }else{
-
- self.homeURL = [resultDic2 objectForKey:@"RUrl"];
-
- NSLog(@"url - %@",self.homeURL);
-
-
- [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logInOK) userInfo:nil repeats:NO];
-
-
-
- }
-
-
- }
-
-
- -(void)logInFail {
-
- [activityIndicatorView stopAnimating];
-
-
- NSString *msg = [resultDic2 objectForKey:@"errMsg"];
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil nil];
- [alert show];
-
- [view1 removeFromSuperview];
-
-
- }
-
-
- -(void)logInOK{
- 跳转到首页就可以了
- [activityIndicatorView stopAnimating];