利用leancloud做一套完整的登陆注册系统

官方网址
官方文档
如果您在阅读我的文章时有疑问 , 请点击这里

文章包含功能: 注册 , 登陆 , 设置头像 , 网名 , 其中网名和头像可以称为数据储存

你需要去官网注册账号和创建一个应用

pos所需要的SDK

SDWeb是因为头像

pod 'AVOSCloud'     # 数据存储、短信、社交、云引擎调用等基础服务模块
pod 'SDWebImage'

打开AppDelegate.m

导入文件

#import <AVOSCloud/AVOSCloud.h>

设置id和key 需要更换为您的id和key 位置:应用-设置-应用key

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    // 配置 SDK 储存
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModuleAPI];
    // 配置 SDK 推送
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModulePush];
    // 配置 SDK 云引擎
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModuleEngine];
    // 配置 SDK 即时通讯
    [AVOSCloud setServerURLString:@"https://router-g0-push.avoscloud.com" forServiceModule:AVServiceModuleRTM];
    //设置id和key
    [AVOSCloud setApplicationId:@"替换为App ID" clientKey:@"替换为App Key"];
    // 放在 SDK 初始化语句 [AVOSCloud setApplicationId:] 后面,只需要调用一次即可
    [AVOSCloud setAllLogsEnabled:YES];
    // Override point for customization after application launch.
    return YES;
}

注册

1.每一个用户都有一个对应的体系 , 这里你可以理解为一行数据 , leancloud自带手机号、邮箱等 , 头像和网名是没有的 , 需要我们自己去创建
2.注册肯定要有界面 , 不管您是xib还是手写 这里不包含UI
3.这里包含 : 用户名 , 密码 , 手机号 , 邮箱 , 你的UI要为此设置4个输入框 , 还有一个注册的按钮
4.手机号只为填写 , 自动绑定 , 不发送验证码(当然你也可以设置为验证)
5.邮箱会向邮箱发送激活链接(不强制激活 , 可设置为强制激活验证)

导入头文件

#import <AVOSCloud/AVOSCloud.h>

注册按钮的点击方法内(注意看注释)

//创建用户
        AVUser *user = [AVUser user];
        //设置 用户名(用户名 你可以理解为ID , wx号 , 并非为网名)    为文本框的内容
        user.username = self.UserNameTF.text;
        //设置 用户的密码  为文本框的内容
        user.password = self.PassWordTF.text;
        //设置 用户的email   为文本框的内容
        user.email = self.EmailTF.text;
        //设置 用户的手机号  为文本框的内容
        user.mobilePhoneNumber = self.PhoneNumberTF.text;
        //注册成功的返回
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            //判断是否成功
            if (succeeded) {// 注册成功
                //为用户设置默认头像 (可以让用户选择本地头像 , 我比较懒 , 没有写)
                //0.05为图片清晰度 , 1为原图 , 原图没有必要 , 缓存慢
                NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"icon"], 0.05);
                //把头像储存到云端(leancloud , 你可以到应用内  file查看)
                //这里是为了获取URL
                AVFile *imageFile = [AVFile fileWithData:imageData];
                //储存头像返回的方法
                [imageFile uploadWithCompletionHandler:^(BOOL succeeded, NSError * _Nullable error) {
                    if (succeeded) {//头像储存成功
                        //把头像的url存到用户的体系里 (每个用户有对应的体系 , 你可以理解为一行数据 , 用户内包含手机号等 , 没有头像和网名 , 所以我们这里要自己创建)
                        // object为URL , key自己取名 , 方面自己查看即可
                        [user setObject:[NSString stringWithFormat:@"%@",imageFile.url] forKey:@"iconUrl"];
                        //设置默认头像
                        [user setObject:user.username forKey:@"NickName"];
                        //保存
                        [user saveInBackground];
                    }else{
                        NSLog(@"#############头像储备失败%@",error);
                    }
                }];
                //弹窗展示2秒 , 提示用户注册成功
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                    //这里为跳转到跟视图
                    [self UserisOK];
                });
            } else {
                // 注册失败
                NSLog(@"%@",error);
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"被注册" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                });
            }
        }];

登陆

登陆方式可以为用户名+密码登陆 , 手机号和邮箱 , 这里为用户名+密码
同样需要您写一个 用户名、密码的文本框以及一个登陆按钮

登陆按钮方法内

[AVUser logInWithUsernameInBackground:self.ZhangHaoTF.text password:self.PassWordTF.text block:^(AVUser *user, NSError *error) {
        //判断用户输入是否规范
        if ([self.ZhangHaoTF.text isEqualToString:@""] || [self.PassWordTF.text isEqualToString:@""]) {
            //账号或密码为空
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入账号和密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
            });
        }else{
            if (user != nil) {
                //登陆成功
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                    //跳转到跟视图
                    [self UserisOK];
                });
            } else {
                //登陆失败
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账号或密码错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                });
            }
        }
    }];

跳转到跟视图的方法

由于技术有限 , 只提供了一种笨的方法

- (void)UserisOK{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *tab = [[UITabBarController alloc] init];
    UINavigationController *Message = [[UINavigationController alloc] initWithRootViewController:[MessageViewController new]];
    Message.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"信息" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    //联系人
    UINavigationController *Contact = [[UINavigationController alloc] initWithRootViewController:[ContactViewController new]];
    Contact.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"联系人" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    //我
    UINavigationController *My = [[UINavigationController alloc] initWithRootViewController:[MyViewController new]];
    My.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    
    tab.viewControllers = @[Message , Contact , My];
    tab.selectedIndex = 2;
    appDelegate.window.rootViewController = tab;
}

当 app != 第一次启动时 , 需要判断用户当前是否为登陆状态 , 如果为登陆 , 跳转到跟视图 , 如果未登陆 , 需要跳转到注册或登陆界面

AppDelegate.m内- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里

AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        // 跳转到首页
        //消息
        UITabBarController *tab = [[UITabBarController alloc] init];
        UINavigationController *Message = [[UINavigationController alloc] initWithRootViewController:[MessageViewController new]];
        Message.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"信息" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
        //联系人
        UINavigationController *Contact = [[UINavigationController alloc] initWithRootViewController:[ContactViewController new]];
        Contact.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"联系人" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
        //我
        UINavigationController *My = [[UINavigationController alloc] initWithRootViewController:[MyViewController new]];
        My.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
        
        tab.viewControllers = @[Message , Contact , My];
        tab.selectedIndex = 2;
        self.window.rootViewController = tab;
    } else {
        self.window.rootViewController = [ViewController new];
        //缓存用户对象为空时,可打开用户注册界面…
    }

退出登陆

[AVUser logOut];  //清除缓存用户对象
        AVUser *currentUser = [AVUser currentUser]; // 现在的currentUser是nil了
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
            //返回到登陆注册界面
            [self presentViewController:[ViewController new] animated:NO completion:nil];
        });

登陆成功后 , 获取用户信息

头像

[cell.IconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[AVUser currentUser] objectForKey:@"iconUrl"]]]];

网名

cell.NickNameLabel.text = [NSString stringWithFormat:@"%@",[[AVUser currentUser] objectForKey:@"NickName"]];

手机号 , 邮箱 , 用户名

NSString *str = 
                 [AVUser currentUser].username;//用户名
                 [AVUser currentUser].email;//邮箱
                 [AVUser currentUser].mobilePhoneNumber;//手机号

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容