用户输入用户名和密码,点击登录。。。我们把用户名和密码(用post方式或者get方式,get方式多用于测试看你需要)传给服务器,服务器进行判断,然后返回一个接口给我们(这里服务器返回的json接口,正确就返回一个正确的接口给我们,错误就返回一个错误的接口给我们)。我们拿到接口去解析,判断是否登录成功做相应的操作。

关于json解析,可以网上找找资料先看看,我就只知道[]是数组,{}是字典,iOS有自带的json解析的。

以前一直说接口文档,接口什么,可自己从来都是模糊的,比如说一个类的接口,我就认为是这个类的方法。。可是,我的boss给我的登录接口的文档的时候,我才知道,哦,接口文档原来是这样子的呀。。嘻嘻。。。

  1. [
  2. {
  3. "result": {
  4. "RUrl":   "http://localhost/pad_4078_B7DA_676D3FE52763",
  5. "errMsg": "",
  6. "result": "OK",
  7. "sid": "B67D78_B7DA_676D3FE52763"
  8. }
  9. }
  10. ]
  11.    jsondata[0][‘result’].result:
  12.    OK,登录成功,打开RUrl
  13.    FAIL,登录失败,取:errMsg

(1)

  1. #import <UIKit/UIKit.h>
  2. #import "ByValueUrl.h"
  3. @interface LogInViewController : UIViewController <UITextFieldDelegate>{
  4. UIActivityIndicatorView *activityIndicatorView;
  5. UITextField *userNameTF;//用户名
  6. UITextField *passWordTF;//密码
  7. NSDictionary *resultDic2;
  8. UIView *view1;//等待转圈的view
  9. }
  10. @property (weak, nonatomic) IBOutlet UIButton *loginButton;//登录按钮
  11. @property (strong,nonatomic) NSString *homeURL;//登录成功拿到的url
  12. @end

(2)

  1. - (IBAction)login:(id)sender {
  2. [self initaAtivityIndicatorView];
  3. [activityIndicatorView startAnimating];//开始动画
  4. //    定时器(设置时间为3秒)
  5. //    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
  6. //加载一个NSURL对象
  7. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.9.1/pb/l?user=%@&pwd=%@",userNameTF.text,passWordTF.text]];//这里的url输入自己的就可以,这里用的get方式,get方式都是明文,用post方式好一点
  8. NSLog(@"userNameTF:%@-",userNameTF.text);
  9. NSLog(@"passWordTF:%@-",passWordTF.text);
  10. NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
  11. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //将请求的url数据放到NSData对象中
  12. NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  13. NSLog(@"str-%@",dataStr);
  14. //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
  15. NSError *error;
  16. NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  17. NSDictionary *resultDic1 = [array objectAtIndex:0];
  18. resultDic2 = [resultDic1 objectForKey:@"result"];
  19. NSString *str = [resultDic2 objectForKey:@"result"];
  20. NSLog(@"str -%@",str);
  21. if ([str isEqualToString:@"FAIL"] ) {
  22. NSLog(@"shibai-%@",str);
  23. //    定时器(设置时间为3秒)
  24. [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logInFail) userInfo:nil repeats:NO];
  25. }else{
  26. self.homeURL = [resultDic2 objectForKey:@"RUrl"];
  27. NSLog(@"url - %@",self.homeURL);
  28. //    定时器(设置时间为3秒)
  29. [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logInOK) userInfo:nil repeats:NO];
  30. }
  31. }
  32. //登录失败
  33. -(void)logInFail {
  34. [activityIndicatorView stopAnimating];//结束动画
  35. NSString *msg = [resultDic2 objectForKey:@"errMsg"];
  36. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil nil];
  37. [alert show];
  38. [view1 removeFromSuperview];
  39. }
  40. //登录成功
  41. -(void)logInOK{
  1. 跳转到首页就可以了
    1. [activityIndicatorView stopAnimating];//结束动画

iOS-登录认证/json解析的更多相关文章

  1. iOS开发系列-JSON解析

    概述 JOSN是一种轻量级的数据格式,一般用于数据交互.服务器返回给客户端,一般都是JSON格式或者XML格式. JSON的格式: {"name" : "CoderHon ...

  2. iOS开发之JSON解析

    JSON解析步骤: - (NSArray *)products { if (_products == nil) { //第一步:获取JSON文件的路径: NSString *path = [[NSBu ...

  3. iOS 后台返回json解析出现的null的解决办法

    在后台返回值为Null为空时,我们代码没有判断时,程序就会崩溃.当时一直很疑惑是为啥,后来发现是数据问题,由于服务器的数据库中有些字段为空,然后以Json形式返回给客户端时就会出现这样的数据.当我们通 ...

  4. IOS自带json解析类解析json

    - (IBAction)test:(id)sender { NSString *result = @"{\"code\":\"S00000\",\&q ...

  5. iOS SDK原生JSON解析

    - (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...

  6. iOS中处理json解析出现的null,nil的解决办法

    最开始是使用的一个函数进行处理,代码如下: - (id) setNoNull:(id)aValue{ if (aValue == nil) { aValue = @"";//为nu ...

  7. 巨蟒python全栈开发django10:ajax&&登录认证

    通过题目进行知识点回顾: 聚合查询 From django.db.models import Avg,Min,Max,F,Q,Count,Sum #查询书籍的平均值 Ret= Models.Book. ...

  8. 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析

    (最近学的都是很基础的接口测试,都是基于UI界面可见的接口,就是发请求,接收响应,分析返回的结果,校验,对共通模块进行封装,仅此而已,其实做自动化的思路基本都是如此,UI也是.) 现在开始用httpC ...

  9. 混合应用 微信登录授权 微信登录认证失败 ios PGWXAPI错误-1 code:-100 / 安卓 message:invalid appsecret innerCode:40125

    最近项目需要做微信登录,于是利用HTML5+ API Reference的OAuth模块管理客户端的用户登录授权验证功能,允许应用访问第三方平台的资源.(链接:https://www.dcloud.i ...

随机推荐

  1. 〖Linux〗实时更新 hosts 文件的脚本

    适用场景: 下载了一个smarthosts的hosts文件,但hosts文件过旧导致一些ip地址已失效无法访问网络. 脚本使用: ./hostsupdate # 直接从 /etc/hosts 中获得需 ...

  2. V-rep学习笔记:外部函数调用方式

    The remote API functions are interacting with V-REP via socket communication in a way that reduces l ...

  3. django之创建第6个项目-过滤器

    1.views.PY # Create your views here. #coding:utf-8 from django.http import HttpResponse import datet ...

  4. python之模块pydoc

    # -*- coding: cp936 -*- #python 27 #xiaodeng import pydoc #主要用于从python模块中自动生成文档,这些文档可以基于文本呈现,也可以生成we ...

  5. 转 configure: error: Cannot find ldap.h

    检查下面是不是已经安装,如果没有安装之:检查:yum list openldapyum list openldap-devel安装 :yum install openldap yum install ...

  6. RHEL7 DNS 服务 unbound 测试

    测试环境: 物理机win10系统,虚拟机软件使用Oracle VirtualBox. rhel1.rusky.com 192.168.100.1 RHEL7(辅DNS) rhel2.rusky.com ...

  7. 转:Ogre的八叉树场景管理器OctreeSceneManager

    上面是我绘制的一张图. 关于八叉树场景管理器主要需要关注两个类,其一是松散八叉树的数据结构Ogre::Octree,其二是八叉树场景管理器Ogre::OctreeSceneManager. 下面摘录图 ...

  8. uri.js的用法事例

    来源于:http://smoothprogramming.com/tutorials/get-set-query-string-values-from-url-using-uri-js/ Get or ...

  9. iOS 批量打包

    如果你曾经试过做多 target 的项目,到了测试人员要测试包的时候,你就会明白什么叫“生不如死”.虽然 Xcode 打包很方便,但是当你机械重复打 N 次包的时候,就会觉得这纯粹是浪费时间的工作.所 ...

  10. STRTOK函数和STRTOK_R函数

    STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...