我的手机:iOS 5.1.1越狱使用Absynth2
我想做什么:
检测来电或拨打电话时…
好的,这是我的代码,我放在didEnterBackground下的AppDelegate,也尝试在didResignActive – 我没有得到任何错误,但我也没有得到任何结果..
callCenter = [[CTCallCenter alloc] init]; [callCenter setCallEventHandler:^(CTCall *call) { NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:@"callState"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"CTCallStateDidChange" object:nil userInfo:dict]; NSLog(@"state changed on call: %@",call); [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(callReceived:) name:CTCallStateIncoming object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(callEnded:) name:CTCallStatedisconnected object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(callConnected:) name:CTCallStateConnected object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(callDial:) name:CTCallStateDialing object:nil];
任何帮助表示赞赏.谢谢!
解决方法
问题是iOS显然没有在后台向UIApplications发送通知.从
iOS documentation for CTCallCenter:
If your application is active when a call event takes place,the
system dispatches the event to your handler immediately. However,call
events can also take place while your application is suspended. While
it is suspended,your application does not receive call events.
既然你被越狱了,为什么不让你的“app”成为一个发射守护进程呢?然后,它可以作为服务一直运行.如果你这样做,那么下面的代码应该得到你的通知(我在越狱的iOS 5.0.1 iPhone 4上测试了这个):
@property (nonatomic,strong) CTCallCenter* callCenter;
- (void) registerForCalls { self.callCenter = [[CTCallCenter alloc] init]; NSLog(@"registering for call center events"); [callCenter setCallEventHandler: ^(CTCall* call) { if ([call.callState isEqualToString: CTCallStateConnected]) { } else if ([call.callState isEqualToString: CTCallStateDialing]) { } else if ([call.callState isEqualToString: CTCallStatedisconnected]) { } else if ([call.callState isEqualToString: CTCallStateIncoming]) { } NSLog(@"\n\n callEventHandler: %@ \n\n",call.callState); }]; }
如果您之前没有这样做,那么这是good tutorial on how to create Launch Daemons.
如果您的应用程序还有一个图形组件,那么您可以构建两个部分:始终运行的启动守护程序,以及用户启动它时运行的UI应用程序.如果需要,他们可以通过通知相互通信.