这是本文档旧的修订版!


环信坐席 iOS SDK


开发工具

Xcode

AgentSDKDemo 为Open Source,link to AgentSDKDemo

目录

SDK介绍

1、文件介绍:依赖库包括HyphenateLite.framework和AgentSDK.framework两个framework,二者缺一不可。

准备工作

1、工程中导入 HyphenateLite.framework和AgentSDK.framework 【勾选 Copy items if needed 和 Create groups】。
2、选中当前的TARGET,向General → Embedded Binaries 中添加以上两个依赖库.Linked Frameworks and Libraries 中会自动增加。
3、向Build Settings → Linking → Other Linker Flags 中增加-ObjC【注意区分大小写】。
4、在工程info.plist文件中 增加隐私权限(必填,用于聊天)

  Privacy - Photo Library Usage Description 需要访问您的相册
  Privacy - Microphone Usage Description 需要访问您的麦克风
  Privacy - Camera Usage Description 需要访问您的摄像机

5、在.pch文件或者全局.h文件中添加如下代码:

#ifdef __OBJC__
    #import <AgentSDK/AgentSDK.h>
    #endif
 

上传AppStore注意事项

  为了方便广大开发者开发测试,framework中提供的二进制文件支持x86_64 armv7 arm64等平台,上传AppStore需要剔除不需要的CPU架构支持,只剩余armv7、arm64

平台即可,命令如下:

【首先进入AgentSDK.framework所在的目录】
//移除二进制文件的x86_64支持
lipo AgentSDK.framework/AgentSDK -remove x86_64 -output AgentSDK   //剔除x86_64
//替换framework内部二进制文件【记得备份】
mv AgentSDK AgentSDK.framework/AgentSDK
//查看剥离后二进制文件支持的CPU架构,如果现实armv7 arm64 ,则完成剥离
lipo -info AgentSDK.framework/AgentSDK
【首先进入HyphenateLite.framework所在目录】
// 移除二进制文件的x86_64支持
lipo HyphenateLite.framework/HyphenateLite -remove x86_64 -output HyphenateLite
//替换framework内部二进制文件【记得备份】
mv HyphenateLite HyphenateLite.framework/HyphenateLite
//查看剥离后的二进制文件支持的CPU架构,如果显示armv7 arm64,就完成剥离
lipo -info HyphenateLite.framework/HyphenateLite

集成离线推送

- (void)registerRemoteNotification { //注册离线推送
 {
    [application registerForRemoteNotifications];
    UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound |   UIUserNotificationTypeAlert;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
 }
 //您注册了推送功能,iOS 会自动回调以下方法,得到 deviceToken,您需要将 deviceToken 传给 SDK。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [[HDClient sharedClient] bindDeviceToken:deviceToken];
}

// 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"error -- %@",error);
}

APNs注册失败,一般是由于使用了通用证书或者是模拟器调试导致,请检查证书并用真机调试。此处是 iOS 系统报的错,如仍不能确定,请从网上查找相关资料。

初始化

在didFinishLaunchingWithOptions中调用初始化方法:

HDOptions *option = [[HDOptions alloc] init];
option.apnsCertName = apnsCertName;//NSString 类型的推送证书名字
option.enableConsoleLog = NO; //控制台的日志是否开启
[[HDClient sharedClient] initializeSDKWithOptions:option];

登录

//传入username、password以及是否隐身登录
[[HDClient sharedClient] asyncLoginWithUsername:<#(NSString *)#> password:<#(NSString *)#> hidingLogin:<#(BOOL)#> completion:^(id responseObject, HDError *error) {
        if (error) {
            //登录失败
        } else {
            //登录成功
        }
    }];

退出方法

[[HDClient sharedClient] logoutCompletion:^(HDError *error) {
        if (error == nil) {
            //退出成功
        } else {
           //退出失败
        }
    }];

判断是否已经登录

[HDClient sharedClient].isLoggedInBefore //BOOL 类型,YES是,NO

添加SDK监听

//添加SDK监控,第二个参数是执行代理方法的队列,暂为主队列
[[HDClient sharedClient] addDelegate:self delegateQueue:nil];
//移除SDK监控
[[HChatClient sharedClient] removeDelegate:self];

/*
 *  SDK连接服务器的状态变化时会接收到该回调
 *
 *  以下情况, 会调用该方法:
 *  1. 登录成功后, 手机无法上网时, 会调用该回调
 *  2. 登录成功后, 网络状态变化时, 会调用该回调
 */
- (void)connectionStateDidChange:(HDConnectionState)aConnectionState;

/**
 管理员是否允许客服自定义最大接待人数

 @param allow YES NO 分别表示允许、不允许
 */
- (void)allowAgentChangeMaxSessions:(BOOL)allow;

/**
  当前账号被迫下线,需要重新登录

 @param reason 下线原因
 */
- (void)userAccountNeedRelogin:(HDAutoLogoutReason)reason;

/*
 *  会话被管理员转接
 *  @param sessionId 被转接的会话ID
 */

- (void)conversationTransferedByAdminWithServiceSessionId:(NSString *)sessionId;

/*
 * 会话被管理员关闭
 * @param  sessionId 被关闭的会话ID
 */

- (void)conversationClosedByAdminWithServiceSessionId:(NSString *)sessionId;

/*
 * 会话自动关闭 
 * @param  sessionId 关闭的会话ID
 */
- (void)conversationAutoClosedWithServiceSessionId:(NSString *)sessionId;

/*
 * 会话最后一条消息变化
 * @param  sessionId 变化的sessionId
 */
- (void)conversationLastMessageChanged:(MessageModel *)message;

/*
 * 新会话
 * @param  sessionId 新会话sessionId
 */
- (void)newConversationWithSessionId:(NSString *)sessionId;

/**
 有新转接会话请求接收

 @param parameters sessionId,用于接受调度
 */
- (void)transferScheduleRequest:(NSString *)sessionId;

/**
 会话被确认转接【需要管理员开启“转接会话需要对方确认”】
 */
- (void)transferScheduleAccept:(NSString *)sessionId;

/**
 会话被拒绝转接

 @param sessionId sessionId
 */
- (void)transferScheduleRefuse:(NSString *)sessionId;

/*
 * 客服身份发生变化
 */
- (void)roleChange:(RolesChangeType)type;

/*
 * 待接入列表变化
 */
- (void)waitListChange;

/*
 * 客服列表发生变化
 */
- (void)agentUsersListChange;

/*
 * 通知中心有有变化
 */
- (void)notificationChange;

添加消息监听

//添加消息监听
[[HDClient sharedClient].chatManager addDelegate:self];
//移除消息监听
[[HDClient sharedClient].chatManager removeDelegate:self];

/*
 *  收到新消息
 *
 *  @param aMessages  消息列表<HDMessage *>
 */
- (void)messagesDidReceive:(NSArray <HDMessage *> *)aMessages;