GoBelieve文档中心
  • Android开发文档
  • IOS开发文档
  • JS开发文档
  • 服务器RestAPI文档

iOS SDK 接入指南

应用集成到自己的客户端

  1. podfile

     pod 'gobelieve', :git => 'https://github.com/GoBelieveIO/im_ios.git'
    
  2. 在AppDelegate初始化deviceID以及message handler

     [IMService instance].deviceID = deviceID;
     [IMService instance].peerMessageHandler = [PeerMessageHandler instance];
     [IMService instance].groupMessageHandler = [GroupMessageHandler instance];
     [IMService instance].customerMessageHandler = [CustomerMessageHandler instance];
    
  3. 在AppDelegate中监听系统网络变化

     -(void)startRechabilityNotifier {
         self.reach = [GOReachability reachabilityForInternetConnection];
         self.reach.reachableBlock = ^(GOReachability*reach) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 NSLog(@"internet reachable");
                 [[IMService instance] onReachabilityChange:YES];
             });
         };
    
         self.reach.unreachableBlock = ^(GOReachability*reach) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 NSLog(@"internet unreachable");
                 [[IMService instance] onReachabilityChange:NO];
             });
         };
    
         [self.reach startNotifier];
    
     }
    
     [self startRechabilityNotifier];
     [IMService instance].reachable = [self.reach isReachable];
    
  4. 登录成功之后设置token和uid, token和uid从应用本身的登录接口获得

     [IMService instance].token = "";
     [PeerMessageHandler instance].uid = uid;
     [GroupMessageHandler instance].uid = uid;
     [CustomerMessageHandler instance].appid = appid;
     [CustomerMessageHandler instance].uid = uid;
    
     SyncKeyHandler *handler = [[SyncKeyHandler alloc] initWithFileName:fileName];
     [IMService instance].syncKeyHandler = handler;
    
  5. 初始化消息db

     NSString *dbPath = @"";//sqlite文件路径
     FMDatabase *db = [Database openMessageDB:dbPath];
    
     [PeerMessageDB instance].db = db;
     [GroupMessageDB instance].db = db;
     [CustomerMessageDB instance].db = db;
    
  6. 启动IMService开始接受消息

     [[IMService instance] start];
    
  7. 添加消息observer,处理相应类型的消息

     //连接状态
     [[IMService instance] addConnectionObserver:ob];
    
     //点对点消息
     [[IMService instance] addPeerMessageObserver:ob];
     //群组消息
     [[IMService instance] addGroupMessageObserver:ob];
     //客服消息
     [[IMService instance] addCustomerMessageObserver:ob];
     //直播的聊天室消息
     [[IMService instance] addRoomMessageObserver:ob];
     //实时消息,用于voip的信令
     [[IMService instance] addRTMessageObserver:ob];
     //系统消息
     [[IMService instance] addSystemMessageObserver:ob];
    
  1. app进入后台,断开socket链接

     [[IMService instance] enterBackground];
    
  1. app返回前台,重新链接socket

    [[IMService instance] enterForeground]; 
    
  2. 发送点对点消息

    PeerMessageViewController* msgController = [[PeerMessageViewController alloc] init];
    msgController.peerUID = peerUID;
    msgController.peerName = @"";
    msgController.currentUID = uid;
    [self.navigationController pushViewController:msgController animated:YES];
    
  3. 发送群组消息

    GroupMessageViewController* msgController = [[GroupMessageViewController alloc] init];
    msgController.groupID = groupID;
    msgController.groupName = @"";
    msgController.currentUID = uid;
    [self.navigationController pushViewController:msgController animated:YES];
    
  4. 发送客服消息

    CustomerMessageViewController *msgController = [[CustomerMessageViewController alloc] init];
    msgController.currentUID = uid;
    msgController.appid = APPID;
    msgController.storeID = storeID;
    msgController.name = @"";
    msgController.appName = @"";
    msgController.storeName = @"";
    [self.navigationController pushViewController:msgController animated:YES];
    
  5. 用户注销

    [[IMService instance] stop]
    

类IMService

设置当前用户的access token

属性名:String accessToken
功能:在调用start之前必须要设置用户的accessToken,token是由第三方应用服务器调用IM服务器RestAPI所得。

设置当前用户id

属性名:int64_t uid
功能:在调用start之前必须要设置用户的uid

设置当前设备id

属性名:String deviceID
功能:设备id可以用户多点登陆的唯一性判断,可选调用

监控网络变化

方法名: -(void) startRechabilityNotifier
功能: 可以及时的根据网络变化情况,断开,建立socket链接。

设置观察者

方法名: -(void) addConnectionObserver:(id<IMConnectionObserver>)observer
功能:设置observer,在observer中处理接受到的消息。
参数说明: 
    * observer 观察者
返回值:无返回值

设置点对点消息的接受者

方法名: -(void) addPeerMessageObserver:(id<PeerMessageObserver>)observer
功能:设置observer,在observer中处理接受到的消息。
参数说明: 
    * observer 观察者
返回值:无返回值

设置群组消息的接受者

方法名: -(void) addGroupMessageObserver:(id<GroupMessageObserver>)observer
功能:设置observer,在observer中处理接受到的消息。
参数说明: 
    * observer 观察者
返回值:无返回值

开始接受消息

方法名:-(void) start
功能:用户登陆成功后,连接im服务器来接受在线消息。

停止接受消息

方法名:-(void) stop
功能:用户注销后,断开和im服务器的连接,从而终止接受在线消息。

应用进入到前台

方法名:-(void) enterForeground
功能:应用进入前台,im服务器将停止将消息推送到通知栏

应用进入到后台

方法名:-(void) enterBackground
功能:应用进入后台,im服务器会将将消息推送到通知栏

发送消息

方法名: -(BOOL) sendPeerMessage:(IMMessage*)msg
功能:发送im消息,如果当前连接处于断开状态,消息会发送失败, 函数返回YES,也不能表明服务器已经接收到消息。
参数说明:
    *msg 消息对象
返回值:BOOL

发送群组消息

方法名: -(BOOL) sendGroupMessage:(IMMessage*)msg
功能:发送im消息,如果当前连接处于断开状态,消息会发送失败, 函数返回YES,也不能表明服务器已经接收到消息。
参数说明:
    *msg 消息对象
返回值:BOOL

接口IMConnectionObserver

连接状态

方法名:-(void) onConnectState:(int)state
功能:连接状态变更通知
参数说明:
    *state 连接的状态 STATE_UNCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_CONNECTFAIL
返回值:无返回值

接口PeerMessageObserver

消息

方法名:-(void) onPeerMessage:(IMMessage*)msg
功能:接受到一条im消息
参数说明:
    *msg 消息对象
返回值:无返回值

消息ACK

方法名:-(void) onPeerMessageACK:(IMMessage*)msg
功能:服务器对收到消息的ack,此时这条消息才能标志为发送成功
参数说明:
    *msgLocalID 消息的本地ID
    *uid 消息接受者
返回值:无返回值

消息发送失败

方法名:-(void) onPeerMessageFailure:(IMMessage*)msg
功能:消息发出后,未收到服务器的ack,在和服务器的连接断开后,会通知消息发送失败。
参数说明:
    *msgLocalID 消息的本地ID
    *uid 消息接受者
返回值:无返回值

接口GroupMessageObserver

群组消息

方法名:-(void) onGroupMessage:(IMMessage*)msg
功能:接受到一条im消息
参数说明:
    *msg 消息对象
返回值:无返回值

群组消息ACK

方法名:-(void) onGroupMessageACK:(IMMessage*)msg
功能:服务器对收到消息的ack,此时这条消息才能标志为发送成功
参数说明:
    *msgLocalID 消息的本地ID
    *gid 群组ID
返回值:无返回值

群组消息发送失败

方法名:-(void) onGroupMessageFailure:(IMMessage*)msg
功能:消息发出后,未收到服务器的ack,在和服务器的连接断开后,会通知消息发送失败。
参数说明:
    *msgLocalID 消息的本地ID
    *gid 群组ID
返回值:无返回值

群组通知

方法名:-(void) onGroupNotification:(NSString*)notification
功能:群组创建,解散和群组成员变更的通知消息
参数说明:
    *notification 通知内容

返回值:无返回值

类IMApi

设置当前用户的device token

方法名:-(BOOL) bindDeviceToken:(NSString*)deviceToken success:(void (^)())success fail:(void (^)())fail
功能:设置device token后,im服务器会对用户推送离线消息
参数说明:
    *deviceToken APNS的deviceToken
返回值:boolean 是否设置成功。

清空当前用户关联的device token

方法名:-(BOOL) unbindDeviceToken:(NSString*)deviceToken success:(void (^)())success fail:(void (^)())fail
功能:im服务器不会再对这台设备推送离线消息
参数说明:
    *deviceToken APNS的deviceToken
    *accessToken 用户当前的accessToken
返回值:boolean 是否清空成功。
回顶部