检测IOS 10中的GSM呼叫状态(Swift 3,Xcode 8)和来自后台状态的通知 [英] Detecting GSM Call States in IOS 10 (Swift 3, Xcode 8) and Notification from Background state
问题描述
TLDR:从后台检测来电结束事件
请参阅以下问题的更新:
TLDR: Detect call end event from background Please see update to the question below:
是否可以检测到/使用来自背景状态的Swift在IOS 10中获取呼叫状态事件。在早期版本中有一个 CORE Telephony API,但现在似乎已经弃用了。
Is it possible to detect/get an event for call state in IOS 10 using Swift from background state. In the earlier versions there was a CORE Telephony api but this seems to be deprecated now.
我见过 CallKit Api 但它表示它适用于基于VOIP的呼叫。我需要获得正常CDMA / GSM呼叫的状态,而不是基于VOIP的呼叫,并且当呼叫结束时,只需向服务器发出通知。我不需要访问任何可能是隐私问题的数据点,只需要一个事件,当呼叫结束时,然后我的应用程序将是一个后台应用程序将启动。而已。有关如何执行此操作的任何指示?
I have seen the CallKit Api but it says that it is for VOIP based calls. I need to get the state of normal CDMA/GSM calls, not VOIP based calls and when the call ends just fire a notification to server. I do not need to access any data points which can be a privacy issue, just need an event for when the call ends and then my app which would be a background app would fire up. Thats it. Any pointers on how to do this ?
更新:使用CallKit Api我能够获得呼叫状态,但无法区分voip和正常的GSM通话。我可以用它。我也可以通过以下方式获得本地通知。
UPDATE: Using CallKit Api i am able to get call states though not able to distinguish between voip and normal GSM calls. I can work with that. I have also been able to get a local notification by doing the below.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
application.beginBackgroundTask(withName: "showN", expirationHandler:nil)
return true
}
extension AppDelegate: CXCallObserverDelegate {
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if call.hasEnded == true {
print("Disconnected")
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:60,repeats:false)
let identifier = "MyCallLocalNotification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error{
print(error)
}
})
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(AppDelegate.showN), userInfo: nil, repeats: false)
print("Done")
}
if call.isOutgoing == true && call.hasConnected == false {
print("Dialing")
}
if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
print("Incoming")
}
if call.hasConnected == true && call.hasEnded == false {
print("Connected")
}
}
}
和函数showN as:
and the function showN as:
func showN(){
let identifier = "MyCallLocalNotification"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:1,repeats:false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error{
print(error)
}
})
但是(正如预期的那样)当应用程序已经在后台工作很长时间(超过3分钟)时,这不起作用或屏幕被锁定时。我觉得我几乎在那里,但遗漏了一些东西。另外认为这不是一种正确的方法,或者必须有更好的(swift / IOS标准)方法来做到这一点。请建议。已经失去了50个赏金积分:)
However (as expected), this does not work when the app has been in background for a long time (more than 3 minutes) or when the screen is locked. I feel i am almost there but missing something. Also think this is not a proper way to do this or there must be a better (swift/IOS standard) way to do this. Please Suggest. Have already lost 50 bounty points :)
推荐答案
很长一段时间后看起来这在iOS 10中可能不可行。确实尝试使用建议的背景模式,但我既不需要播放音乐,也不需要一直跟踪位置,因此它们不适合我。
After a long while it looks like this may not be feasible in iOS 10. I did try using the background modes as suggested but neither i need to play music nor keep tracking the location at all times hence they do not work out for me.
我不能使用 Push Kit
,因为目的是为正常的GSM通话而不是voip通话获取事件。
I cannot use Push Kit
because the intent is to get events for normal GSM calls and not voip calls.
这篇关于检测IOS 10中的GSM呼叫状态(Swift 3,Xcode 8)和来自后台状态的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!