利用leancloud做一套完整的登陸注冊(cè)系統(tǒng)

官方網(wǎng)址
官方文檔
如果您在閱讀我的文章時(shí)有疑問 , 請(qǐng)點(diǎn)擊這里

文章包含功能: 注冊(cè) , 登陸 , 設(shè)置頭像 , 網(wǎng)名 , 其中網(wǎng)名和頭像可以稱為數(shù)據(jù)儲(chǔ)存

你需要去官網(wǎng)注冊(cè)賬號(hào)和創(chuàng)建一個(gè)應(yīng)用

pos所需要的SDK

SDWeb是因?yàn)轭^像

pod 'AVOSCloud'     # 數(shù)據(jù)存儲(chǔ)、短信、社交、云引擎調(diào)用等基礎(chǔ)服務(wù)模塊
pod 'SDWebImage'

打開AppDelegate.m

導(dǎo)入文件

#import <AVOSCloud/AVOSCloud.h>

設(shè)置id和key 需要更換為您的id和key 位置:應(yīng)用-設(shè)置-應(yīng)用key

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    // 配置 SDK 儲(chǔ)存
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModuleAPI];
    // 配置 SDK 推送
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModulePush];
    // 配置 SDK 云引擎
    [AVOSCloud setServerURLString:@"https://avoscloud.com" forServiceModule:AVServiceModuleEngine];
    // 配置 SDK 即時(shí)通訊
    [AVOSCloud setServerURLString:@"https://router-g0-push.avoscloud.com" forServiceModule:AVServiceModuleRTM];
    //設(shè)置id和key
    [AVOSCloud setApplicationId:@"替換為App ID" clientKey:@"替換為App Key"];
    // 放在 SDK 初始化語句 [AVOSCloud setApplicationId:] 后面,只需要調(diào)用一次即可
    [AVOSCloud setAllLogsEnabled:YES];
    // Override point for customization after application launch.
    return YES;
}

注冊(cè)

1.每一個(gè)用戶都有一個(gè)對(duì)應(yīng)的體系 , 這里你可以理解為一行數(shù)據(jù) , leancloud自帶手機(jī)號(hào)、郵箱等 , 頭像和網(wǎng)名是沒有的 , 需要我們自己去創(chuàng)建
2.注冊(cè)肯定要有界面 , 不管您是xib還是手寫 這里不包含UI
3.這里包含 : 用戶名 , 密碼 , 手機(jī)號(hào) , 郵箱 , 你的UI要為此設(shè)置4個(gè)輸入框 , 還有一個(gè)注冊(cè)的按鈕
4.手機(jī)號(hào)只為填寫 , 自動(dòng)綁定 , 不發(fā)送驗(yàn)證碼(當(dāng)然你也可以設(shè)置為驗(yàn)證)
5.郵箱會(huì)向郵箱發(fā)送激活鏈接(不強(qiáng)制激活 , 可設(shè)置為強(qiáng)制激活驗(yàn)證)

導(dǎo)入頭文件

#import <AVOSCloud/AVOSCloud.h>

注冊(cè)按鈕的點(diǎn)擊方法內(nèi)(注意看注釋)

//創(chuàng)建用戶
        AVUser *user = [AVUser user];
        //設(shè)置 用戶名(用戶名 你可以理解為ID , wx號(hào) , 并非為網(wǎng)名)    為文本框的內(nèi)容
        user.username = self.UserNameTF.text;
        //設(shè)置 用戶的密碼  為文本框的內(nèi)容
        user.password = self.PassWordTF.text;
        //設(shè)置 用戶的email   為文本框的內(nèi)容
        user.email = self.EmailTF.text;
        //設(shè)置 用戶的手機(jī)號(hào)  為文本框的內(nèi)容
        user.mobilePhoneNumber = self.PhoneNumberTF.text;
        //注冊(cè)成功的返回
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            //判斷是否成功
            if (succeeded) {// 注冊(cè)成功
                //為用戶設(shè)置默認(rèn)頭像 (可以讓用戶選擇本地頭像 , 我比較懶 , 沒有寫)
                //0.05為圖片清晰度 , 1為原圖 , 原圖沒有必要 , 緩存慢
                NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"icon"], 0.05);
                //把頭像儲(chǔ)存到云端(leancloud , 你可以到應(yīng)用內(nèi)  file查看)
                //這里是為了獲取URL
                AVFile *imageFile = [AVFile fileWithData:imageData];
                //儲(chǔ)存頭像返回的方法
                [imageFile uploadWithCompletionHandler:^(BOOL succeeded, NSError * _Nullable error) {
                    if (succeeded) {//頭像儲(chǔ)存成功
                        //把頭像的url存到用戶的體系里 (每個(gè)用戶有對(duì)應(yīng)的體系 , 你可以理解為一行數(shù)據(jù) , 用戶內(nèi)包含手機(jī)號(hào)等 , 沒有頭像和網(wǎng)名 , 所以我們這里要自己創(chuàng)建)
                        // object為URL , key自己取名 , 方面自己查看即可
                        [user setObject:[NSString stringWithFormat:@"%@",imageFile.url] forKey:@"iconUrl"];
                        //設(shè)置默認(rèn)頭像
                        [user setObject:user.username forKey:@"NickName"];
                        //保存
                        [user saveInBackground];
                    }else{
                        NSLog(@"#############頭像儲(chǔ)備失敗%@",error);
                    }
                }];
                //彈窗展示2秒 , 提示用戶注冊(cè)成功
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注冊(cè)成功" 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];
                    //這里為跳轉(zhuǎn)到跟視圖
                    [self UserisOK];
                });
            } else {
                // 注冊(cè)失敗
                NSLog(@"%@",error);
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注冊(cè)失敗" message:@"被注冊(cè)" 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];
                });
            }
        }];

登陸

登陸方式可以為用戶名+密碼登陸 , 手機(jī)號(hào)和郵箱 , 這里為用戶名+密碼
同樣需要您寫一個(gè) 用戶名、密碼的文本框以及一個(gè)登陸按鈕

登陸按鈕方法內(nèi)

[AVUser logInWithUsernameInBackground:self.ZhangHaoTF.text password:self.PassWordTF.text block:^(AVUser *user, NSError *error) {
        //判斷用戶輸入是否規(guī)范
        if ([self.ZhangHaoTF.text isEqualToString:@""] || [self.PassWordTF.text isEqualToString:@""]) {
            //賬號(hào)或密碼為空
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請(qǐng)輸入賬號(hào)和密碼" 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];
                    //跳轉(zhuǎn)到跟視圖
                    [self UserisOK];
                });
            } else {
                //登陸失敗
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"賬號(hào)或密碼錯(cuò)誤" 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];
                });
            }
        }
    }];

跳轉(zhuǎn)到跟視圖的方法

由于技術(shù)有限 , 只提供了一種笨的方法

- (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:@""]];
    //聯(lián)系人
    UINavigationController *Contact = [[UINavigationController alloc] initWithRootViewController:[ContactViewController new]];
    Contact.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"聯(lián)系人" 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;
}

當(dāng) app != 第一次啟動(dòng)時(shí) , 需要判斷用戶當(dāng)前是否為登陸狀態(tài) , 如果為登陸 , 跳轉(zhuǎn)到跟視圖 , 如果未登陸 , 需要跳轉(zhuǎn)到注冊(cè)或登陸界面

AppDelegate.m內(nèi)- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里

AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        // 跳轉(zhuǎn)到首頁
        //消息
        UITabBarController *tab = [[UITabBarController alloc] init];
        UINavigationController *Message = [[UINavigationController alloc] initWithRootViewController:[MessageViewController new]];
        Message.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"信息" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
        //聯(lián)系人
        UINavigationController *Contact = [[UINavigationController alloc] initWithRootViewController:[ContactViewController new]];
        Contact.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"聯(lián)系人" 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];
        //緩存用戶對(duì)象為空時(shí),可打開用戶注冊(cè)界面…
    }

退出登陸

[AVUser logOut];  //清除緩存用戶對(duì)象
        AVUser *currentUser = [AVUser currentUser]; // 現(xiàn)在的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];
            //返回到登陸注冊(cè)界面
            [self presentViewController:[ViewController new] animated:NO completion:nil];
        });

登陸成功后 , 獲取用戶信息

頭像

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

網(wǎng)名

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

手機(jī)號(hào) , 郵箱 , 用戶名

NSString *str = 
                 [AVUser currentUser].username;//用戶名
                 [AVUser currentUser].email;//郵箱
                 [AVUser currentUser].mobilePhoneNumber;//手機(jī)號(hào)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容