我目前正在尝试编写一个应用程序来拦截文本消息并根据该消息的内容做出反应。我试图挂钩_receivedMessage:(struct __CKSMSRecord *)message replace:(BOOL)replace
CKSMSService 类中的方法,但这似乎根本没有被调用。
有人可以告诉我我必须加入什么功能/类吗?我需要在文本消息显示并存储到数据库之前拦截它。我在 IOS 5.0.1 上。
任何帮助都非常感谢。
此代码段应拦截 SMS 消息 - 您可以将其扩展为其他类型的通知。也适用于 iOS 5.0.1。但不适用于 iMessage。与 CoreTelephony 框架链接(那里有一堆私有标头,您可以对其进行分类转储)
#include <dlfcn.h>
#define CORETELPATH "/System/Library/PrivateFrameworks/CoreTelephony.framework/CoreTelephony"
id(*CTTelephonyCenterGetDefault)();
void (*CTTelephonyCenterAddObserver) (id,id,CFNotificationCallback,NSString*,void*,int);
static void telephonyEventCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString *notifyname=(NSString *)name;
if ([notifyname isEqualToString:@"kCTMessageReceivedNotification"])//received SMS
{
NSLog(@" SMS Notification Received :kCTMessageReceivedNotification");
// Do blocking here.
}
}
-(void) registerCallback {
void *handle = dlopen(CORETELPATH, RTLD_LAZY);
CTTelephonyCenterGetDefault = dlsym(handle, "CTTelephonyCenterGetDefault");
CTTelephonyCenterAddObserver = dlsym(handle,"CTTelephonyCenterAddObserver");
dlclose(handle);
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(
ct,
NULL,
telephonyEventCallback,
NULL,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
尽管张贴者已经接受了rajagp 的回答,但我很确定它在 iOS 5 上并没有解决问题实际提出的问题。对于 iOS 5,我不再看到消息内容,尽管我确实收到了有新消息的通知。
因此,我所做的是将 rajagp 的通知处理程序用于kCTMessageReceivedNotification
,并在其中使用此处发布的代码从 SMS 数据库中实际获取文本消息的内容。
这仍然适用于 iOS 7,但我发现您在收到 kCTMessageReceivedNotification 通知后需要稍微延迟。否则你会错过刚刚收到的短信。我使用 0.1 秒的延迟,带有 [self performSelector .. afterDelay:0.1];