Rajender Kumar
Rajender Kumar

Reputation: 1377

Call recording using iOS core telephony framework

I am making an emergency app. In which user can tap on call button, in case of emergency. I have used core telephony for getting call events as described by apple and AVFoundation framework for recording call audio. Here is my code:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupCallRecorder()
    }

func callConnected() {
        print("callConnected")
        self.audioRecorder?.record()
    }
    func callDisconnected() {
        print("callDisconnected")
        self.audioRecorder?.stop()
    }
    func setupCallRecorder() {

        let audioSession:AVAudioSession = AVAudioSession.sharedInstance()

        //ask for permission
        if (audioSession.respondsToSelector("requestRecordPermission:")) {
            AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
                if granted {
                    print("granted")

                    //set category and activate recorder session
                    try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
                    try! audioSession.setActive(true)

                    //get documnets directory
                    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
                    let soundFilePath = NSString(format: "%@/sound.caf", documentsDirectory)
                    let soundFileURL = NSURL.fileURLWithPath(soundFilePath as String)

                    //create AnyObject of settings
                    let settings: [String : AnyObject] = [
                        AVFormatIDKey:Int(kAudioFormatAppleIMA4), //Int required in Swift2
                        AVSampleRateKey:44100.0,
                        AVNumberOfChannelsKey:2,
                        AVEncoderBitRateKey:12800,
                        AVLinearPCMBitDepthKey:16,
                        AVEncoderAudioQualityKey:AVAudioQuality.Max.rawValue
                    ]

                    //record
                    try! self.audioRecorder = AVAudioRecorder(URL: soundFileURL, settings: settings)

                } else{
                    print("not granted")
                }
            })
        }

        let callCenter = CTCallCenter();
        callCenter.callEventHandler = { (call:CTCall!) in

            switch call.callState {
            case CTCallStateConnected:
                print("CTCallStateConnected")
                self.callConnected()
            case CTCallStateDisconnected:
                print("CTCallStateDisconnected")
                self.callDisconnected()
            default:
                //Not concerned with CTCallStateDialing or CTCallStateIncoming
                break
            }
        }
    }

But when call (iOS default call feature) is initiated from the app, my app goes to background and call event handler blocks are not called. I have read somewhere that call/audio recording will only work when your app is in foreground, so I have also tried by bringing my app to foreground once call is started, but even then its not working. Now I have following questions/concerns:

  1. Is it something that apple does not allow us to do?
  2. Apps can only record VOIP/SIP calls and not default calls?
  3. Is this possible to record calls in iOS without using any server tricks?

Note: This app is not for jail break devices and I want my app to upload on app store.

Upvotes: 0

Views: 3436

Answers (1)

creker
creker

Reputation: 9580

  1. It is something that many countries do not allow. Apple don't allow it at all and doesn't even have any private APIs to do that
  2. Apps can record their own stuff all they want given that they notify the user about it. Cellular network is completely different thing
  3. No. Even with jailbreak you have to inject your code into lower level parts of iOS audio stack and work with raw audio streams that go through the system.

Thus, there is no way you can get something like that into the AppStore.

Upvotes: 2

Related Questions