Le Trong Triet
Le Trong Triet

Reputation: 111

Using openTok after accept a Call (CallKit)

I am working on a project which requires opentok and callkit for notifying users. However, the application keeps crashing when openTok tries to connect to a session. IDK what is going on right now. Here is my work flow and codes:

Push notification about available opentok session -> start incoming call -> user accept the call -> start the opentok storyboard -> do some backend stuff -> connect to a session !!!! THIS IS WHEN IT CRASHES.

ERROR: Thread 1: EXC_BAD_ACCESS (code=2, address=0x1968a0ad8) enter image description here

Besides, I would like to ask for advice about receive notification. Instead of showing the notification on the screen. I would like to start the call, using callkit, like whatsapp or fb messenger. Please give me a hint about this also. Right now, I can only start the call when the app is in foreground when push notification is sent.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    let aps = userInfo["aps"] as! [String: AnyObject]
    // 1
    if aps["content-available"] as? Int == 1 {

        let uuid = UUID(uuidString: MyVariables.uuid)
        AppDelegate.shared.displayIncomingCall(uuid: uuid!, handle: "Sanoste", hasVideo: false) { _ in
        }
    }else  {
        return
    }

    completionHandler(UIBackgroundFetchResult.newData)
}


func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {

    action.fulfill()
    AppDelegate.shared.openTok()
}


func openTok() {

    let mainStoryboard = UIStoryboard(name: "callView", bundle: nil)
    let vc = mainStoryboard.instantiateViewController(withIdentifier: "callViewController") as! callViewController

    vc.view.frame = UIScreen.main.bounds
    UIView.transition(with: self.window!, duration: 0.5, options: .transitionCrossDissolve, animations: {
        self.window!.rootViewController = vc
    }, completion: nil)

}

// Join a session from MBE
func joinSession() {

    var error: OTError?

    pSession = OTSession(apiKey: openTokSessionKey.pApiKey, sessionId: openTokSessionKey.pSessionId, delegate: self as OTSessionDelegate)
    pSession?.connect(withToken: openTokSessionKey.pToken, error: &error)
    if error != nil {
        print(error!)
    }

    sSession = OTSession(apiKey: openTokSessionKey.sApiKey, sessionId: openTokSessionKey.sSessionId, delegate: self as OTSessionDelegate)
    sSession?.connect(withToken: openTokSessionKey.sToken, error: &error)
    if error != nil {
        print(error!)
    }
}

Anyone helps please ?

Upvotes: 0

Views: 649

Answers (1)

Sridhar Bollam
Sridhar Bollam

Reputation: 1204

This crash is a kind of warning. If you disable "Main Thread Sanitizer" in your project settings in Xcode 9 and rebuild your project, can fix the issue.

Upvotes: 1

Related Questions