//
// ViewController.m
// 网络请求
//
// Created by love on 15/11/5.
// Copyright © 2015年 天目湖. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLConnectionDataDelegate>
@property (nonatomic,weak) UITextField *nameTextField;
@property (nonatomic,weak) UITextField *passwordTextField;
//从服务器接受完整的数据
@property (nonatomic,strong) NSMutableData *serviceData;
@end
@implementation ViewController
/**
* 网络请求的步骤
1.确定地址NSURL
2.建立请求NSURLRequest
3.建立并启动连接NSURLConnection
4.通过代理方法处理网络请求
遵守协议:NSURLConnectionDataDelegate
网络代理方法:
1.接收到服务器的响应,服务器要传数据,客户端做接收准备
2.接收服务器传输的数据,可能会多次执行
3.接收数据完成,做后续处理
4.服务器请求失败,原因很多,网络等
5.向服务器发送数据,此方法仅适用于POST,尤其上传文件
*/
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
#pragma mark 设置UI
- (void)setupUI{
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:4];
for (NSInteger i = 0; i<2; i++) {
//两个label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20+i*40, 80, 40)];
[self.view addSubview:label];
[arrayM addObject:label];
}
//两个textfield
for (NSInteger i = 0; i<2; i++) {
UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 24+i*40, 200, 32)];
[self.view addSubview:textfield];
[arrayM addObject:textfield];
//边框
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
//文字对齐方式
// [textfield setTextAlignment:NSTextAlignmentCenter];
[textfield setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
}
//设置label
[arrayM[0]setText:@"用户名"];
[arrayM[1]setText:@"密码"];
self.nameTextField = arrayM[2];
self.passwordTextField = arrayM[3];
//设置密码输入
self.passwordTextField.secureTextEntry = YES;
//1个button
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setFrame:CGRectMake(100, 106, 200, 40)];
[loginButton setTitle:@"登录" forState:UIControlStateNormal];
[self.view addSubview:loginButton];
[loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark 按钮监听方法
- (void)login{
NSString *userName = self.nameTextField.text;
NSString *pwd = self.passwordTextField.text;
NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.105/~apple/itcast/loginphp"];
NSURL *url = [NSURL URLWithString:urlString];
//2,建立请求NSURLRequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//请求方式
[request setHTTPMethod:@"POST"];
//数据体
NSString *bodyStr = [NSString stringWithFormat:@"username=%@password=%@",userName,pwd];
NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:body];
[request setHTTPBody:body];
//建立并启动连接
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
//服务器通知准备 准备中转数据
self.serviceData = [NSMutableData data];
}
#pragma mark -私有方法 尽量不要在方法中直接使用属性
#pragma mark GET登录
- (void)loginWithGetName:(NSString *)username PWD:(NSString *)pwd{
//1.确定地址NSURL
NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.105/~apple/itcast/loginphp?username=%@&password=%@",username,pwd];
//提示 1.url中如果带有中文字符 需要转换成带%号格式
//2 在ios开发中如果没有意外 统统使用UTF8的格式
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
//建立请求NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//建立连接
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//启动连接
[connection start];
//服务器通知准备 准备中转数据
}
#pragma mark 网络代理方法
#pragma mark 1 接收到服务器的响应 服务器要传数据 客户端做好接收准备
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
}
#pragma mark 2接收服务器传输的数据 可能会多次执行
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//对每次传输的数据进行拼接 需要中转数据
[self.serviceData appendData:data];
}
#pragma mark 3.接收数据完成 做后续处理
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//对方法2接收的数据做后续处理即可
NSString *str = [[NSString alloc]initWithData:_serviceData encoding:NSUTF8StringEncoding];
//对服务器返回的字符串进行处理
//1.从str中找出用户名所在的位置->0
NSRange range = [str rangeOfString:@"用户名"];
NSLog(@"%@",NSStringFromRange(range));
NSString *message = nil;
if (range.location>0) {
//取用户名后面的字符串,一直到末尾
NSRange messageRange = NSMakeRange(range.location+1, str.length-range.length);
message = NSStringFromRange(messageRange);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end