However,call events can also take place while your application is
suspended. While it is suspended,your application does not receive
call events. When your application resumes the active state,it
receives a single call event for each call that changed state
与这个问题有关的部分是
When your application resumes the active state,it receives a single
call event for each call that changed state
在应用程式被暂停时,应用程式会收到过去发生的通话的来电事件.这可以根据这个问题的答案:How does the Navita tem app get call log information?
我的问题是:如果我的应用程序被暂停并发生呼叫,那么当我的应用程序恢复活动状态时,如何检索呼叫发生的呼叫事件?
我已经尝试了许多很多代码实验,但是当我的应用程序恢复活动状态时,无法检索任何呼叫信息.
这是我尝试过的最简单的事情:
1)使用Xcode单视图应用程序模板创建一个新项目.
2)将以下代码添加到didFinishLaunchingWithOptions中
3)启动应用程序
4)远离应用程序的任务
5)从另一台设备拨打电话,接听电话,挂断电话
6)使应用程序返回到前台,从而恢复活动状态.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.callCenter = [[CTCallCenter alloc] init]; [self.callCenter setCallEventHandler:^(CTCall *call) { NSLog(@"Event handler called"); if ([call.callState isEqualToString: CTCallStateConnected]) { NSLog(@"Connected"); } else if ([call.callState isEqualToString: CTCallStateDialing]) { NSLog(@"Dialing"); } else if ([call.callState isEqualToString: CTCallStateDisconnected]) { NSLog(@"Disconnected"); } else if ([call.callState isEqualToString: CTCallStateIncoming]) { NSLog(@"Incomming"); } }]; return YES; }
使用此代码,如果应用程序在调用发生时处于前台,我可以获取调用事件.但是,如果我在进行呼叫之前离开了应用程序,那么当我的应用程序下一个恢复活动状态时,我无法获取通话事件 – 因为它应该在Apple文档中.
我试过的其他事情:
1)文档规定,在默认的优先级全局调度队列中调度块对象,所以我尝试将setCallEventHandler的注册置于dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {})中)
2)调用setCallEventHandler:在appBecameActive而不是didFinishLaunchingWithOptions
3)通过使用startUpdatingLocation或startMonitoringForSignificantLocationChanges通过beginBackgroundTaskWithExpirationHandler和/或位置更新向应用程序添加后台功能.
4)上述各种组合.
一旦我在设备上运行代码,就能够在应用程序被暂停时获得发生的呼叫事件,那么奖励将被授予.
这是在iOS 7上.
解决方法
基本上,我分配了两个这样的CTCallCenter实例
void (^block)(CTCall*) = ^(CTCall* call) { NSLog(@"%@",call.callState); }; -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { callCenter1 = [[CTCallCenter alloc] init]; callCenter1.callEventHandler = block; callCenter2 = [[CTCallCenter alloc] init]; callCenter2.callEventHandler = block; return YES; }
Swift中的相似代码:
func block (call:CTCall!) { println(call.callState) } func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Declare callcenter in the class like 'var callcenter = CTCallCenter()' callcenter.callEventHandler = block return true }
为了测试这个,我打电话,回答了它,然后把它挂起来,而应用程序是在后台.当我启动它,我收到3个呼叫事件:传入,连接,断开连接.