iOS讲解迷惑--LeanCloud

前言: LeanCloud云服务器, 可以进行数据存储和即时通讯等。

Step1: 你要去网站https://leancloud.cn/注册账号, 创建应用,获得

IDkey

Step2:看文档 https://leancloud.cn/docs/start.html 按步骤操作, 很详细的

以下是 登录注册和增删改查的代码



在Appdelegate中

//
//  AppDelegate.m
//  CocoaPodsDemo
//
//  Created by nyl on 15/11/2.
//  Copyright (c) 2015年 nieyinlong. All rights reserved.
//

#import "AppDelegate.h"
#import <AVOSCloud/AVOSCloud.h>


#import "Student.h"

// ID和key值
#define AVOSCLOUDAPPID @"CTALoVFhejC0uv0nmEGwpxhN"

#define AVOSCLOUDAPPKEY @"TI8WAaCphuFVCDfa3IY53AGc"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 设置ACOSCloud
    [AVOSCloud setApplicationId:AVOSCLOUDAPPID clientKey:AVOSCLOUDAPPKEY];
    

    [AVAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    
    
    // 注册student
    [Student registerSubclass];
    
    return YES;
}


登录

//
//  LoginViewController.m
//  LeanCloud
//
//  Created by nyl on 15/11/2.
//  Copyright © 2015年 nieyinlong. All rights reserved.
//

#import "LoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)actionLoginButton:(id)sender {
    
    [AVUser logInWithUsernameInBackground:self.userName.text password:self.passWord.text block:^(AVUser *user, NSError *error) {
        // 如果错误
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            if (codeNum == 210){
                [self showAlterViewWithTitle:@"提示" Message:@"用户名或者密码错误"];
            // 登陆成功
            }
        } else {
           [self showAlterViewWithTitle:@"提示" Message:@"登陆成功"];
        }
    }];

}
     
// 封装一个alertView
- (void)showAlterViewWithTitle:(NSString *)title Message:(NSString *)message
{
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self performSelector:@selector(closeAlter:) withObject:alertView afterDelay:1];
    
    [self presentViewController:alertView animated:YES completion:nil];
}
// 设置1秒之后消失
- (void)closeAlter:(UIAlertController *)alert
{
    [alert dismissViewControllerAnimated:YES completion:nil];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


注册

//
//   Controller.m
//  LeanCloud
//
//  Created by nyl on 15/11/2.
//  Copyright © 2015年 nieyinlong. All rights reserved.
//

#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>

@interface  RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *passWord;

@property (weak, nonatomic) IBOutlet UITextField *email;

@property (weak, nonatomic) IBOutlet UITextField *phoneNum;


@end

@implementation  RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (IBAction)actionRegisterButton:(id)sender {
    
    // 如果用户名,密码,邮箱,电话 为空
    if ([self.userName.text isEqualToString:@""] || [self.passWord.text isEqualToString:@""] || [self.email.text isEqualToString:@""] || [self.phoneNum.text isEqualToString:@""]){
        [self showAlterViewWithTitle:@"提示" Message:@"用户名,密码,邮箱不能为空"];
        // 如果不为空的话
    } else {
        [self registerWithUserName:self.userName.text UserPassWord:self.passWord.text UserMail:self.email.text UserPhone:self.phoneNum.text];
        
    }
}

// 注册
- (void)registerWithUserName:(NSString *)name UserPassWord:(NSString *)passWord UserMail:(NSString *)mail UserPhone:(NSString *)phone
{
    AVUser *user = [AVUser user];
    user.username = name;
    user.password = passWord;
    user.email = mail;
    user.mobilePhoneNumber = phone;
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
           //  NSError *err = error;
            
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            switch (codeNum) {
                case 203:
                    [self showAlterViewWithTitle:@"提示" Message:@"该邮箱已经被占用"];
                    break;
                case 125:
                    [self showAlterViewWithTitle:@"提示" Message:@"该邮箱格式不正确"];
                    break;
                case 127:
                    [self showAlterViewWithTitle:@"提示" Message:@"请输入正确的手机号码"];
                    break;
                case 202:
                    [self showAlterViewWithTitle:@"提示" Message:@"用户名已经被占用"];
                    break;
                case 214:
                    [self showAlterViewWithTitle:@"提示" Message:@"电话号码已经被暂用"];
                default:
                    break;
            }
            
            
        } else {
            [self showAlterViewWithTitle:@"提示" Message:@"注册成功"];
        }
    }];
}

// 封装一个alertView
- (void)showAlterViewWithTitle:(NSString *)title Message:(NSString *)message
{
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self performSelector:@selector(closeAlter:) withObject:alertView afterDelay:1];

    [self presentViewController:alertView animated:YES completion:nil];
}
// 设置1秒之后消失
- (void)closeAlter:(UIAlertController *)alert
{
    [alert dismissViewControllerAnimated:YES completion:nil];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end





增删改查


1. 创建student类

student.h

#import "AVObject.h"

@interface Student : AVObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *sex;

/**
 *  头像
 */
@property (nonatomic, strong) AVFile *image;

@end

Student.m

//
//  Student.m
//  CocoaPodsDemo
//
//  Created by nyl on 15/11/2.
//  Copyright (c) 2015年 nieyinlong. All rights reserved.
//

#import "Student.h"

@implementation Student


/**
 *  重要, 是系统动态生成getter setter方法
 */


/*
*  忽略系统的getter setter方法
*/
@dynamic name, age, sex, image;


@end





在VC中操作:  CURD

//
//  ViewController.m

//
//  Created by nylon 15/11/2.
//  Copyright (c) 2015年 nieyinlong. All rights reserved.
//

#import "ViewController.h"
//#import <AFNetworking.h>
//#import <AVOSCloud.h>
#import <AVOSCloud/AVOSCloud.h>

#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self createObject];
    
   // [self createStudentObject];
    
//    [self updateStudentObject];
    
//    [self selectStudent];
    [self deleteStudentObject];
    
}



// 存一个简单的数据
- (void)createObject
{
    
    AVObject *teacher = [AVObject objectWithClassName:@"Teacher"];
    [teacher setObject:@"张三" forKey:@"name"];
    [teacher setObject:@"27" forKey:@"age"];
    [teacher setObject:[NSNumber numberWithInt:1] forKey:@"id"];
    // 直接上传, 主线程操作
//    [teacher save];
    
    //. 方式2, 子线程操作
    [teacher saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"创建成功");
        }
    }];
}




- (void)createStudentObject
{
    Student *stu = [[Student alloc] init];
    stu.name = @"李四";
    stu.age = @"19";
    stu.sex = @"男";
    
    /**
     *  传入一个本地图片
     */
    // pathForResource: 图片路径
    // ofType: 图片类型
    stu.image = [AVFile fileWithName:@"5.png" contentsAtPath:[[NSBundle mainBundle] pathForResource:@"5" ofType:@"png"]];
    
    [stu saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"stu 创建成功, id=%@", stu.objectId);
        }
    }];
    
    
    
}

//5637187560b294bc66251aba
// 更改操作
- (void)updateStudentObject
{
    // 1. 根据上传上传上去的id, 查找到学生
    Student *stu = [Student objectWithoutDataWithObjectId:@"5637187560b294bc66251aba"];
    
    stu.sex = @"女";
    [stu saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"更新成功");
        }
    }];
    
}

#pragma mark----- 查询
- (void)selectStudent
{
    Student *stu = [Student objectWithoutDataWithObjectId:@"5637187560b294bc66251aba"];
    [stu fetchInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        if (!error) {
            // 取出
            NSString *name = [object valueForKey:@"name"];
            
            NSLog(@"学生的名字 = %@", name);
        } else {
            NSLog(@"查找失败!!!!!!!!!");
        }
    }];
    
}


#pragma mark----- 删除
- (void)deleteStudentObject
{
    Student *stu = [Student objectWithoutDataWithObjectId:@"5637187560b294bc66251aba"];
    [stu deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"删除成功");
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值