Detect start of a phone call to send JSON in background

There are phone numbers and user can tap on one of them to make a call. When the call is successfully connected, the app should send a JSON to my server.

I have a problem - as soon as a phone number is tapped, the Phone app pops up and my app is sent to background. I've read the CoreTelephony Framework docs and CTCall class reference but I'm not sure what these four statuses are:

extern NSString const *CTCallStateDialing;
extern NSString const *CTCallStateIncoming;
extern NSString const *CTCallStateConnected;
extern NSString const *CTCallStateDisconnected;

Are they checked in the background or while the Phone app is running. How to detect it?

Should I use applicationDidEnterBackground method and CTCall class in it to fire a JSON ?


You should go for CTCallCenter and implement a callEventHandler that will be fired when the call status changes. Here is how to do it.

First, you will need a CTCallCenter instance:

@property(nonatomic, strong) CTCallCenter *callCenter;

Then, set a callEventHandler when your app launches.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        _callCenter = [[CTCallCenter alloc] init];

        [_callCenter setCallEventHandler:^(CTCall *call)
         {             
             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(@"Incoming");
             }
         }];  

    return YES;
}

About the callEventHandler :

This property's block object is dispatched on the default priority global dispatch queue when a call changes state. To handle such call events, define a handler block in your application and assign it to this property. You must implement the handler block to support being invoked from any context.

Source: CTCallCenter Class Reference

Hope it helps.

链接地址: http://www.djcxy.com/p/62868.html

上一篇: VB.NET WebBrowser禁用JavaScript

下一篇: 检测在后台发送JSON的电话开始