Reputation: 1377
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:
Note: This app is not for jail break devices and I want my app to upload on app store.
Upvotes: 0
Views: 3436
Reputation: 9580
Thus, there is no way you can get something like that into the AppStore.
Upvotes: 2