云信平台设置:
1、登录云信平台,开通信令和音视频通话(试用)。
2、测试阶段,建议将鉴权方式设置成调试模式。
1)点进功能配置,修改鉴权方式。
2)选择“调试模式”,无需NERTC Token验证,可以帮助我们更快的集成rtc功能。
3、语音呼叫时,会用到aps推送功能,因此平台需要我们上传推送证书。这里以上传p12为例,p12证书的产生,可以参考我的另一篇帖子:aps推送证书申请流程。
1)点击证书管理,上传推送证书。
2)点击“添加证书”,填写各项内容。已添加的证书,会在列表中显示。
4、此外,我们还需要两个测试账号,进行音视频通话。
1)点击“API调试”,通过API调试,创建两个测试账号。
2)随意输入accid和token,创建成功会返回200,保存创建成功的accid和token备用。
代码集成:
1、编写Podfile文件,因为笔者需要修改UI较多,故使用本地依赖。您可以依照自己的需求设置。
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'YXDemo2' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#登录组件
pod 'YXLogin', '1.0.0'
#可选UI库
# pod 'NEContactUIKit', '9.4.0'
# pod 'NEConversationUIKit', '9.4.0'
# pod 'NEChatUIKit', '9.4.0'
# pod 'NETeamUIKit', '9.4.0'
#可选Kit库(和UIKit对应)
pod 'NEContactKit', '9.4.0'
pod 'NEConversationKit', '9.4.0'
pod 'NEChatKit', '9.4.0'
pod 'NETeamKit', '9.4.0'
#基础kit库
pod 'NECommonUIKit', '9.4.0'
pod 'NECommonKit', '9.4.0'
pod 'NECoreIMKit', '9.4.0'
pod 'NECoreKit', '9.4.0'
#扩展库
# pod 'NEMapKit', '9.4.0'
#呼叫组件,音视频通话能力,需要开通 音视频2.0,可选,聊天一面会根据依赖初始化自动显示音视频通话入口
# pod 'NERtcCallUIKit', '1.8.2'
pod 'NERtcCallKit', '1.8.2'
pod 'NERtcSDK', '4.6.29'
# 如果需要查看UI部分源码请注释掉以上在线依赖,打开下面的本地依赖
pod 'NEContactUIKit', :path => 'NEContactUIKit/NEContactUIKit.podspec'
pod 'NEConversationUIKit', :path => 'NEConversationUIKit/NEConversationUIKit.podspec'
pod 'NETeamUIKit', :path => 'NETeamUIKit/NETeamUIKit.podspec'
pod 'NEChatUIKit', :path => 'NEChatUIKit/NEChatUIKit.podspec'
pod 'NEMapKit', :path => 'NEMapKit/NEMapKit.podspec'
pod 'NERtcCallUIKit', :path => 'NERtcCallUIKit/NERtcCallUIKit.podspec'
end
#fix bug in Xcode 14
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'RSKPlaceholderTextView'
target.build_configurations.each do |config|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
end
end
end
end
#⚠️如果pod依赖报错,可打开以下注释
#post_install do |installer|
# installer.pods_project.targets.each do |target|
# target.build_configurations.each do |config|
# config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
# end
# end
#end
2、将官方demo里的UI源码库,拷贝到项目根目录。
3、执行pod update,首次安装可能需要多次install才能成功。
4、进行初始化操作。
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[IMManager shared] initIM];
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
static let shared = IMManager()
let account = "10086"
let token = "a9efaa4d25ac531536bfba0a0bee54b5"
func initIM() {
// init
let option = NIMSDKOption()
option.appKey = "您的appKey"
option.apnsCername = pushCerName
IMKitClient.instance.setupCoreKitIM(option)
relogin()
//添加登录状态监听
NIMSDK.shared().loginManager.add(self)
//添加IM接受监听
NIMSDK.shared().chatManager.add(self)
NEKeyboardManager.shared.enable = true
NEKeyboardManager.shared.shouldResignOnTouchOutside = true
setupCall()
}
func relogin() {
NIMSDK.shared().loginManager.login(account, token: token) { error in
print("IM loginManager,登录成功")
}
}
//MARK: 呼叫
func setupCall() {
//呼叫组件初始化
let option = NERtcCallOptions()
option.apnsCerName = pushCerName
option.isDisableLog = true
let uiConfig = NERtcCallUIConfig()
uiConfig.option = option
uiConfig.appKey = kAppKey
uiConfig.uiConfig.showCallingSwitchCallType = option.supportAutoJoinWhenCalled
NERtcCallKit.sharedInstance().timeOutSeconds = 30
NERtcCallUIKit.sharedInstance().setup(with: uiConfig)
}
5、注册推送,实现代理。
func registerAPNS(){
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.badge, .sound, .alert]) { grant, error in
if grant == false {
DispatchQueue.main.async {
UIApplication.shared.keyWindow?.makeToast(NSLocalizedString("open_push", comment: ""))
}
}
}
} else {
let setting = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
}
UIApplication.shared.registerForRemoteNotifications()
UIApplication.shared.applicationIconBadgeNumber = 0
}
func applicationWillEnterForeground(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NIMSDK.shared().updateApnsToken(deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NELog.infoLog("app delegate : ", desc: error.localizedDescription)
}
Xcode设置:
1、为项目添加推送能力。
2、配置摄像头和麦克风隐私权限。
Privacy - Camera Usage Description
——需要您的同意,才能使用摄像头,以便于相机拍摄,上传、发布照片
Privacy - Microphone Usage Description
——需要您的同意,才能使用麦克风,以便于视频录制、语音识别、语音聊天
3、填写Bundle ID,设置好证书,连接真机运行,跑通项目。
4、在两部手机上,分别使用上文创建好的两个测试账号运行,跳到聊天页面,就可以发起音视频通话了。