短信与电话的拦截

原文地址:http://blog.csdn.net/laigb/article/details/6617264


首先添加coreTelephony.framework

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #include <notify.h>  
  3. #include <stdio.h>  
  4. #include <stdarg.h>  
  5. #include <string.h>  
  6. typedef struct __CTSMSMessage CTSMSMessage;  
  7. NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);  
  8. NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);  
  9. id CTTelephonyCenterGetDefault(void);  
  10. void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);  
  11. void dolog(id formatstring,...)  
  12. {  
  13.     va_list arglist;  
  14.     if (formatstring)  
  15.     {  
  16.         va_start(arglist, formatstring);  
  17.         id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];  
  18.         printf("%s\n", [outstring UTF8String]);  
  19.         va_end(arglist);  
  20.     }  
  21. }  
  22. static void callback(CFNotificationCenterRef center,  
  23.                      void *observer, CFStringRef name,  
  24.                      const void *object, CFDictionaryRef userInfo)  
  25. {  
  26.       
  27.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  28.     // printf("NOTIFICATION: %s\n", [name UTF8String]);  
  29.     if (!userInfo) return;  
  30.       
  31.     NSDictionary *info = (NSDictionary*)userInfo;  
  32.     int dcount = CFDictionaryGetCount(userInfo);  
  33.     id keys = [(NSDictionary*)userInfo allKeys];  
  34.     int i;  
  35.     for (i = 0; i < dcount; i++)  
  36.     {  
  37.         id key = [keys objectAtIndex:i];  
  38.         dolog(@"  %@: %@", key, [info objectForKey:key]);  
  39.     }      
  40.       
  41.       
  42.     if ([[(NSDictionary *)userInfo allKeys]  
  43.          containsObject:@"kCTSMSMessage"]) // SMS Message  
  44.     {  
  45.         CTSMSMessage *message = (CTSMSMessage *)  
  46.         [(NSDictionary *)userInfo objectForKey:@"kCTSMSMessage"];  
  47.         NSString *address = CTSMSMessageCopyAddress(NULL, message);  
  48.         NSString *text = CTSMSMessageCopyText(NULL, message);  
  49.         NSArray *lines = [text componentsSeparatedByString:@"\n"];  
  50.           
  51.         printf("  %s %d\n", [address cString], [lines count]);  
  52.         printf("  %s\n", [text cString]);  
  53.         fflush(stdout);  
  54.     }  
  55.       
  56.     [pool release];  
  57.       
  58.     return ;  
  59. }  
  60. static void signalHandler(int sigraised)  
  61. {  
  62.     printf("\nInterrupted.\n");  
  63.     exit(0);  
  64. }  
  65. int main(int argc, char **argv)  
  66. {  
  67.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  68.       
  69.     // Initialize listener by adding CT Center observer implicit  
  70.     id ct = CTTelephonyCenterGetDefault();  
  71.     CTTelephonyCenterAddObserver( ct, NULL, callback,NULL,NULL,  
  72.                                  CFNotificationSuspensionBehaviorHold);  
  73.       
  74.     // Handle Interrupts  
  75.     sig_t oldHandler = signal(SIGINT, signalHandler);  
  76.     if (oldHandler == SIG_ERR)  
  77.     {  
  78.         printf("Could not establish new signal handler");  
  79.         exit(1);  
  80.     }  
  81.       
  82.     // Run loop lets me catch notifications  
  83.     printf("Starting run loop and watching for notification.\n");  
  84.     CFRunLoopRun();  
  85.       
  86.     // Shouldn't ever get here. Bzzzt  
  87.     printf("Unexpectedly back from CFRunLoopRun()!\n");  
  88.     [pool release];  
  89. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值