利用LeanCloud來實現註冊帳號,存儲帳號以及,登陸時查詢帳號是否正確.集成方式很簡單能夠看這裏的官方文檔.地址是這裏: https://leancloud.cn/docs/
數據庫
// AppDelegate.m文件網絡
// // AppDelegate.m // LoginView // // Created by 裴波波 on 16/4/21. // Copyright © 2016年 裴波波. All rights reserved. // #import "AppDelegate.h" #import <AVOSCloud/AVOSCloud.h> @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // applicationId 即 App Id,clientKey 是 App Key。 //這裏的id 以及 key 都是在leancloud中建立應用之後提供的,用來訪問leancloud統計以及受權 [AVOSCloud setApplicationId:@"GCyStwB6xVWMfvLiGktfTu4p-gzGzoHsz " clientKey:@"bNJj02mLLUbtbQyExkCLw9yM"]; //跟蹤應用打開狀況 [AVAnalytics trackAppOpenedWithLaunchOptions:launchOptions]; return YES; } @end
// // ViewController.m // LoginView // // Created by 裴波波 on 16/4/21. // Copyright © 2016年 裴波波. All rights reserved. // #import "ViewController.h" #import <AVOSCloud/AVOSCloud.h> #import "PBViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @property (weak, nonatomic) IBOutlet UITextField *txtPassword; @end @implementation ViewController #pragma mark - 註冊用戶 - (IBAction)clcikRegisterUsers:(id)sender { //獲取用戶輸入字符 NSString * name = self.txtUserName.text; NSString * pwd = self.txtPassword.text; NSLog(@"name : %@ pwd : %@",name,pwd); //將註冊內容 異步 寫到雲 dispatch_async(dispatch_get_global_queue(0, 0), ^{ AVObject *post = [AVObject objectWithClassName:@"PBBUsers"]; [post setObject:pwd forKey:name]; [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (succeeded) { // 保存成功了! NSLog(@"帳戶保存成功"); } }]; }); } #pragma mark - 點擊登陸 - (IBAction)clcikLoginBtn:(id)sender { //獲取用戶輸入字符 NSString * name = self.txtUserName.text; NSString * pwd = self.txtPassword.text; NSLog(@"name : %@ pwd : %@",name,pwd); //異步訪問網絡驗證帳號密碼 dispatch_async(dispatch_get_global_queue(0, 0), ^{ //聯網查詢用戶名密碼 //數據查詢 AVQuery *query = [AVQuery queryWithClassName:@"PBBUsers"]; // [query whereKey:@"words" equalTo:@"Hello World!"]; [query whereKey:name equalTo:pwd]; [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) { // object 就是符合條件的第一個 AVObject if (error) { NSLog(@"%@",error); NSLog(@"帳號或者密碼錯誤"); return; } NSString * str = object[@"words"]; NSLog(@"%@",str); //跳轉到主線程執行 dispatch_async(dispatch_get_main_queue(), ^{ PBViewController * vc = [PBViewController new]; [self.navigationController pushViewController:vc animated:YES]; }); }]; }); }
- (void)viewDidLoad { [super viewDidLoad]; //測試存儲 // AVObject *post = [AVObject objectWithClassName:@"TestObject"]; // [post setObject:@"Hello World!" forKey:@"words"]; // [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { // if (succeeded) { // // 保存成功了! // } // }]; } //-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // // //數據查詢 // AVQuery *query = [AVQuery queryWithClassName:@"TestObject"]; // [query whereKey:@"words" equalTo:@"Hello World!"]; // [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) { // // object 就是符合條件的第一個 AVObject // if (error) { // NSLog(@"%@",error); // return; // } // NSString * str = object[@"words"]; // NSLog(@"%@",str); // }]; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
訪問網絡要異步執行,主隊列要異步執行,同步會卡住,死鎖app