欢迎订阅我的头条号:一点热
上一节说到,Android客户端的开发,主要是编写了,如何使用Android studio 如何创建一个Android项目,已经使用gradle来加载第三方库,并且使用了异步网路传输的库:android-async-http,最终通过网络传输,在客户端打印出我们之前在服务器的网络返回的JSON数据格式,这一节,我们继续以IOS客户端开发为例,开发一个ios客户端来验证我们服务器登录部分是否正确。
知识准备:
Object 的基础知识,Xcode的下载与安装,cocoapods的安装,AFNetworking的使用。前两节已经搭建好的服务器,用于客户端访问验证,如果没有看过,请自行看会之前的服务器端登录部分的开发。
开发语言:
object C,这里就以object C来开发,当然有一些人是直接从swift入手的,我看看到最后可以把所有功能完成后,在开发一个swift的客户端。
开发环境
Mac+Xcode
可能会遇到的难题:
不知道如何安装cocoapods。这个大家可以自行解决,或者给我留言。关注我的头条号:一点热,给我回复。
继续开始今天的课程:
1、创建项目
打开xcode,点击file->new -> project,这个时候选择ios 的apllication,然后选择single view appliction.
点击next后,输入自己的域名,公司组织,还有这里我用object C
点击next,创建好工程后,整个项目显示如下
这个时候我们可以点击左上角的运行,这是模拟器会启动,但是屏幕上显示的是空白的页面
这时候,我们可以添加我们需要的相应的布局文件,主要是添加一个邮箱和密码的输入框和登陆的输入框
if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7) {
[selfsetEdgesForExtendedLayout:UIRectEdgeNone];
}
_emailTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(10, 40, self.view.bounds.size.width-20, 40)];
[_emailTextFieldsetBorderStyle:UITextBorderStyleRoundedRect];
[
_emailTextFieldsetPlaceholder:@"yeehot程序王:邮箱"];
[_emailTextFieldsetDelegate:self];
[_emailTextFieldsetCenter:CGPointMake(self.view.bounds.size.width / 2, _emailTextField.frame.origin.y + 20)];
[[selfview] addSubview:_emailTextField];
_passwordTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(10, 90, self.view.bounds.size.width-20, 40)];
[_passwordTextFieldsetBorderStyle:UITextBorderStyleRoundedRect];
[
_passwordTextFieldsetPlaceholder:@"yeehot程序王:用户密码"];
[_passwordTextFieldsetDelegate:self];
[_passwordTextFieldsetCenter:CGPointMake(self.view.bounds.size.width / 2, _passwordTextField.frame.origin.y + 20) ];
[[selfview] addSubview:_passwordTextField];
_loginBtn = [[UIButtonalloc] initWithFrame:CGRectMake(60, 150, self.view.bounds.size.width-120, 40)];
[_loginBtnsetBackgroundColor:RGB(21, 23, 14)];
[[_loginBtnlayer] setCornerRadius:8];
[_loginBtnsetTitle:@"登录"
forState:UIControlStateNormal];
[[selfview] addSubview:_loginBtn];
注意,由于上面我们这里有一个RGB的函数,但是找不到,那是我们引入了一个宏,这时我们需要创建一个YeehotProgramKing.pch,用来定义一些宏,或者头文件等等。我这里定义了一个颜色值,简化输入。
#ifndef YeehotProgramKing_pch
#define YeehotProgramKing_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#define RGB(r, g, b) RGBA(r,g,b,1.0f)
#define RGBA(r, g, b,a) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a]
#endif /* YeehotProgramKing_pch */
这个时候需要我们在编译器是加载.pch位置,我们需要将Precompile Prefix Header为Yes,这样我们在预编译后的pch文件会将缓存起来,下一次就不用再次编译,提高编译速度,点击target的项目,在Apple LLVM 7.0 -Language 栏目中找到 Prefix Header,这里输入的格式:"项目名字/创建pch的的名字.pch",我这里就是
YeehotPorgramKing/YeehotProgramKing.pch,
这个时候我们编译一次,如果没有错误的话,就可以在模拟器可以看到我们刚刚的布局文件了。
到这里,我们还没有实现网络的连接,那么我们需要加入网络连接的功能,我这里也是使用第三方的库。AFNetworking 3.0我们可以在github上找到。
https://github.com/AFNetworking/AFNetworking/
我这里是用cocospod来管理,真的方便,所以我也建议大家使用这个,当然大家可以下载afnetworking然后导入到项目中,
要使用cocoapods,我们是需要安装cocoapods
sudo gem install cocoapods
如果不能下载可以使用国内淘宝的库,这里我就不做详细的介绍了。
接着我们需要创建一个Podfile,我们需要在项目的的根目录,也就是和
YeehotPorgramKing.xcodeproj这里同一个目录创建,创建后,输入如下的代码。
platform :ios, '8.0'
target 'YeehotPorgramKing' do
pod 'AFNetworking', '~> 3.0'
end
这个时候,我们使用控制台定位到Podfile的目录下。输入
pod install,最后会下载我们的afnetworking库,如下图
这个时候我们退出程序,打开我们的根目录,项目多了一个
YeehotPorgramKing.xcworkspace,我们以后就打开这个工作空间,
打开后,我们可以看到afnetwork已经加入到工程里面了。
我们在刚刚创建的YeehotProgramKing.pch加入afnetwork的头文件,这样就不用我们在每个文件加入头文件了。
#import
#import
加入后,我们还需要回到ViewController编写登陆的函数。
首先增加登陆按钮的点击响应的函数
[_loginBtnaddTarget:selfaction:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
然后添加登陆的方法
- (void)login:(UIButton *)sender {
[_emailTextFieldresignFirstResponder];
[_passwordTextFieldresignFirstResponder];
NSString *email = [_emailTextFieldtext];
NSString *password = [_passwordTextFieldtext];
// 初始化Manager
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
// 不加上这句话,会报“Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
// 请求的参数,我们这里有两个参数,email和passwd
NSDictionary *parameters = @{@"email": email, @"passwd" :password};
NSLog(@"infos===%@",email);
[manager GET:@"http://192.168.3.4:8080/Yeehot-Program-King/user/login"parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
// 请求成功,解析数据
NSLog(@"%@", responseObject);
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaveserror:nil];
// 请求成功,解析数据
NSLog(@"%@", responseObject);
NSData *info = responseObject;
NSString *datas = [[NSStringalloc]initWithData:info encoding:NSUTF8StringEncoding];
// NSLog(@"JSON: %@", datas);
NSData *jsonData = [datas dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict=[NSJSONSerialization
JSONObjectWithData:jsonData
options:NSJSONReadingMutableLeaves
error:nil];
UIAlertController *alertController = [
UIAlertControlleralertControllerWithTitle:@"yeehot程序王登陆情况"message:datas
preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [
UIAlertActionactionWithTitle:@"取消"
style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [
UIAlertActionactionWithTitle:@"确定"
style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[selfpresentViewController:alertController animated:YEScompletion:nil];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@", [error localizedDescription]);
}];
// // post请求
// [manager POST:@"http://192.168.3.4:8080/Yeehot-Program-King/user/login" parameters:parameter constructingBodyWithBlock:^(id _Nonnull formData) {
//
//
// } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//
// } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//
// // 请求成功,解析数据
// NSLog(@"%@", responseObject);
//
// NSData *info = responseObject;
// NSString *datas = [[NSString alloc]initWithData:info encoding:NSUTF8StringEncoding];
// // NSLog(@"JSON: %@", datas);
// NSData *jsonData = [datas dataUsingEncoding:NSUTF8StringEncoding];
// NSDictionary *jsonDict=[NSJSONSerialization
// JSONObjectWithData:jsonData
// options:NSJSONReadingMutableLeaves
// error:nil];
//
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"yeehot程序王登陆情况" message:datas
preferredStyle:UIAlertControllerStyleAlert];
//
// // Create the actions.
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
// }];
//
// UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
// }];
//
// // Add the actions.
// [alertController addAction:cancelAction];
// [alertController addAction:otherAction];
//
// [self presentViewController:alertController animated:YES completion:nil];
//
//
// } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
// // 请求失败
// NSLog(@"%@", [error localizedDescription]);
// }];
}
最后我们还需要修改http协议,这是由于iOS9中新增App Transport Security(简称ATS)特性。所以我们需要在Info.plist,增加如下的代码:
这是时候启动测速,输入账号和密码
如果密码错误弹出如下的信息:
如果正确的话,或弹出如下的信息
这一节,就写得这里,下一节我们讲解登陆验证的一些优化。
欢迎订阅我的头条号:一点热,未经同意,请勿转载