一、准备工作
1、注册环信帐号
注册一个环信账号之后,我们用注册的帐号登陆。然后创建一个应用,会得到一个对应的AppKey,这个AppKey在初始化环信SDK的时候需要用到。
2、制作推送证书
如果需要做离线推送的功能,需要制作一个推送证书。如果只是需要实现单聊、群聊等功能,可以跳过此步骤。个人建议刚开始接触环信的开发者可以忽略此步骤。
3、下载环信SDK
二、集成环信的SDK
1、把环信SDK添加到工程中
从环信官网下载下来的是一个压缩包,解压之后,把我们需要的环信SDK,即HyphenateSDK这个文件夹,整个的拖入到我们的工程中。如下图:
在lib文件夹下面有两个静态库,只需要用到一个,根据你的需求选择。
libHyphenateSDK.a不包含实时语音功能,libHyphenateFullSDK.a包含所有功能。
2、添加对应的依赖库
2.1. 向Build Phases → Link Binary With Libraries 中添加依赖库
SDK依赖库有
- CoreMedi.framework
- AudioToolbox.framework
- AVFoundation.framework
- MobileCoreServices.framework
- ImageIO.framework
- SystemConfiguration.framework
- libc++.dylib
- libresolv.dylib
- libz.dylib
- libstdc++.6.0.9.dylib
- libsqlite3.dylib
(如果使用的是xcode7,后缀为tbd)
SDK包含实时语音依赖库有
- CoreMedia.framework
- AudioToolbox.framework
- AVFoundation.framework
- MobileCoreServices.framework
- ImageIO.framework
- SystemConfiguration.framework
- libc++.dylib
- libresolv.dylib
- libz.dylib
- libstdc++.6.0.9.dylib
- libsqlite3.dylib
- libiconv.dylib
温馨提示:注意不要添加错了,也不能添加少了,添加完毕之后,不要着急,先编译一下。编译成功,则说明没有问题;如果编译报错,则仔细对照上面例举的静态库进行添加,直到编译成功,再进行下一步。
3、配置工程
3.1 不包含语音静态库的配置方法
(1) 删掉libHyphenateFullSDK.a,保留libHyphenateSDK.a;
(2) 在Build Settings -> Other Linker Flags 添加”fore_load”和”libHyphenateSDK.a”的相对路径。
如下图所示:
3.2 包含语音静态库的配置方法
(1) 删掉libHyphenateSDK.a,保留libHyphenateFullSDK.a;
(2) 在Build Settings -> Other Linker Flags 添加”-ObjC”。
如下图所示:
4、验证SDK是否添加成功
在AppDelegate.m文件中添加环信SDK初始化的方法,记得添加头文件”EaseMob.h”。下面提供了我用的测试AppKey,你可以替换成你自己申请的AppKey。编译成功,则说明你已经正确集成了环信的SDK了。
如果编译有问题,可能存在的原因:
(1) 静态库没有添加正确;
(2) 静态库工程配置不正确;
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> </code><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">- (</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">BOOL</span><span style="font-variant-ligatures: no-common-ligatures">)application:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIApplication</span><span style="font-variant-ligatures: no-common-ligatures"> *)application didFinishLaunchingWithOptions:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSDictionary</span><span style="font-variant-ligatures: no-common-ligatures"> *)launchOptions {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">// Override point for customization after application launch.</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMOptions</span><span style="font-variant-ligatures: no-common-ligatures"> *options = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMOptions</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d">optionsWithAppkey</span><span style="font-variant-ligatures: no-common-ligatures">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"chongwu#chongwu"</span><span style="font-variant-ligatures: no-common-ligatures">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">// options.apnsCertName = @"";</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">sharedClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">] </span><span style="font-variant-ligatures: no-common-ligatures">initializeSDKWithOptions</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:options];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *error1 = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">sharedClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">] </span><span style="font-variant-ligatures: no-common-ligatures">registerWithUsername</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"easeuidemo"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">password</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"1"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span><span style="font-variant-ligatures: no-common-ligatures"> (error1==</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">nil</span><span style="font-variant-ligatures: no-common-ligatures">) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">注册成功</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-variant-ligatures: no-common-ligatures">);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><span style="font-variant-ligatures: no-common-ligatures"></span> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *error2 = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">EMClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">sharedClient</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">] </span><span style="font-variant-ligatures: no-common-ligatures">loginWithUsername</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"easeuidemo"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">password</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"1"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span><span style="font-variant-ligatures: no-common-ligatures"> (!error2) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">登陆成功</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-variant-ligatures: no-common-ligatures">);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><div><span style="font-variant-ligatures: no-common-ligatures"> </span></div>