引用
1.手機要越獄,沒有越獄的話,下面的可以不用看了!
2.IOS 要5.0以上,4.xx的同上 首先,聲明下!由于公司移動開發的項目中,需要根據手機的內容進行邏輯處理,也就是要實現手機短信攔截,由于,本人一直搞的是java,對OC 語言還是比較陌生的,這段辛酸路總算熬出個苗頭!由于,公司中沒有人搞這個,遂只能網爬了,郁悶的發現,網上的代碼幾乎不能運行,在朋友的幫助下,成功的對手機短信進行了攔截!下面貼下研究的心得,由于IT眼沒有OC語言標簽,下面貼的OC語言用C++代替! 引用
項目首先,導入CoreTelephony.framework,OK 不需要別的包了,僅此而已!
在AppleDelegate.m中寫上如下代碼: -
- extern NSString* const kCTSMSMessageReceivedNotification;
- extern NSString* const kCTSMSMessageReplaceReceivedNotification;
- extern NSString* const kCTSIMSupportSIMStatusNotInserted;
- extern NSString* const kCTSIMSupportSIMStatusReady;
-
-
- extern NSString *CTCallCopyAddress(void*, CTCall *);
- void* CTSMSMessageSend(id server,id msg);
- typedef struct __CTSMSMessage CTSMSMessage;
- NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);
- NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);
-
- int CTSMSMessageGetRecordIdentifier(void * msg);
- NSString * CTSIMSupportGetSIMStatus();
- NSString * CTSIMSupportCopyMobileSubscriberIdentity();
- id CTSMSMessageCreate(void* unknow
- void * CTSMSMessageCreateReply(void* unknow
-
- id CTTelephonyCenterGetDefault(void);
- void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
- void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
- int CTSMSMessageGetUnreadCount(void);
引用
回調函數:
- static void callback(CFNotificationCenterRef center,void *observer,CFStringRef name,const void *object, CFDictionaryRef userInfo){
-
-
- NSString *strNotficationName=(NSString*)name;
-
-
- if ([strNotficationName isEqualToString:@"kCTMessageReceivedNotification"]) {
- int a=0;
- }
-
- @synchronized(nil) {
- if (!userInfo) return;
- if ([[(NSDictionary *)userInfo allKeys]
- containsObject:@"kCTMessageIdKey"])
- {
-
-
- NSDictionary *info = (NSDictionary *)userInfo;
- CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageTypeKey"];
- int result;
- CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);
- Class CTTelephonyCenter=NSClassFromString(@"CTTelephonyCenter");
-
- Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");
- id mc = [CTMessageCenter sharedMessageCenter];
- int count=[mc incomingMessageCount];
- id mcarr=[mc allIncomingMessages];
-
-
-
-
- id incMsg = [[mc allIncomingMessages] objectAtIndex:0];
-
- int msgType = (int)[incMsg messageType];
-
- if (msgType == 1)
- {
- id phonenumber = [incMsg sender];
-
- NSString *senderNumber = (NSString *)[phonenumber canonicalFormat];
- id incMsgPart = [[[[incMsg items] objectAtIndex:0] retain] retain];
- NSData *smsData = [[[incMsgPart data] retain] retain];
- NSString *smsText = [NSString stringWithUTF8String:[smsData bytes]];
-
- NSLog(@"senderNumber = %@,text =%@",senderNumber,smsText);
- }
-
- }
-
- }
-
-
-
-
- }
引用
注入監聽:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
-
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- id ct = CTTelephonyCenterGetDefault();
- CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorDrop);
- }