版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wa172126691/article/details/80708495
说明:目前swift环信集成 通过pod方式集成后打开聊天界面会一直报如下的错,目前还没找到解决方式,但是同手动集成 sdk以及EaseUI就可以正常使用,本片博客,介绍的就是通过手动方式集成,如果哪个同僚解决了通过 pod方式集成报的错误,请联系我:我的微信:13022861472
第一步,从环信官网下载环信 SDK
2.将 ‘HyphenateFullSDK’导入项目
HyphenateFullSDK:是包含音视频的
HyphenateSDK:是不包含音视频的
3.向General → Embedded Binaries 中添加依赖库
添加动态库:需要在TARGETS -> General -> Embedded Binaries 中添加动态库,
如果 Linked Frameworks and Libraries 中有两个 ‘Hyphenate.framework’库,就删掉一个
4. 添加其它第三方的依赖库
在 Build Phases ->Linked Frameworks and Libraries处添加
这些是要添加的库(11个)
Pods_EMTest除外
5.配置一些信息
在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据。此处设置为暂时退回到http协议。
6.导入EaseUI库
bitcode是被编译程序的一种中间形式的代码。包含bitcode配置的程序将会在App store上被编译和链接。bitcode允许苹果在后期重新优化程序的二进制文件,而不需要重新提交一个新的版本到App store上;第三方SDK没有支持Bitcode包就选no
7.创建桥接文件
8.创建 pch文件
9.继承 EaseMessageViewController 实现聊天界面
//
// ChatViewController.swift
// HuanXinDome
//
//
import UIKit
class ChatViewController: EaseMessageViewController {
//跟对方聊天时,调用的初始化方法,第一个参数为对方的环信注册的id ,第二个参数为聊天的类型 EMConversationTypeChat:单聊
override init!(conversationChatter: String!, conversationType: EMConversationType) {
super.init(conversationChatter: conversationChatter, conversationType: conversationType)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
9.初始化,登录,进行聊天
//
// AppDelegate.swift
// HuanXinDome
//
// Created by 陕西帮你电子科技有限公司 on 2018/6/15.
// Copyright © 2018年 陕西帮你电子科技有限公司. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//环信配置
let options:EMOptions = EMOptions.init(appkey: "申请的appkey")
options.apnsCertName = "创建的推送证书名字"
let error = EMClient.shared().initializeSDK(with: options)
if error == nil{
print("环信初始化成功")
let emId = "13233253173"//登录的环信id
let emPwd = "pd13233253173"//密码
let error = EMClient.shared().login(withUsername: emId, password: emPwd)
if error == nil{
self.window = UIWindow.init(frame: UIScreen.main.bounds)
print("登录成功")
let dui_emId = "15991788992" //要聊的对方的环信id
//打开聊天界面与对方聊天(第一个参数为聊天对方的id,第二个参数为聊天的类型(此处为单聊))
let vc = ChatViewController.init(conversationChatter: dui_emId, conversationType: EMConversationTypeChat)
vc?.title = "你正在和15991788992聊天"
let nav = UINavigationController.init(rootViewController: vc!)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
}
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
完成
Dome地址