项目首先,导入CoreTelephony.framework,OK 不需要别的包了,仅此而已!
发送很简单:
[ [CTMessageCenter sharedMessageCenter] sendSMSWithText:@"1111" serviceCenter:nil toAddress:@"138XXXX0610"];
截取:
在AppleDelegate.m中写上如下代码:
//extern id allIncomingMessages;
//extern int incomingMessageCount;
extern NSString* const kCTSMSMessageReceivedNotification;
extern NSString* const kCTSMSMessageReplaceReceivedNotification;
extern NSString* const kCTSIMSupportSIMStatusNotInserted;
extern NSString* const kCTSIMSupportSIMStatusReady;
//typedef struct _CTCall CTCall;
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/*always 0*/,NSString* number,NSString* text);
void * CTSMSMessageCreateReply(void* unknow/*always 0*/,void * forwardTo,NSString* text);
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){
// NSLog(@"%@",name);
NSString *strNotficationName=(NSString*)name;
if ([strNotficationName isEqualToString:@"kCTMessageReceivedNotification"]) {
int a=0;
}
// NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@synchronized(nil) {
if (!userInfo) return;
if ([[(NSDictionary *)userInfo allKeys]
containsObject:@"kCTMessageIdKey"]) // SMS Message
{
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 incomingMessageWithId:result];
// if (count==0) {
// return;
// }
id incMsg = [[mc allIncomingMessages] objectAtIndex:0];
int msgType = (int)[incMsg messageType];
if (msgType == 1) //experimentally detected number
{
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*)[[NSString alloc] initWithData:smsData encoding:NSASCIIStringEncoding] ;
NSString *smsText = [NSString stringWithUTF8String:[smsData bytes]];
NSLog(@"senderNumber = %@,text =%@",senderNumber,smsText);
}
}
}
// [pool release];
}
注入监听:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorDrop);
}
获取IMEI
在项目中加入:Message.framework
创建头文件network.h
从http://ericasadun.com/iPhoneDocs300/_network_controller_8h-source.html复制头文件代码
// Message/NetWorkController.h
@class NSString, NSTimer;
@interface NetworkController : NSObject
{
struct __SCDynamicStore *_store;
NSString *_domainName;
unsigned int _waitingForDialToFinish:1;
unsigned int _checkedNetwork:1;
unsigned int _isNetworkUp:1;
unsigned int _isFatPipe:1;
unsigned int _edgeRequested:1;
NSTimer *_notificationTimer;
}
+ (id)sharedInstance;
- (void)dealloc;
- (id)init;
- (BOOL)isNetworkUp;
- (BOOL)isFatPipe;
- (BOOL)inAirplaneMode;
- (id)domainName;
- (BOOL)isHostReachable:(id)fp8;
- (id)primaryEthernetAddressAsString;
- (id)IMEI;
- (id)edgeInterfaceName;
- (BOOL)isEdgeUp;
- (void)bringUpEdge;
- (void)keepEdgeUp;
- (void *)createPacketContextAssertionWithIdentifier:(id)fp8;
@end
#import "network.h"
// 获得IMEI
NetworkController *ntc = [NetworkController sharedInstance];
NSString *imeistring = [ntc IMEI];
// show IMEI
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your IMEI Is"
message:imeistring
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];