zoukankan      html  css  js  c++  java
  • iOS-Senior21-环信(代码)

    //先引入类库

    #import "EMSDK.h"

    1.在AppDelegate.m中

    //初始化一个环信对象

    EMOptions *emOp =[EMOptionsoptionsWihtAppkey:@"lyf520zln#bjs160305"];

    [[EMClient sharedClient] initializeSDKWithOptions : emOp];

    //获取到keyWindow

    [self.window makeKeyAndVisible];

    //获取main.storyboard

    UIStoryboard *main = [UIStoryboard storyboardWithName : @"Main" bundle : nil];

    UINavigationController *loginNC = [main instanttiateInitialViewController];

    //通过chat.storyboard的根视图控制器,模态推出下个界面

    [self.window.rootViewController presentViewController : loginNC animated : YES completion : nil];

    2.在viewController.m里

    //声明两个插座变量

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

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

    #pragma mark - 点击登录按钮

    - (IBAction) loginButton : (id) sender {

    if (self.userNameField.text.length == 0 || self.passwordField.text.length == 0){

    NSLog(@"用户名密码不能为空");

    return ;

    }

    //环信登录的方法

    EMError *error = [[EMClient sharedClient] loginWithUserName : self.userNameField.text password : self.passwordField.text];

    NSLog(@"error = %@",error);

    if (!error){ 

    NSLog(@"登录成功");

    //返回会话列表

    [self dismissViewControllerAnimated : YES completion : nil];

    }

    }

    2.在RegisterViewController.m里

    //声明两个插座变量

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

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

    #pragma mark - 点击注册按钮

    - (IBAction)didClickRegistButton : (UIButton *)sender {

    if (self.userNameField.text.length == 0 || self.passwordField.text.length == 0) {

    NSLog(@"用户名或密码不能为空");

    return ;

    }

    //环信的注册

    EMError *error =[ [EMClient sharedClient] registerWithUserName: self.userNameField.text password : self.passwordField.text];

    NSLog(@"error = %@",error);

    if (!error){

    NSLog(@"注册成功");

    }

    }

    3.在chatTableViewController.m里

    //会话属性

    @property (nonatomic,strong) EMConversation *conversation;

    //消息数组

    @property (nonatomic,strong) NSMutableArray *messageArray;

    #pragma mark - 视图显示

    - (void) viewDidAppear :(BOOL) animationed {

    [super viewDidAppear : animated];

    NSLog(@"%@",[EMClient sharedClient].currentUsername);

    //创建一个会话

    self.conversation = [[EMClient sharedClient].chatManager getConversation : @"[EMClicent sharedClient].currentUsername" type : EMConversationTypeChat createIfNotExist : YES];

    //注册接收消息

    [[EMClient sharedClient].chatManager addDelegate : self delegateQueue : nil];

    //移除消息回调

    [[EMClient sharedClient].chatManager removeDelegate : self];

    }

    - (void) viewDidLoad {

    [super viewDidLoad];

    self.messageArray = [NSMutableArray array];

    //注册接收消息

    [[EMClient sharedClient].chatManager addDelegate : self delegateQueue : nil];

    //注册cell

    [self.tableView registerClass : [UITableViewCell class] forCellReuseIdent ifier : @"cell"];

    }

    #pragma mark - 接收消息的代理方法

    - (void) didReceiveMessages : (NSArray *)aMessages {

    NSLog (@"接收到: %@",aMessages);

    //刷新tableview

    [self.tableView reloadData];

    }

    #pragma mark - 点击注销按钮

    - (void)didClickLogoutButton :(UIBarButtonItem *)sender {

    EMError *error = [[EMClient sharedClient]logout : YES];

    if(!error){

    NSLog(@"退出成功");

    }

    //返回登录页面

    UIStoryboard *main = [UIStoryboard storyboardWithName : @"Main" bundle : nil];

    UINavigationController *loginNC = [main instantiateInitialViewController];

    [self pressentViewController :loginNC animated :YES completion : nil];

    }

    #pragma mark - 点击发送按钮

    - (void) didClickSendMessageButton :(UIBarButtonItem *) sender {

    //创建消息内容messageBody

    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText : @"向对方扔一坨屎"];

    NSString *from = [[EMClient sharedClient] currentUsername];

    //消息对象

    EMMessage *message = [[EMMessage alloc] initWithConversationID : from from : from to : from body :body ext : nil];

    message.chatType = EMChatTypeChat;

    #pragma mark - 发送消息

        [[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {

            

        } completion:^(EMMessage *message, EMError *error) {

            NSLog(@"发送的消息%@",message);

            if (!error) {

                NSLog(@"发送成功");

               message.to = @"";

                //设置发送方向

                message.direction = EMMessageDirectionReceive;

                [self.messageArray addObject:message];

                //刷新tableview

                [self.tableView reloadData];

            }

        }];

    }

    #pragma mark - 设置cell

    分区:1

    列数:self.messageArray.count

    cell:用系统自带初始化

    {

    UITableViewCell = *cell = [tableView dequeueReusableCellWithIdentifier :@"cell" forIndexPath : indexPath];

    EMMessage *message = self.messageArray[indexPath.row];

    if (message.direction == EMMessageDirectionSend){

    cell.textLabel.text = @"";

    cell.detailTextLabel.text  = [self textFromMessage : message];

    cell.detailTextLabel.textColor = [UIColor magentaColor];

    }else {

    cell.textLabel.text = [self textFromMessage : message];

    cell.detailTextLabel.text = @"";

    }

    return cell;

    }

    #pragma mark - 取出消息中的文本

    - (NSString *)textFromMessage :(EMMessage *)message {

    EMMessageBody *body = message.body;

    EMTextMessageBody *textBody = (EMTextMessageBody *)body;

    //输出文本内容

    NSLog(@"textBody = %@",textBody.text);

    NSString *text = textBody.text;

    return text;

    }

  • 相关阅读:
    docker file和容器数据卷,发布镜像
    Docker的镜像原理和分层理解和打包镜像
    docker部署nginx,tomcat 练习
    Docker 命令和运行原理简单剖析
    Docker安装
    Java垃圾回收-栈和堆部分知识
    aio-pika的使用
    技术基础 | 在Apache Cassandra中改变VNodes数量的影响
    行业动态 | 通过使用Apache Cassandra实现实时供应链管理
    行业动态 | Apache Pulsar 对现代数据堆栈至关重要的四个原因
  • 原文地址:https://www.cnblogs.com/zhoulina/p/5551121.html
Copyright © 2011-2022 走看看