mmexport1620470726213.jpg
配图:PP岛

介绍CimKit是基于CIM项⽬框架下的iOS端SDK。配合CIM服务我们可以快速搭建属于⾃⼰的推送系统、IM系统。
Requirements 项⽬要求 : iOS10 ARC Podfile

如果你有好的建议和帮助您可以直接联系我:siterwu@gmail.com Todo 1、数据缓存 2、通⽤UI组件 3、推送集成 项⽬地址: https://gitee.com/Siter/cimkit

1、客户端引入sdk

  1. use_frameworks!
  2. pod 'CimKit'


然后运⾏⼀下命令安装:

  1. $ pod install


2. 初始化配置

2.1 导⼊头⽂件

  1. #import "CIMHeader.h"

2.2 配置服务端IP和端口

  1. [[CIMService instance] configHost:@"192.168.51.197" onPort:23456];

2.4 绑定⽤户ID

一般是登录后获得用户ID

  1. [[CIMService instance] connectionBindUserId:@"111111"];

2.4 添加监听回调

  1. /*消息接收回调*/
  2. [[CIMService instance] addMessageObserver:self];
  3. /*连接事件回调*/
  4. [[CIMService instance] addConnectionObserver:self];

2.5 移出监听回调

退出应用时移出回调

  1. [[CIMService instance] removeMessageObserver:self];
  2. [[CIMService instance] removeConnectionObserver:self];

2.6 回调说明

CIMPeerMessageObserver

  1. /// 接受到消息
  2. /// @param msg msg description
  3. -(void)cimHandleMessage:(CIMMessageModel * _Nonnull)msg;
  4. /// 消息解析失败
  5. /// @param data data description
  6. -(void)cimHandleMessageError:(NSData * _Nonnull)data;

CIMConnectionObserver

  1. /// ⽤户绑定成功
  2. /// @param bindSuccess bindSuccess description
  3. -(void)cimDidBindUserSuccess:(BOOL)bindSuccess;
  4. /// 连接成功
  5. -(void)cimDidConnectSuccess;
  6. /// 断开连接
  7. -(void)cimDidConnectClose;
  8. /// 连接失败
  9. /// @param error res description
  10. -(void)cimDidConnectError:(NSError *_Nullable)error;

3 功能接口

3.1 配置服务端IP和端口

  1. /// 配置IM服务器
  2. /// @param 服务端域名或者IP
  3. /// @param 推送端口23456
  4. -(void)configHost:(NSString *)host onPort:(NSInteger)port;

3.2 连接服务器并绑定⽤户

  1. /// 连接服务器并绑定⽤户
  2. /// @param userId userId description
  3. -(void)connectionBindUserId:(NSString *)userId;

3.3 添加消息监听回调

  1. /// @param observer observer description (可添加多个)不同时记得Remove
  2. -(void)addMessageObserver:(id)observer;

3.4 添加连接状态监听回调

  1. /// @param observer observer description (可添加多个)不同时记得Remove
  2. -(void)addConnectionObserver:(id)observer;

3.5 移除消息监听

  1. /// @param observer observer description
  2. -(void)removeMessageObserver:(id)observer;

3.6 移除链接监听

  1. /// @param observer observer description
  2. -(void)removeConnectionObserver:(id)observer;

3.7 退出后台断开连接

  1. -(void)enterBackground;

3.8 进⼊前台重新连接

  1. -(void)enterForeground;

3.9 重新连接

  1. -(void)reconnect;

3.10 断开连接

  1. -(void)disconnect;
  1. +(CIMService*)instance;
  2. /// 配置IM服务器
  3. /// @param host host description
  4. /// @param port port description
  5. -(void)configHost:(NSString *)host onPort:(NSInteger)port;
  6. /// 连接服务器并绑定⽤户
  7. /// @param userId userId description
  8. -(void)connectionBindUserId:(NSString *)userId;
  9. /// 添加消息监听回调
  10. /// @param observer observer description (可添加多个)不同时记得Remove
  11. -(void)addMessageObserver:(id)observer;
  12. /// 添加连接状态监听回调
  13. /// @param observer observer description (可添加多个)不同时记得Remove
  14. -(void)addConnectionObserver:(id)observer;
  15. 2020/11/25 Markdoc Preview
  16. 3/3
  17. /// 移除监听
  18. /// @param observer observer description
  19. -(void)removeMessageObserver:(id)observer;
  20. /// 移除监听回调
  21. /// @param observer observer description
  22. -(void)removeConnectionObserver:(id)observer;
  23. /// 退出后台 断开连接
  24. -(void)enterBackground;
  25. /// 进⼊前台重新连接
  26. -(void)enterForeground;
  27. /// 重新连接
  28. -(void)reconnect;
  29. /// 断开连接
  30. -(void)disconnect;

4 给客户端推送消息

https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz#H0a3Y