我没有测试您的代码,但我认为您的问题可能是您需要使用 Core Telephony 通知中心来注册该事件(不是您评论中代码中的内容)。像这样的东西:
// register for all Core Telephony notifications
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, // center
NULL, // observer
telephonyEventCallback, // callback
NULL, // event name (or all)
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
你的回调函数是
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");
}
}
}
我在此类似问题中提供了另一种技术。另外,请注意我在该问题中关于未在UIApplication
已放入后台的通知中的评论。
更新:请参阅下面关于kCTCallStatus
在 iOS 6 上使用而不是kCTCall
.