百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

社交软件开发6-客户端开发-ios端开发验证登陆部分

bigegpt 2025-03-05 13:39 9 浏览

欢迎订阅我的头条号:一点热

上一节说到,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,增加如下的代码:

NSAppTransportSecurity

NSAllowsArbitraryLoads



这是时候启动测速,输入账号和密码

如果密码错误弹出如下的信息:



如果正确的话,或弹出如下的信息



这一节,就写得这里,下一节我们讲解登陆验证的一些优化。

欢迎订阅我的头条号:一点热,未经同意,请勿转载

相关推荐

程序员请收好:10个非常有用的 Visual Studio Code 插件

一个插件列表,可以让你的程序员生活变得轻松许多。作者|Daan译者|Elle出品|CSDN(ID:CSDNnews)以下为译文:无论你是经验丰富的开发人员还是刚刚开始第一份工作的初级开发人...

PADS在WIN10系统中菜单显示不全的解决方法

决定由AD转PADS,打开发现菜单显示不正常,如下图所示:这个是由于系统的默认字体不合适导致,修改一下系统默认字体即可,修改方法如下:打开开始菜单-->所有程序-->Windows系统--...

一文讲解Web前端开发基础环境配置

先从基本的HTML语言开始学习。一个网页的所有内容都是基于HTML,为了学好HTML,不使用任何集成工具,而用一个文本编辑器,直接从最简单的HTML开始编写HTML。先在网上下载notepad++文...

TCP/IP协议栈在Linux内核中的运行时序分析

本文主要是讲解TCP/IP协议栈在Linux内核中的运行时序,文章较长,里面有配套的视频讲解,建议收藏观看。1Linux概述  1.1Linux操作系统架构简介Linux操作系统总体上由Linux...

从 Angular Route 中提前获取数据

#头条创作挑战赛#介绍提前获取意味着在数据呈现在屏幕之前获取到数据。本文中,你将学到,在路由更改前怎么获取到数据。通过本文,你将学会使用resolver,在AngularApp中应用re...

边做游戏边划水: 基于浅水方程的水面交互、河道交互模拟方法

以下文章来源于腾讯游戏学堂,作者Byreave篇一:基于浅水方程的水面交互本文主要介绍一种基于浅水方程的水体交互算法,在基本保持水体交互效果的前提下,实现了一种极简的水面模拟和物体交互方法。真实感的...

Nacos介绍及使用

一、Nacos介绍Nacos是SpringCloudAlibaba架构中最重要的组件。Nacos是一个更易于帮助构建云原生应用的动态服务发现、配置和服务管理平台,提供注册中心、配置中心和动态DNS...

Spring 中@Autowired,@Resource,@Inject 注解实现原理

使用案例前置条件:现在有一个Vehicle接口,它有两个实现类Bus和Car,现在还有一个类VehicleService需要注入一个Vehicle类型的Bean:publicinte...

一文带你搞懂Vue3 底层源码

作者:妹红大大转发链接:https://mp.weixin.qq.com/s/D_PRIMAD6i225Pn-a_lzPA前言vue3出来有一段时间了。今天正式开始记录一下梗vue3.0.0-be...

一线开发大牛带你深度解析探讨模板解释器,解释器的生成

解释器生成解释器的机器代码片段都是在TemplateInterpreterGenerator::generate_all()中生成的,下面将分小节详细展示该函数的具体细节,以及解释器某个组件的机器代码...

Nacos源码—9.Nacos升级gRPC分析五

大纲10.gRPC客户端初始化分析11.gRPC客户端的心跳机制(健康检查)12.gRPC服务端如何处理客户端的建立连接请求13.gRPC服务端如何映射各种请求与对应的Handler处理类14.gRP...

聊聊Spring AI的Tool Calling

序本文主要研究一下SpringAI的ToolCallingToolCallbackorg/springframework/ai/tool/ToolCallback.javapublicinter...

「云原生」Containerd ctr,crictl 和 nerdctl 命令介绍与实战操作

一、概述作为接替Docker运行时的Containerd在早在Kubernetes1.7时就能直接与Kubelet集成使用,只是大部分时候我们因熟悉Docker,在部署集群时采用了默认的dockers...

在MySQL登录时出现Access denied for user ~~ (using password: YES)

Windows~~~在MySQL登录时出现Accessdeniedforuser‘root‘@‘localhost‘(usingpassword:YES),并修改MySQL密码目录适用...

mysql 8.0多实例批量部署script

背景最近一个项目上,客户需要把阿里云的rdsformysql数据库同步至线下,用作数据的灾备,需要在线下的服务器上部署mysql8.0多实例,为了加快部署的速度,写了一个脚本。解决方案#!/bi...