Ios拦截手机短信程序

引用
1.手机要越狱,没有越狱的话,下面的可以不用看了!  2.IOS 要5.0以上,4.xx的同上  首先,声明下!由于公司移动开发的项目中,需要根据手机的内容进行逻辑处理,也就是要实现手机短信拦截,由于,本人一直搞的是JAVA,对OC 语言还是比较陌生的,这段辛酸路总算熬出个苗头!由于,公司中没有人搞这个,遂只能网爬了,郁闷的发现,网上的代码几乎不能运行,在朋友的帮助下,成功的对手机短信进行了拦截!下面贴下研究的心得,由于IT眼没有OC语言标签,下面贴的OC语言用C++代替! 
引用
项目首先,导入CoreTelephony.framework,OK 不需要别的包了,仅此而已!  在AppleDelegate.m中写上如下代码:
  1. //extern id allIncomingMessages;
  2. //extern int incomingMessageCount;
  3. extern NSString* const kCTSMSMessageReceivedNotification;
  4. extern NSString* const kCTSMSMessageReplaceReceivedNotification;
  5. extern NSString* const kCTSIMSupportSIMStatusNotInserted;
  6. extern NSString* const kCTSIMSupportSIMStatusReady;
  7. //typedef struct _CTCall CTCall;
  8. extern NSString *CTCallCopyAddress(void*, CTCall *);
  9. void* CTSMSMessageSend(id server,id msg);
  10. typedef struct __CTSMSMessage CTSMSMessage;
  11. NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);
  12. NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);
  13. int CTSMSMessageGetRecordIdentifier(void * msg);
  14. NSString * CTSIMSupportGetSIMStatus();
  15. NSString * CTSIMSupportCopyMobileSubscriberIdentity();
  16. id  CTSMSMessageCreate(void* unknow/*always 0*/,NSString* number,NSString* text);
  17. void * CTSMSMessageCreateReply(void* unknow/*always 0*/,void * forwardTo,NSString* text);
  18. id CTTelephonyCenterGetDefault(void);
  19. void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
  20. void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
  21. int CTSMSMessageGetUnreadCount(void);
引用
回调函数:
  1. static void callback(CFNotificationCenterRef center,void *observer,CFStringRef name,const void *object, CFDictionaryRef userInfo){
  2. //    NSLog(@"%@",name);
  3. NSString *strNotficationName=(NSString*)name;
  4. if ([strNotficationName isEqualToString:@"kCTMessageReceivedNotification"]) {
  5. int a=0;
  6. }
  7. //    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  8. @synchronized(nil) {
  9. if (!userInfo) return;
  10. if ([[(NSDictionary *)userInfo allKeys]
  11. containsObject:@"kCTMessageIdKey"]) // SMS Message
  12. {
  13. NSDictionary *info = (NSDictionary *)userInfo;
  14. CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageTypeKey"];
  15. int result;
  16. CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);
  17. Class CTTelephonyCenter=NSClassFromString(@"CTTelephonyCenter");
  18. Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");
  19. id mc = [CTMessageCenter sharedMessageCenter];
  20. int count=[mc incomingMessageCount];
  21. id mcarr=[mc allIncomingMessages];
  22. //        id incMsg =[mc incomingMessageWithId:result];
  23. //        if (count==0) {
  24. //            return;
  25. //        }
  26. id incMsg = [[mc allIncomingMessages] objectAtIndex:0];
  27. int msgType = (int)[incMsg messageType];
  28. if (msgType == 1) //experimentally detected number
  29. {
  30. id phonenumber = [incMsg sender];
  31. NSString *senderNumber = (NSString *)[phonenumber canonicalFormat];
  32. id incMsgPart = [[[[incMsg items] objectAtIndex:0] retain] retain];
  33. NSData *smsData = [[[incMsgPart data] retain] retain];
  34. //            NSString *smsText = (NSString*)[[NSString alloc] initWithData:smsData encoding:NSASCIIStringEncoding] ;
  35. NSString *smsText =    [NSString stringWithUTF8String:[smsData bytes]];
  36. NSLog(@"senderNumber = %@,text =%@",senderNumber,smsText);
  37. }
  38. }
  39. }
  40. //    [pool release];
  41. }
引用
注入监听:
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. // Override point for customization after application launch.
  5. self.window.backgroundColor = [UIColor whiteColor];
  6. [self.window makeKeyAndVisible];
  7. id ct = CTTelephonyCenterGetDefault();
  8. CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorDrop);
  9. }
上一篇:webapp 开发调试测试方法总结


下一篇:hdu2413(二分+二分匹配)