Intercepting phone call - iPhone (correct method to hook in CoreTelephony)

 ̄綄美尐妖づ 提交于 2019-11-27 14:14:57
Nate

I didn't test your code, but I think your problem might be that you need to use the Core Telephony notification center to register for that event (not what you had in the code in your comment). Something like this:

// register for all Core Telephony notifications
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct,   // center
                             NULL, // observer
                             telephonyEventCallback,  // callback
                             NULL,                    // event name (or all)
                             NULL,                    // object
                             CFNotificationSuspensionBehaviorDeliverImmediately);

and your callback function is

static void telephonyEventCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    NSString *notifyname = (NSString*)name;
    if ([notifyname isEqualToString:@"kCTCallIdentificationChangeNotification"])
    {
        NSDictionary* info = (NSDictionary*)userInfo;
        CTCall* call = (CTCall*)[info objectForKey:@"kCTCall"];
        NSString* caller = CTCallCopyAddress(NULL, call);

        if (call.callState == CTCallStateDisconnected)
        {
            NSLog(@"Call has been disconnected");
        }
        else if (call.callState == CTCallStateConnected)
        {
            NSLog(@"Call has just been connected");
        }
        else if (call.callState == CTCallStateIncoming)
        {
            NSLog(@"Call is incoming");
        }
        else if (call.callState == CTCallStateDialing)
        {
            NSLog(@"Call is Dialing");
        }
        else
        {
            NSLog(@"None of the conditions");
        }
    }
}

I offer another technique in this similar question here. Also, note my comment in that question about not getting the notifications in a UIApplication that has been put into the background.

Update: see cud_programmer's comment below about using kCTCallStatus on iOS 6 instead of kCTCall.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!