user1871869 user1871869 - 6 months ago 57
Swift Question

Find if user is in a call or not?

I wanted to see if a user was using the application and to see if they were in a phone call or not. I was following this link to see check if a user was in a phone call or not: iOS How to check if currently on phone call. However, this looks like it's for Objective-C. I was wondering if there was a Swift equivalent for this. This is my attempt:

var currCall = CTCallCenter()
var call = CTCall()

for call in currCall.currentCalls{
if call.callState == CTCallStateConnected{
println("In call.")
}
}


However, it doesn't seem as if call has an attribute
.callState
like how it does in the previous example. Any help would be appreciated! Thanks!

Answer

Update for Swift 2.2: you just have to safely unwrap currCall.currentCalls.

import CoreTelephony
let currCall = CTCallCenter()

if let calls = currCall.currentCalls {
    for call in calls {
        if call.callState == CTCallStateConnected {
            print("In call.")
        }
    }
}

Previous answer: you need to safely unwrap and to tell what type it is, the compiler doesn't know.

import CoreTelephony
let currCall = CTCallCenter()

if let calls = currCall.currentCalls as? Set<CTCall> {
    for call in calls {
        if call.callState == CTCallStateConnected {
            println("In call.")
        }
    }
}