按下主页按钮

时间:2017-04-23 08:16:53

标签: ios swift

我正在开发一个应用程序,一旦应用程序进入后台(当用户点击iPhone中的主页按钮时)并接收并接听电话,我必须执行一些操作。

以下是我尝试过的代码,

callCenter = CTCallCenter()

        self.callCenter!.callEventHandler = {(call: CTCall) -> Void in
            if (call.callState == CTCallStateConnected)
            {
               //do something
            }

            else if (call.callState == CTCallStateDisconnected)
            {
               //do something
            }
        }
    }

- 当应用程序处于前台并接听电话时,将调用该事件,但当应用程序位于后台时,该事件不起作用。

任何使其有效的解决方案?

注意:我正在将应用程序提交到应用商店(iOS 9及更高版本,swift 2.3),因此启用VoIP功能对我不起作用,因为它可能会导致拒绝。

1 个答案:

答案 0 :(得分:-1)

您可以将代码放在AppDelegate.swift中的其中一种方法中:

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}