环信iOS开发攻城狮的集散地

iOS 1分钟集成环信单聊

2016-07-07  本文已影响1811人  阳光的大男孩儿

Time:2017年7月7
上一篇:环信集成SDK思路

前言:

1.为了实现一分钟,我们只需要知道怎么做实现单聊功能就好。至于为何可以自己看源码。
2.这种集成方式适合在新建的工程中,或者是 你添加的工程的第三方与环信本身中的第三方没有冲突的工程中。
3.这是集成了一位我的好朋友的代码,因此我将时间缩短,并且会一直更新代码。
4.你需要原来已经在环信官网注册拥有了自己的账号,并且已经创建好了一个工程拥有至少一个Appkey。
5.功能并不完善,因此要是看完善功能,建议看官方demo或者其他的博客文章资源。
6.目前版本是最新环信V3.0.准确点是V3.1.3版本。

一.资源准备:

1.下载资源官方demo。iOS SDK官方demo
2.下载资源ChatDemo-UI3.00-Simple
3.下载360云盘内容网址 (提取码:73a8)

二.集成过程:

1.新建工程“personChat”在桌面上。
2.解压ChatDemo-UI3.00-Simple会看到如下内容,并且将ChatSDK于ChatUI添加到自己的工程里面。

添加到工程.png
3.解压打开官方的demo,找到并将libHyphenateFullSDK.a 文件添加到目录【ChatSDK/HyphenateFullSDK/lib】下。(该目录是你刚刚拖进去工程中的目录)。

a.找到文件:


找到.a文件.png

b.添加到路径中:

添加到路径中.png

4.添加10个类库名字如下:

CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
MobileCoreServices.framework
ImageIO.framework
libc++.dylib
libz.dylib
libstdc++.6.0.9.dylib
libsqlite3.dylib
libiconv.dylib
添加类库

5.添加pch文件:将ChatDemo-UI3.00-Simple中的pch直接拖到自己的工程来。并且引用头文件。将pch中的红包功能注销掉。如下图:
a.引入pch文件:

pch添加

b.注销红包:


注销

6.把bitcode设置成NO,因为环信SDK不支持,如下:

bitcode设置为NO

7.comd+B编译运行。运行成功。
8.打开360云盘下载的demo。复制代码在AppDelegate.m 与 viewController里面。如下:
AppDelegate.m里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
self.window =[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor=[UIColor whiteColor];
    ViewController *VC = [[ViewController alloc]init];
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:VC];
    nav.navigationBar.backgroundColor =[UIColor whiteColor];
    self.window.rootViewController = nav;
    [self.window makeKeyWindow];
    //为了实现单聊功能,我们可以先忽略推送证书
    [[EaseSDKHelper shareHelper] easemobApplication:application
                      didFinishLaunchingWithOptions:launchOptions
                                             appkey:appKey
                                       apnsCertName:nil
                                        otherConfig:nil];
    
    EMError *error = [[EMClient sharedClient] registerWithUsername:@"1" password:@"1"];
    if (error==nil) {
        NSLog(@"注册成功");
    }else{
        NSLog(@"注册失败");//注册失败的原因往往是因为用户已经存在了,如果这样子就需要我们重新输入一个ID,重新注册
    }
    
    //此处是为了检查注册的账户能不能成功登陆成功。
    error = [[EMClient sharedClient] loginWithUsername:@"1" password:@"1"];
    if (!error) {
        NSLog(@"登录成功");
    }else {
        NSLog(@"登录失败");
    }

    return YES;
}

在viewcontroller.m 里面如下:(不要忘记引入头文件)

#import "ViewController.h"

#import "UserCacheManager.h"
#import "ChatViewController.h"
#import "ChatUIHelper.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *my =[UIButton buttonWithType:(UIButtonTypeCustom)];
    my.frame = CGRectMake(100, 200, 200, 100);
    my.backgroundColor =[UIColor greenColor];
    [self.view addSubview:my];
    [my addTarget:self action:@selector(myEvent) forControlEvents:UIControlEventTouchUpInside];

}

- (void)myEvent{
   //注意此处的用户名与你的注册用户名应该一致,同时用户名就是ID
    NSString *userName = @"1";
    NSString *pwd = @"1";
    
    [self showHudInView:self.view hint:@"Loading..."];
    
    [[EMClient sharedClient] asyncLoginWithUsername:userName password:pwd success:^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideHud];
            // 保存用户信息
            [UserCacheManager saveInfo:userName imgUrl:@"http://img.yxbao.com/news_img/201312/10/1386652628_1.jpg" nickName:@"哈哈"];
            
            //设置是否自动登录
            [[EMClient sharedClient].options setIsAutoLogin:YES];
            
            [[ChatUIHelper shareHelper] asyncGroupFromServer];
            [[ChatUIHelper shareHelper] asyncConversationFromDB];
            [[ChatUIHelper shareHelper] asyncPushOptions];
            
            //发送自动登陆状态通知
            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@([[EMClient sharedClient] isLoggedIn])];
            
            //在跳转的过程中需要写一个你已经注册好的用户ID,这样两个不同的用户ID才能实现单聊功能(我用了两个测试机测试,每一个测试机是一个ID)
            ChatViewController *vc = [[ChatViewController alloc]initWithConversationChatter:@"werqwe" conversationType:EMConversationTypeChat];
            [self.navigationController pushViewController:vc animated:YES];
            
        });
       
    } failure:^(EMError *aError) {
        
        NSLog(@"%@",aError.description);
        
    }];
    
}

9.最后更改plist文件允许访问http与https:

更改pist文件.png

10搞定了。我去我算了算好像超过1分钟了。(ps:写了一个半小时我去!)

环信IM互帮互助QQ群:340452063

上一篇下一篇

猜你喜欢

热点阅读