NSURLSession GET 和 POST 使用步骤(注册页面的注册登录判断)

来源:互联网 发布:贵州大数据发展 编辑:程序博客网 时间:2024/06/06 20:15

一 .GET(使用步骤)
//0.0创建URL字符串

NSString *urlString = [NSString stringWithFormat:@"http://localhost/php/login/login.php?username=%@&password=%@",self.UserNameTextField.text,self.PassWordTextField.text];//0.1因为有中文要进行百分号转义.(*** 注意点: 当有汉字时候***)NSString *convertString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];//1.创建URLNSURL *URL = [NSURL URLWithString:convertString];//2.创建会话开启任务并启动(默认是数据任务是GET)[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {    //3.错误处理    if(error == nil)    {        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];        if([result[@"state"] isEqualToString:@"OK"])        {            NSLog(@"登入成功");        }        else        {            NSLog(@"登录失败");        }    }    else    {        NSLog(@"%@",error);    }}] resume];//不要忘记启动任务.

2.POST(使用步骤)
说明:POST 使用步骤基本和GET差不多,但是要注意,由于是POST要修改请求类型,详见下面代码

//点击时候用POST传递数据(实际开发中基本不用,此乃测试)
- (void)touchesBegan:(NSSet

原创粉丝点击