xcode iOS 5 - CTCallCenter 不适合我
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11215482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 5 - CTCallCenter not working for me
提问by Aramik Gharachehdaghi
My phone: iOS 5.1.1 Jailbroken using Absynth2
我的手机:iOS 5.1.1 越狱,使用 Absynth2
What I'm trying to do: detect an incoming call or when a call is being dialed...
我正在尝试做的是: 检测来电或拨打电话时...
Okay here is my code that i placed inside the AppDelegate
under didEnterBackground
, also tried in didResignActive
- i don't get any errors but i also don't get any results..
好的,这是我放在AppDelegate
under 中的代码didEnterBackground
,也尝试过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];
any help is appreciated. thanks!
任何帮助表示赞赏。谢谢!
回答by Nate
The problem is that iOS apparently doesn't deliver the notifications to UIApplications in the background. From the iOS documentation for CTCallCenter:
问题是 iOS 显然不会在后台向 UIApplications 发送通知。来自CTCallCenter的iOS 文档:
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.
如果您的应用程序在调用事件发生时处于活动状态,系统会立即将事件分派给您的处理程序。但是,当您的应用程序挂起时,呼叫事件也可能发生。当它暂停时,您的应用程序不会接收呼叫事件。
Since you are jailbroken, why not make your "app" a launch daemon? Then, it can run all the time as a service. If you do this, then the following code should get your notifications (I tested this on a jailbroken iOS 5.0.1 iPhone 4):
既然你越狱了,为什么不让你的“应用程序”成为启动守护程序?然后,它可以作为服务一直运行。如果你这样做,那么下面的代码应该会收到你的通知(我在越狱的 iOS 5.0.1 iPhone 4 上测试了这个):
@property (nonatomic, strong) CTCallCenter* callCenter;
and register for notifications with:
并注册通知:
- (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);
}];
}
Here's a good tutorial on how to create Launch Daemons, if you haven't done that before.
如果您以前没有做过,这里有一个关于如何创建 Launch Daemons的很好的教程。
If you also have a graphical component to your app, then you can build two parts: the launch daemon to run all the time, and the UI app that runs when the user launches it. They can communicate with each other with notifications, if need be.
如果您的应用程序还有一个图形组件,那么您可以构建两个部分:始终运行的启动守护程序,以及在用户启动它时运行的 UI 应用程序。如果需要,他们可以通过通知相互通信。
回答by Luan Nguyen
If you want your app always run in the background, then you have to make your app Voip app. You can also do a little trick, which makes your app play a infinite silent music when it goes background.
如果您希望您的应用程序始终在后台运行,那么您必须制作您的应用程序 Voip 应用程序。您还可以做一个小技巧,让您的应用在进入后台时播放无限无声的音乐。