`
yangsongjing
  • 浏览: 243446 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

iOS 学习笔记2(http get 登录)

阅读更多

//

//  ViewController.m

//  WTS

//

//  Created by Allen on 14-8-20.

//  Copyright (c) 2014 Allen. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "ViewController.h"

 

@interfaceViewController ()

            

 

@end

 

@implementation ViewController

#pragma mark 登录

- (void) login{

    NSString *value1 = _account.text;

    NSString *value2 = _password.text;

    

    if(value1 == nil || [value1 length] == 0 || [value2 length] == 0) {

        // 对话框

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"用户名或密码不能为空"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定", nil];

        [alert show];

        return;

    }else{

        // 对话框

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:[@"账号:"stringByAppendingFormat:@"%@ 密码:%@",value1,value2] delegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定", nil];

        [alert show];

        // label

        _text.text = [@"账号:" stringByAppendingFormat:@"%@ 密码:%@",value1,value2];

    }

   

    

    //  键盘关闭叫出键盘到第一响应者

    // resignFirstResponder 代表不想当视图第一响应者

    //    [_account resignFirstResponder];

    //    [_password resignFirstResponder];

    

    // 如果第一响应者存在某个视图里面那么就可以用这个view 关掉键盘

    [self.view endEditing:YES];

    

    // GET 调用WTS登录接口

    [self getData:value1 Andpwd:value2];

}

 

-(void) getData :name Andpwd : pwd{

    //第一步,创建URL

    NSString *str1 = @"http://wts.angico.cn:81/SysService.svc/Login/";

    NSString *str2 = [str1 stringByAppendingFormat:@"%@/%@",name,pwd];

    

    NSLog(@"访问的接口地址为:%@",str2);

    

    NSURL *url = [NSURL URLWithString:str2];

    

    //第二步,通过URL创建网络请求

    //NSURLRequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒)

    //    其中缓存协议是个枚举类型包含:

    //    NSURLRequestUseProtocolCachePolicy(基础策略)

    //    NSURLRequestReloadIgnoringLocalCacheData(忽略本地缓存)

    //    NSURLRequestReturnCacheDataElseLoad(首先使用缓存,如果没有本地缓存,才从原地址下载)

    //    NSURLRequestReturnCacheDataDontLoad(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作)

    //    NSURLRequestReloadIgnoringLocalAndRemoteCacheData(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)

    //    NSURLRequestReloadRevalidatingCacheData(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)

    

    NSURLRequest *request = [[NSURLRequestalloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];

   

    //第三步,连接服务器

    NSData *received = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];

    

    // 获取返回值

    NSString *str = [[NSStringalloc]initWithData:received encoding:NSUTF8StringEncoding];

    

    NSLog(@"%@",str);

}

 

            

- (void)viewDidLoad {

    [superviewDidLoad];

    // 设置默认显示的hint

    _account.placeholder = @"请输入账号";

    _password.placeholder = @"请输入密码";

    //  设置密码属性

    _password.secureTextEntry = YES;

}

 

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

}

 

 

@end

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics