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內(nèi)部可以直接對返回的數(shù)據(jù)進(jìn)行處理
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]);
}];
// 啟動任務(wù)
[task resume];
}
@end