Skip to content

Commit edc900f

Browse files
simonphamSimon Pham
andauthored
fix: ios crashes (#168)
How to reproduce the crash: 1. Start a call. 2. Accept the call from iOS app immediately after ending the call from remote side. This should fix the issue #167 --------- Co-authored-by: Simon Pham <cuong.pham@fullertonhealth.com>
1 parent 56d8f21 commit edc900f

1 file changed

Lines changed: 120 additions & 8 deletions

File tree

ios/Classes/SwiftAgoraRtmPlugin.swift

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ import AgoraRtmKit
1212
public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
1313
var registrar: FlutterPluginRegistrar!
1414
var methodChannel: FlutterMethodChannel!
15-
15+
1616
var nextClientIndex: Int = 0
1717
var clients: [Int: RTMClient] = [:]
18-
18+
1919
public static func register(with registrar: FlutterPluginRegistrar) {
2020
let channel = FlutterMethodChannel(name: "io.agora.rtm", binaryMessenger: registrar.messenger())
2121
let instance = SwiftAgoraRtmPlugin()
2222
instance.methodChannel = channel
2323
instance.registrar = registrar
2424
registrar.addMethodCallDelegate(instance, channel: channel)
2525
}
26-
26+
2727
public func detachFromEngine(for registrar: FlutterPluginRegistrar) {
2828
methodChannel.setMethodCallHandler(nil)
2929
clients.removeAll()
3030
}
31-
31+
3232
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
3333
let methodName = call.method
3434
guard let callArguments = call.arguments as? [String: Any] else {
@@ -49,35 +49,59 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
4949
result(FlutterMethodNotImplemented)
5050
}
5151
}
52-
52+
5353
func handleCallManagerMethod(_ methodName: String?, _ params: [String: Any?]?, _ result: @escaping FlutterResult) {
5454
if let clientIndex = params?["clientIndex"] as? Int, let agoraClient = clients[clientIndex], let callManager = agoraClient.call?.manager {
5555
let args = params?["args"] as? [String: Any?]
56+
if (agoraClient.call == nil) {
57+
result(["errorCode": -1])
58+
return
59+
}
5660
switch methodName {
5761
case "createLocalInvitation":
5862
let calleeId = args?["calleeId"] as? String
63+
if (calleeId == nil) {
64+
result(["errorCode": -1])
65+
return
66+
}
5967
let localInvitation = AgoraRtmLocalInvitation(calleeId: calleeId!)
6068
agoraClient.call!.localInvitations[localInvitation.hash] = localInvitation
6169
result(["errorCode": 0, "result": localInvitation.toJson()])
6270
case "sendLocalInvitation":
6371
let localInvitation = (args?["localInvitation"] as? [String: Any?])?.toLocalInvitation(agoraClient.call!)
72+
if (localInvitation == nil) {
73+
result(["errorCode": -1])
74+
return
75+
}
6476
callManager.send(localInvitation!) {
6577
result(["errorCode": $0.rawValue])
6678
}
6779
case "acceptRemoteInvitation":
6880
let remoteInvitation = (args?["remoteInvitation"] as? [String: Any?])?.toRemoteInvitation(agoraClient.call!)
81+
if (remoteInvitation == nil) {
82+
result(["errorCode": -1])
83+
return
84+
}
6985
callManager.accept(remoteInvitation!) {
7086
agoraClient.call!.remoteInvitations.removeValue(forKey: remoteInvitation!.hash)
7187
result(["errorCode": $0.rawValue])
7288
}
7389
case "refuseRemoteInvitation":
7490
let remoteInvitation = (args?["remoteInvitation"] as? [String: Any?])?.toRemoteInvitation(agoraClient.call!)
91+
if (remoteInvitation == nil) {
92+
result(["errorCode": -1])
93+
return
94+
}
7595
callManager.refuse(remoteInvitation!) {
7696
agoraClient.call!.remoteInvitations.removeValue(forKey: remoteInvitation!.hash)
7797
result(["errorCode": $0.rawValue])
7898
}
7999
case "cancelLocalInvitation":
80100
let localInvitation = (args?["localInvitation"] as? [String: Any?])?.toLocalInvitation(agoraClient.call!)
101+
if (localInvitation == nil) {
102+
result(["errorCode": -1])
103+
return
104+
}
81105
callManager.cancel(localInvitation!) {
82106
agoraClient.call!.localInvitations.removeValue(forKey: localInvitation!.hash)
83107
result(["errorCode": $0.rawValue])
@@ -87,7 +111,7 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
87111
}
88112
}
89113
}
90-
114+
91115
func handleStaticMethod(_ methodName: String?, _ params: [String: Any?]?, _ result: @escaping FlutterResult) {
92116
switch methodName {
93117
case "createInstance":
@@ -112,7 +136,7 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
112136
result(FlutterMethodNotImplemented)
113137
}
114138
}
115-
139+
116140
func handleClientMethod(_ methodName: String?, _ params: [String: Any?]?, _ result: @escaping FlutterResult) {
117141
if let clientIndex = params?["clientIndex"] as? Int, let agoraClient = clients[clientIndex], let client = agoraClient.client {
118142
let args = params?["args"] as? [String: Any?]
@@ -123,6 +147,10 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
123147
case "login":
124148
let token = args?["token"] as? String
125149
let userId = args?["userId"] as? String
150+
if userId == nil {
151+
result(["errorCode": -1])
152+
return
153+
}
126154
client.login(byToken: token, user: userId!) {
127155
result(["errorCode": $0.rawValue])
128156
}
@@ -134,39 +162,67 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
134162
let peerId = args?["peerId"] as? String
135163
let message = args?["message"] as? [String: Any?] ?? [:]
136164
let options = args?["options"] as? [String: Any?] ?? [:]
165+
if peerId == nil {
166+
result(["errorCode": -1])
167+
return
168+
}
137169
client.send(message.toRtmMessage(), toPeer: peerId!, sendMessageOptions: options.toSendMessageOptions()) {
138170
result(["errorCode": $0.rawValue])
139171
}
140172
case "createChannel":
141173
let channelId = args?["channelId"] as? String
174+
if channelId == nil {
175+
result(["errorCode": -1])
176+
return
177+
}
142178
let agoraRtmChannel = RTMChannel(clientIndex, channelId!, registrar.messenger())
143179
let channel = client.createChannel(withId: channelId!, delegate: agoraRtmChannel)
144180
agoraClient.channels[channelId!] = channel
145181
result(["errorCode": 0])
146182
case "queryPeersOnlineStatus":
147183
let peerIds = args?["peerIds"] as? [String]
184+
if peerIds == nil {
185+
result(["errorCode": -1])
186+
return
187+
}
148188
client.queryPeersOnlineStatus(peerIds!) {
149189
result(["errorCode": $1.rawValue, "result": $0?.reduce(into: [String: Int]()) {
150190
$0[$1.peerId] = $1.state.rawValue
151191
}])
152192
}
153193
case "subscribePeersOnlineStatus":
154194
let peerIds = args?["peerIds"] as? [String]
195+
if peerIds == nil {
196+
result(["errorCode": -1])
197+
return
198+
}
155199
client.subscribePeersOnlineStatus(peerIds!) {
156200
result(["errorCode": $0.rawValue])
157201
}
158202
case "unsubscribePeersOnlineStatus":
159203
let peerIds = args?["peerIds"] as? [String]
204+
if peerIds == nil {
205+
result(["errorCode": -1])
206+
return
207+
}
160208
client.unsubscribePeersOnlineStatus(peerIds!) {
161209
result(["errorCode": $0.rawValue])
162210
}
163211
case "queryPeersBySubscriptionOption":
164212
let option = args?["option"] as? Int
213+
if option == nil {
214+
result(["errorCode": -1])
215+
return
216+
}
165217
client.queryPeers(bySubscriptionOption: AgoraRtmPeerSubscriptionOptions(rawValue: option!)!) {
166218
result(["errorCode": $1.rawValue, "result": $0])
167219
}
168220
case "renewToken":
169221
let token = args?["token"] as? String
222+
if token == nil {
223+
result(["errorCode": -1])
224+
return
225+
}
170226
client.renewToken(token!) {
171227
result(["errorCode": $1.rawValue, "result": $0])
172228
}
@@ -182,6 +238,10 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
182238
}
183239
case "deleteLocalUserAttributesByKeys":
184240
let keys = args?["keys"] as? [String]
241+
if keys == nil {
242+
result(["errorCode": -1])
243+
return
244+
}
185245
client.deleteLocalUserAttributes(byKeys: keys!) {
186246
result(["errorCode": $0.rawValue])
187247
}
@@ -191,80 +251,132 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
191251
}
192252
case "getUserAttributes":
193253
let userId = args?["userId"] as? String
254+
if userId == nil {
255+
result(["errorCode": -1])
256+
return
257+
}
194258
client.getUserAllAttributes(userId!) {
195259
result(["errorCode": $2.rawValue, "result": $0?.toJson(), "userId": $1])
196260
}
197261
case "getUserAttributesByKeys":
198262
let userId = args?["userId"] as? String
199263
let keys = args?["keys"] as? [String]
264+
if userId == nil || keys == nil {
265+
result(["errorCode": -1])
266+
return
267+
}
200268
client.getUserAttributes(userId!, byKeys: keys!) {
201269
result(["errorCode": $2.rawValue, "result": $0?.toJson(), "userId": $1])
202270
}
203271
case "setChannelAttributes":
204272
let channelId = args?["channelId"] as? String
205273
let attributes = args?["attributes"] as? [[String: Any?]] ?? []
206274
let options = args?["options"] as? [String: Any?] ?? [:]
275+
if channelId == nil {
276+
result(["errorCode": -1])
277+
return
278+
}
207279
client.setChannel(channelId!, attributes: attributes.toRtmChannelAttributeList(), options: options.toChannelAttributeOptions()) {
208280
result(["errorCode": $0.rawValue])
209281
}
210282
case "addOrUpdateChannelAttributes":
211283
let channelId = args?["channelId"] as? String
212284
let attributes = args?["attributes"] as? [[String: Any?]] ?? []
213285
let options = args?["options"] as? [String: Any?] ?? [:]
286+
if channelId == nil {
287+
result(["errorCode": -1])
288+
return
289+
}
214290
client.addOrUpdateChannel(channelId!, attributes: attributes.toRtmChannelAttributeList(), options: options.toChannelAttributeOptions()) {
215291
result(["errorCode": $0.rawValue])
216292
}
217293
case "deleteChannelAttributesByKeys":
218294
let channelId = args?["channelId"] as? String
219295
let keys = args?["keys"] as? [String]
220296
let options = args?["options"] as? [String: Any?] ?? [:]
297+
if channelId == nil || keys == nil {
298+
result(["errorCode": -1])
299+
return
300+
}
221301
client.deleteChannel(channelId!, attributesByKeys: keys!, options: options.toChannelAttributeOptions()) {
222302
result(["errorCode": $0.rawValue])
223303
}
224304
case "clearChannelAttributes":
225305
let channelId = args?["channelId"] as? String
226306
let options = args?["options"] as? [String: Any?] ?? [:]
307+
if channelId == nil {
308+
result(["errorCode": -1])
309+
return
310+
}
227311
client.clearChannel(channelId!, options: options.toChannelAttributeOptions()) {
228312
result(["errorCode": $0.rawValue])
229313
}
230314
case "getChannelAttributes":
231315
let channelId = args?["channelId"] as? String
316+
if channelId == nil {
317+
result(["errorCode": -1])
318+
return
319+
}
232320
client.getChannelAllAttributes(channelId!) {
233321
result(["errorCode": $1.rawValue, "result": $0?.toJson()])
234322
}
235323
case "getChannelAttributesByKeys":
236324
let channelId = args?["channelId"] as? String
237325
let keys = args?["keys"] as? [String]
326+
if channelId == nil || keys == nil {
327+
result(["errorCode": -1])
328+
return
329+
}
238330
client.getChannelAttributes(channelId!, byKeys: keys!) {
239331
result(["errorCode": $1.rawValue, "result": $0?.toJson()])
240332
}
241333
case "getChannelMemberCount":
242334
let channelIds = args?["channelIds"] as? [String]
335+
if channelIds == nil {
336+
result(["errorCode": -1])
337+
return
338+
}
243339
client.getChannelMemberCount(channelIds!) {
244340
result(["errorCode": $1.rawValue, "result": $0?.toJson()])
245341
}
246342
case "setParameters":
247343
let parameters = args?["parameters"] as? String
344+
if parameters == nil {
345+
result(["errorCode": -1])
346+
return
347+
}
248348
let errorCode = client.setParameters(parameters!)
249349
result(["errorCode": errorCode])
250350
case "setLogFile":
251351
let filePath = args?["filePath"] as? String
352+
if filePath == nil {
353+
result(["errorCode": -1])
354+
return
355+
}
252356
let errorCode = client.setLogFile(filePath!)
253357
result(["errorCode": errorCode])
254358
case "setLogFilter":
255359
let filter = args?["filter"] as? Int
360+
if filter == nil {
361+
result(["errorCode": -1])
362+
return
363+
}
256364
let errorCode = client.setLogFilters(AgoraRtmLogFilter(rawValue: filter!)!)
257365
result(["errorCode": errorCode])
258366
case "setLogFileSize":
259367
let fileSizeInKBytes = args?["fileSizeInKBytes"] as? Int32
368+
if fileSizeInKBytes == nil {
369+
result(["errorCode": -1])
370+
return
371+
}
260372
let errorCode = client.setLogFileSize(fileSizeInKBytes!)
261373
result(["errorCode": errorCode])
262374
default:
263375
result(FlutterMethodNotImplemented)
264376
}
265377
}
266378
}
267-
379+
268380
func handleChannelMethod(_ methodName: String?, _ params: [String: Any?]?, _ result: @escaping FlutterResult) {
269381
if let clientIndex = params?["clientIndex"] as? Int, let channelId = params?["channelId"] as? String, let agoraClient = clients[clientIndex], let channel = agoraClient.channels[channelId] {
270382
let args = params?["args"] as? [String: Any?]

0 commit comments

Comments
 (0)