Find if user is in a call or not?

后端 未结 3 552
一向
一向 2020-12-21 01:58

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 n

相关标签:
3条回答
  • 2020-12-21 02:31

    Or, shorter (swift 5.1):

    private var isOnPhoneCall: Bool {
        return CXCallObserver().calls.contains { $0.hasEnded == false }
    }
    
    0 讨论(0)
  • 2020-12-21 02:34

    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.")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 02:36

    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
    }
    
    0 讨论(0)
提交回复
热议问题