短信与电话的拦截

首先添加coreTelephony.framework

#import <UIKit/UIKit.h>
#include <notify.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
typedef struct __CTSMSMessage CTSMSMessage;
NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);
NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);
id CTTelephonyCenterGetDefault(void);
void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
void dolog(id formatstring,...)
{
    va_list arglist;
    if (formatstring)
    {
        va_start(arglist, formatstring);
        id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        printf("%s\n", [outstring UTF8String]);
        va_end(arglist);
    }
}
static void callback(CFNotificationCenterRef center,
                     void *observer, CFStringRef name,
                     const void *object, CFDictionaryRef userInfo)
{
    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // printf("NOTIFICATION: %s\n", [name UTF8String]);
    if (!userInfo) return;
    
    NSDictionary *info = (NSDictionary*)userInfo;
    int dcount = CFDictionaryGetCount(userInfo);
    id keys = [(NSDictionary*)userInfo allKeys];
    int i;
    for (i = 0; i < dcount; i++)
    {
        id key = [keys objectAtIndex:i];
        dolog(@"  %@: %@", key, [info objectForKey:key]);
    }    
    
    
    if ([[(NSDictionary *)userInfo allKeys]
         containsObject:@"kCTSMSMessage"]) // SMS Message
    {
        CTSMSMessage *message = (CTSMSMessage *)
        [(NSDictionary *)userInfo objectForKey:@"kCTSMSMessage"];
        NSString *address = CTSMSMessageCopyAddress(NULL, message);
        NSString *text = CTSMSMessageCopyText(NULL, message);
        NSArray *lines = [text componentsSeparatedByString:@"\n"];
        
        printf("  %s %d\n", [address cString], [lines count]);
        printf("  %s\n", [text cString]);
        fflush(stdout);
    }
    
    [pool release];
    
    return ;
}
static void signalHandler(int sigraised)
{
    printf("\nInterrupted.\n");
    exit(0);
}
int main(int argc, char **argv)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    // Initialize listener by adding CT Center observer implicit
    id ct = CTTelephonyCenterGetDefault();
    CTTelephonyCenterAddObserver( ct, NULL, callback,NULL,NULL,
                                 CFNotificationSuspensionBehaviorHold);
    
    // Handle Interrupts
    sig_t oldHandler = signal(SIGINT, signalHandler);
    if (oldHandler == SIG_ERR)
    {
        printf("Could not establish new signal handler");
        exit(1);
    }
    
    // Run loop lets me catch notifications
    printf("Starting run loop and watching for notification.\n");
    CFRunLoopRun();
    
    // Shouldn't ever get here. Bzzzt
    printf("Unexpectedly back from CFRunLoopRun()!\n");
    [pool release];
}


评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值