YXMessageManager.swift
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// YXMessageManager.swift
// ParentAssistant
//
// Created by 葛建军 on 2018/3/15.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
import CoreData
class YXMessageManager: NSObject,NIMLoginManagerDelegate,NIMSystemNotificationManagerDelegate {
static let share=YXMessageManager()
private override init() {}
func addYXDelegate(){
//添加云信代理
NIMSDK.shared().loginManager.add(YXMessageManager.share as NIMLoginManagerDelegate)
NIMSDK.shared().systemNotificationManager.add(YXMessageManager.share as NIMSystemNotificationManagerDelegate)
}
//登录云信
func loginWithYX(account:String,token:String,completion:((Bool)->Void)?){
NIMSDK.shared().loginManager.login(account, token: token) { (error) in
var b:Bool
if error==nil {
//登录成功
b=true
}else{
//登录失败
b=false
}
completion?(b)
}
}
func autoLogin(account:String,token:String){
NIMSDK.shared().loginManager.autoLogin(account, token: token)
}
//退出登录
func loginOut(completion:((Bool)->Void)?){
NIMSDK.shared().loginManager.logout { (error) in
var b:Bool
if error==nil {
//退出成功
b=true
}else{
//退出失败
b=false
}
completion?(b)
}
}
/**
* 被踢(服务器/其他端)回调
*
* @param code 被踢原因
* @param clientType 发起踢出的客户端类型
*/
func onKick(_ code: NIMKickReason, clientType: NIMLoginClientType) {
var reason="您的帐号已在别处登录"
switch (code) {
case NIMKickReason.byClient:
var clientName=""
switch (clientType) {
case NIMLoginClientType.typeAOS:
clientName="Android";
case NIMLoginClientType.typeiOS:
clientName="iOS";
case NIMLoginClientType.typePC:
clientName="电脑";
case NIMLoginClientType.typeWeb:
clientName="网页";
default:
clientName="";
}
reason = clientName == "" ? "你的帐号在其他设备上登录,请注意帐号信息安全,若非本人操作请及时修改密码或联系客服人员" : "你的帐号在\(clientName)端登录,请注意帐号信息安全,若非本人操作请及时修改密码或联系客服人员"
break
case NIMKickReason.byClientManually:
break
default: //code=NIMKickReason.ByServer:
reason="你被服务器踢下线"
break;
}
appDelegate.window!.makeToast("下线通知:\(reason)", duration: 3, position: CSToastPositionCenter)
AccountManager.shared.logOut(auto: true)
}
/**
* 登录回调
*
* @param step 登录步骤
* @discussion 这个回调主要用于客户端UI的刷新
*/
func onLogin(_ step: NIMLoginStep) {
}
/**
* 自动登录失败回调
*
* @param error 失败原因
* @discussion 自动重连不需要上层开发关心,但是如果发生一些需要上层开发处理的错误,SDK 会通过这个方法回调
* 用户需要处理的情况包括:AppKey 未被设置,参数错误,密码错误,多端登录冲突,账号被封禁,操作过于频繁等
*/
func onAutoLoginFailed(_ error: Error) {
AccountManager.shared.logOut()
}
func onReceive(_ notification: NIMCustomSystemNotification) {
if let info=JSON.fromString(notification.content) {
if info["type"].stringValue=="attach"{//群发通知
let attachItem = NSManagedObject.creatWith(identifier: "Message") as! Message
attachItem.date=Date(timeIntervalSince1970: notification.timestamp)
attachItem.content = info["content"].stringValue
attachItem.ownId = AccountManager.shared.userid
attachItem.save()
NotificationCenter.default.post(name: Notification.Name(rawValue: MessageNotification.receiveMessage), object: nil, userInfo: nil)
}else if info["type"].stringValue=="ordermess"{//订购通知
let attachItem = NSManagedObject.creatWith(identifier: "Message") as! Message
attachItem.date=Date(timeIntervalSince1970: notification.timestamp)
attachItem.type = "ordermess"
attachItem.content = info["content"].stringValue
attachItem.ownId = AccountManager.shared.userid
attachItem.save()
NotificationCenter.default.post(name: Notification.Name(rawValue: MessageNotification.receiveMessage), object: nil, userInfo: nil)
}
}
}
}