1

My phone: iOS 5.1.1 Jailbroken using 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..

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!

Nate
  • 31,017
  • 13
  • 83
  • 207

2 Answers2

5

The problem is that iOS apparently doesn't deliver the notifications to UIApplications in the background. From the 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.

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):

@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.

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Does it mean, that if I want to develop an application that every time that the user gets a call it will send an sms - it wouldn't work? Is there another way to do it? – Dejell Mar 07 '13 at 11:05
  • @Odelya, are you building for the App Store, for jailbroken phones, or for non-jailbroken phones, but you won't be submitting to the App Store? – Nate Mar 08 '13 at 02:22
  • I am building for the AppStore. – Dejell Mar 10 '13 at 07:14
  • Then this won't work for you. An App Store app can't be running in the background at all times, which it would need to do to be able to **always** be listening for incoming calls. This question was about `jailbreak` development. With jailbreak apps, you can make your app a **Launch Daemon**, which allows it to listen for calls, or do anything else, as soon as the phone boots up. There is no time limit on how long a Launch Daemon can run, but you can't build Launch Daemons for the App Store. – Nate Mar 10 '13 at 08:21
  • @Nate : I want to [disconnect the call programmatically in ios for jailbroken iphone](http://stackoverflow.com/q/19279588/1603072). Your comment gave me the hope that we can do that. Can you give me the Answer with the detail description of how it can be done and which are the stpes that i should follow to implement these things ? – Bhavin Oct 09 '13 at 18:16
0

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.