如何在后台状态下获取传入/传出呼叫事件 [英] How to get incoming/outgoing call event in background state

查看:103
本文介绍了如何在后台状态下获取传入/传出呼叫事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,它具有播放声音的功能,这是我成功实现的。即使应用程序正在运行(前台状态)并且我们收到来电,应用音乐也会停止并在呼叫断开时再次恢复。

In one of my app it has a feature of playing sound that I achieved successfully. Even though when app is running (foreground state) and we received the incoming call, app music gets stopped and resume again when call gets disconnected.

现在真正的问题在这里。当应用程序进入后台状态时,我们没有收到任何传入/传出呼叫事件。在后台模式中如果音乐正在我的应用程序内播放并且我们接到任何来电,则应用音乐会自动停止,但与iPhone音乐应用程序不同,当呼叫断开时不会再次恢复。

Now real problem is here. When app enters in the background state, we are not receiving any event for incoming/outgoing call. In the background mode If music is playing inside my app and we get any incoming call, then app music is stopped automatically but not resume again when call disconnected unlike iPhone Music app.

这是iOS的限制还是我们可以实现的?

注意:我不是在寻找对于越狱设备或企业应用程序的任何解决方案

Note: I'm not looking for any solution for Jailbreak devices or Enterprise apps

推荐答案

您是否尝试在AppDelegate类中创建呼叫中心并分配处理程序块?以下必须工作。

Have you tried to create call center and assign handler block in AppDelegate class? The following has to work.

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let callCenter: CTCallCenter = CTCallCenter()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()

        callCenter.callEventHandler = {

            (call: CTCall!) in

                switch call.callState {

                    case CTCallStateConnected:

                        print("CTCallStateConnected")

                    case CTCallStateDisconnected:

                        print("CTCallStateDisconnected")

                    case CTCallStateIncoming:

                        print("CTCallStateIncoming")

                    default:

                        print("default")

                }

        }

        return true

    }

}

不要忘记打开背景模式这个。并在后台执行某些操作,就像接收位置一样。

Do not forget to switch on Background Modes for this. And perform something in the background as well, smth like receiving location.

这篇关于如何在后台状态下获取传入/传出呼叫事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆