当前位置:   article > 正文

iOS5 拦截短信_ctmessagecenterdelegate

ctmessagecenterdelegate
http://blog.csdn.net/laigb/article/details/6617264

引用

项目首先,导入CoreTelephony.framework,OK 不需要别的包了,仅此而已!
在AppleDelegate.m中写上如下代码:
C++代码   收藏代码
  1. //extern id allIncomingMessages;  
  2. //extern int incomingMessageCount;  
  3.   
  4. extern NSString* const kCTSMSMessageReceivedNotification;  
  5. extern NSString* const kCTSMSMessageReplaceReceivedNotification;  
  6. extern NSString* const kCTSIMSupportSIMStatusNotInserted;  
  7. extern NSString* const kCTSIMSupportSIMStatusReady;  
  8.   
  9.   
  10. //typedef struct _CTCall CTCall;  
  11. extern NSString *CTCallCopyAddress(void*, CTCall *);   
  12. void* CTSMSMessageSend(id server,id msg);  
  13. typedef struct __CTSMSMessage CTSMSMessage;  
  14. NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);  
  15. NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);  
  16.   
  17. int CTSMSMessageGetRecordIdentifier(void * msg);  
  18. NSString * CTSIMSupportGetSIMStatus();  
  19. NSString * CTSIMSupportCopyMobileSubscriberIdentity();  
  20. id  CTSMSMessageCreate(void* unknow/*always 0*/,NSString* number,NSString* text);  
  21. void * CTSMSMessageCreateReply(void* unknow/*always 0*/,void * forwardTo,NSString* text);  
  22.   
  23. id CTTelephonyCenterGetDefault(void);  
  24. void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);  
  25. void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);  
  26. int CTSMSMessageGetUnreadCount(void);   

引用

回调函数:
C++代码   收藏代码
  1. static void callback(CFNotificationCenterRef center,void *observer,CFStringRef name,const void *object, CFDictionaryRef userInfo){  
  2.       
  3. //    NSLog(@"%@",name);  
  4.      
  5.     NSString *strNotficationName=(NSString*)name;  
  6.       
  7.       
  8.     if ([strNotficationName isEqualToString:@"kCTMessageReceivedNotification"]) {  
  9.         int a=0;      
  10.     }  
  11.       
  12. //    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  13.      @synchronized(nil) {  
  14.         if (!userInfo) return;  
  15.         if ([[(NSDictionary *)userInfo allKeys]  
  16.              containsObject:@"kCTMessageIdKey"]) // SMS Message  
  17.         {  
  18.   
  19.               
  20.             NSDictionary *info = (NSDictionary *)userInfo;  
  21.             CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageTypeKey"];  
  22.             int result;  
  23.             CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);   
  24.             Class CTTelephonyCenter=NSClassFromString(@"CTTelephonyCenter");  
  25.               
  26.             Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");  
  27.             id mc = [CTMessageCenter sharedMessageCenter];  
  28.             int count=[mc incomingMessageCount];  
  29.             id mcarr=[mc allIncomingMessages];  
  30.     //        id incMsg =[mc incomingMessageWithId:result];  
  31.     //        if (count==0) {  
  32.     //            return;  
  33.     //        }  
  34.             id incMsg = [[mc allIncomingMessages] objectAtIndex:0];  
  35.               
  36.             int msgType = (int)[incMsg messageType];  
  37.               
  38.             if (msgType == 1) //experimentally detected number  
  39.             {  
  40.                 id phonenumber = [incMsg sender];  
  41.                   
  42.                 NSString *senderNumber = (NSString *)[phonenumber canonicalFormat];  
  43.             id incMsgPart = [[[[incMsg items] objectAtIndex:0] retain] retain];  
  44.             NSData *smsData = [[[incMsgPart data] retain] retain];  
  45. //            NSString *smsText = (NSString*)[[NSString alloc] initWithData:smsData encoding:NSASCIIStringEncoding] ;  
  46.               NSString *smsText =    [NSString stringWithUTF8String:[smsData bytes]];  
  47.   
  48.                 NSLog(@"senderNumber = %@,text =%@",senderNumber,smsText);  
  49.             }  
  50.           
  51.         }  
  52.           
  53.     }  
  54.   
  55. //    [pool release];  
  56.       
  57.       
  58.   
  59. }  

引用

注入监听:
C++代码   收藏代码
  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. }  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/525866
推荐阅读
相关标签
  

闽ICP备14008679号