28.简单的环信单聊(用于商城聊天)

这里的集成是带语音的

1.导入我整理过JoanKing环信整理文件 密码: 29m8
2.在pch文件导入
  #ifdef __OBJC__
  /**
   *   环信的设置
   */
  #import "EMSDKFull.h"
  #import "EaseUI.h"
  #import "ChatViewController.h"

  #endif
3.导入库

1.SDK 包含实时语音依赖库有:

 CoreMedia.framework
 AudioToolbox.framework
 AVFoundation.framework
 MobileCoreServices.framework
 ImageIO.framework
 libc++.dylib
 libz.dylib
 libstdc++.6.0.9.dylib
 libsqlite3.dylib
 libiconv.dylib

(如果使用的是 xcode7,后缀为 tbd。)

第 2 步:SDK 不支持 bitcode,设置:Xcodeproj->Build Settings->Enable Bitcode 设置为 NO。

28.简单的环信单聊(用于商城聊天)_第1张图片

4.代码配置

  • 1.先说AppDelegate.m里面

    /*
       环信
     */
    #import "EMSDK.h"
    

    下面的两个变量你们需要注册环信的开发者平台账号,再创建应用来获取
    appkey:@"注册相应应用的APPkey"
    apnsCertName:@"推送证书的名字"

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//AppKey:注册的AppKey,详细见下面注释。
//apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
EMOptions *options = [EMOptions optionsWithAppkey:@"注册相应应用的APPkey"];
options.apnsCertName = @"推送证书的名字";
[[EMClient sharedClient] initializeSDKWithOptions:options];

//设置回调监听代理(设置自动登录之后才能在退出后台进行获取聊天的列表)
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];
BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;

if (isAutoLogin) {
    
    NSLog(@"123==重新登录成功");
}

/*
 EaseUI的使用
 */
[[EaseSDKHelper shareHelper] hyphenateApplication:application
                    didFinishLaunchingWithOptions:launchOptions
                                           appkey:@"注册相应应用的APPkey"
                                     apnsCertName:@"推送证书的名字"
                                      otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];

//代码注册离线推送

//iOS8 注册APNS
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
    [application registerForRemoteNotifications];
    UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
    UIUserNotificationTypeSound |
    UIUserNotificationTypeAlert;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
}
else{
    UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
    UIRemoteNotificationTypeSound |
    UIRemoteNotificationTypeAlert;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
}
   /*
    *  推送的方法: - (void)didReceiveMessages:(NSArray *)aMessages{}
    */
}

// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

   /**
    *  环信的离线推送
    */
    [[EMClient sharedClient] bindDeviceToken:deviceToken];

}

// APP进入后台
 - (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}

// APP将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
   [[EMClient sharedClient] applicationWillEnterForeground:application];
}
  • 2.注册方法
   EMError *error = [[EMClient sharedClient] registerWithUsername:@"注册的账号" password:@"注册的秘密"];
    if (error==nil)
    {
          NSLog(@"注册成功");
    }
  • 3.登陆

     /**
      *   环信登陆成功才算成功
      */
     EMError *error = [[EMClient sharedClient] loginWithUsername:myId password:@"dft"];
          
     if (!error)
     {
          NSLog(@"登录成功);
          //设置自动登录
          [[EMClient sharedClient].options setIsAutoLogin:YES];
          // 成功后跳转页面
       }
    
  • 4.与指定用户进行聊天

    导入#import "ChatViewController.h"
    #pragma mark  与个人的聊天
    -(void)clicKPerson
    {
         NSString *zy_id = @"62"; //以用户名字来进行聊天
    
       /*
        *  设置商家的单聊模式
        */
    
      ChatViewController *chatViewController = [[ChatViewController alloc]initWithConversationChatter:zy_id conversationType:EMConversationTypeChat];
    
      chatViewController.title =zy_id;
    
       [self.navigationController pushViewController:chatViewController animated:YES];
    }
    

5.获取聊天列表(跳转进入)

#pragma mark  聊天列表的转入
-(void)clicKchat{

  EaseConversationListViewController *easeConversationListViewController = [EaseConversationListViewController new];
  [self.navigationController pushViewController:easeConversationListViewController animated:YES];

 }
  • 到这里你就完成一个聊天的流程以及推送的功能

5.在此我再加一个用户用其他手机登陆的提示按钮(下次在写),记得点喜欢哦!!!

你可能感兴趣的:(28.简单的环信单聊(用于商城聊天))