NSAppTransportSecurity:Dictionary
NSAllowsArbitraryLoads:Boolean YES
#import "ViewController.h"
@interface ViewController ()<NSURLSessionDelegate>
@property (strong, nonatomic) UITextField *userFile;
@property (strong, nonatomic) UITextField *passFile;
@property (copy, nonatomic) NSString *userStr;
@property (copy, nonatomic) NSString *passStr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *userFile = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 44)];
self.userFile = userFile;
self.userFile.delegate = self;
userFile.placeholder = @"请输入用户";
userFile.backgroundColor =[UIColor lightGrayColor];
[self.view addSubview:userFile];
UITextField *passFile = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, self.view.frame.size.width-100, 44)];
self.passFile = passFile;
self.passFile.delegate = self;
passFile.placeholder = @"请输入密码";
passFile.backgroundColor =[UIColor lightGrayColor];
[self.view addSubview:passFile];
UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 44)];
[loginBtn setTitle:@"登录" forState:UIControlStateNormal];
[loginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[loginBtn addTarget:self action:@selector(loginMethod) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.userFile){
self.userStr = textField.text;
NSLog(@"%@",self.userStr);
}else{
self.passStr = textField.text;
NSLog(@"%@",self.passStr);
}
}
- (void)loginMethod {
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"http://172.20.10.2:8080/login.json"];
// 通过URL初始化task,在block内部可以直接对返回的数据进行处理
NSURLSessionTask *task = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// weakSelf.dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];
// 启动任务
[task resume];
}
@end