Find if user is in a call or not?

十年热恋 提交于 2019-12-01 18:53:12

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.")
        }
    }
}

iOS 10, Swift 3

import CallKit

/**
    Returns whether or not the user is on a phone call
*/
private func isOnPhoneCall() -> Bool {
    for call in CXCallObserver().calls {
        if call.hasEnded == false {
            return true
        }
    }
    return false
}

Or, shorter (swift 5.1):

private var isOnPhoneCall: Bool {
    return CXCallObserver().calls.contains { $0.hasEnded == false }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!