Difference between revisions of "CoreTelephony"

From The iPhone Wiki
Jump to: navigation, search
(New page: CoreTelephony is the iPhone framework used for high-level cell modem interaction. It handles SMS, MMS, phone calls, etc. It is written in C, C++, and in 3.0, Objective-C portions were ad...)
 
(Added example code to listen for incoming SMS messages)
 
Line 14: Line 14:
 
CFStringRef CTSMSMessageCopyText(void *, CTSMSMessage *);
 
CFStringRef CTSMSMessageCopyText(void *, CTSMSMessage *);
 
CTSMSMessage* CTSMSMessageCreate(void*,CFStringRef number, CFStringRef name);
 
CTSMSMessage* CTSMSMessageCreate(void*,CFStringRef number, CFStringRef name);
 
   
 
====Objective-C====
 
====Objective-C====
Line 20: Line 19:
 
[[CTMessageCenter sharedMessageCenter] sendSMSWithText:message serviceCenter:nil toAddress:number];
 
[[CTMessageCenter sharedMessageCenter] sendSMSWithText:message serviceCenter:nil toAddress:number];
 
where message and number are NSStrings filled with the correct data.
 
where message and number are NSStrings filled with the correct data.
  +
  +
=====Experimental code to listen for incoming SMS=====
  +
<code>
  +
static void callback(CFNotificationCenterRef center, void *observer, NSString* name, const void *object, NSDictionary* info) {
  +
fprintf(stderr, "Notification intercepted: %s\n", [name UTF8String]);
  +
if([name isEqualToString:@"kCTMessageReceivedNotification"] && info)
  +
{
  +
NSNumber* messageType = [info valueForKey:@"kCTMessageTypeKey"];
  +
if([messageType isEqualToNumber:[NSNumber numberWithInt:1/*empirically determined!*/]])
  +
{
  +
NSNumber* messageID = [info valueForKey:@"kCTMessageIdKey"];
  +
CTMessageCenter* mc = [CTMessageCenter sharedMessageCenter];
  +
CTMessage* msg = [mc incomingMessageWithId:[messageID intValue]];
  +
NSObject<CTMessageAddress>* phonenumber = [msg sender];
  +
  +
NSString *senderNumber = (NSString*)[phonenumber canonicalFormat];
  +
NSString *sender = (NSString*)[phonenumber encodedString];
  +
CTMessagePart* msgPart = [[msg items] objectAtIndex:0]; //for single-part msgs
  +
NSData *smsData = [msgPart data];
  +
NSString *smsText = [[NSString alloc] initWithData:smsData encoding:NSUTF8StringEncoding];
  +
fprintf(stderr, "SMS Message from %s / %s: \"%s\"\n",[senderNumber UTF8String],[sender UTF8String],[smsText UTF8String]);
  +
}
  +
}
  +
return;
  +
}
  +
  +
int main(int argc, char **argv)
  +
{
  +
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  +
  +
id ct = CTTelephonyCenterGetDefault();
  +
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);
  +
  +
// Start the run loop. Now we'll receive notifications.
  +
[[NSRunLoop currentRunLoop] run];
  +
[pool drain];
  +
printf("Unexpectedly back from CFRunLoopRun()!\n");
  +
[pool release];
  +
}
  +
</code>

Latest revision as of 21:21, 18 August 2010

CoreTelephony is the iPhone framework used for high-level cell modem interaction. It handles SMS, MMS, phone calls, etc. It is written in C, C++, and in 3.0, Objective-C portions were added.

Usage

In order to use CoreTelephony, you need to set up your build environment to link against it.
You also need a set of header files, which can be obtained by using the class-dump tool on the framework.

C functions/types

 typedef struct __CTSMSMessage CTSMSMessage;
 void*                CTTelephonyCenterGetDefault();
 void                 CTSMSMessageSend(void*,CTSMSMessage*);
 CFStringRef          CTSMSMessageCopyAddress(void *, CTSMSMessage *);
 CFStringRef          CTSMSMessageCopyText(void *, CTSMSMessage *);
 CTSMSMessage*        CTSMSMessageCreate(void*,CFStringRef number, CFStringRef name);

Objective-C

The ObjC parts are much easier to work with because of the magic of class-dump. The only work I've done is getting an SMS message sent out. This can be accomplished in one line of code:

 [[CTMessageCenter sharedMessageCenter]  sendSMSWithText:message serviceCenter:nil toAddress:number];

where message and number are NSStrings filled with the correct data.

Experimental code to listen for incoming SMS

 static void callback(CFNotificationCenterRef center, void *observer, NSString* name, const void *object, NSDictionary* info) {
 fprintf(stderr, "Notification intercepted: %s\n", [name UTF8String]);
 if([name isEqualToString:@"kCTMessageReceivedNotification"] && info)
 {
   NSNumber* messageType = [info valueForKey:@"kCTMessageTypeKey"];
   if([messageType isEqualToNumber:[NSNumber numberWithInt:1/*empirically determined!*/]])
   {
     NSNumber* messageID = [info valueForKey:@"kCTMessageIdKey"];
     CTMessageCenter* mc = [CTMessageCenter sharedMessageCenter];
     CTMessage* msg = [mc incomingMessageWithId:[messageID intValue]];
     NSObject<CTMessageAddress>* phonenumber = [msg sender];
     
     NSString *senderNumber = (NSString*)[phonenumber canonicalFormat];
     NSString *sender = (NSString*)[phonenumber encodedString];
     CTMessagePart* msgPart = [[msg items] objectAtIndex:0]; //for single-part msgs
     NSData *smsData = [msgPart data];
     NSString *smsText = [[NSString alloc] initWithData:smsData encoding:NSUTF8StringEncoding];
     fprintf(stderr, "SMS Message from %s / %s: \"%s\"\n",[senderNumber UTF8String],[sender UTF8String],[smsText UTF8String]);
   }
 }
 return;
 }
 int main(int argc, char **argv)
 {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
 id ct = CTTelephonyCenterGetDefault();
 CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);
 
 // Start the run loop. Now we'll receive notifications.
 [[NSRunLoop currentRunLoop] run];
 [pool drain];
 printf("Unexpectedly back from CFRunLoopRun()!\n");
 [pool release];
 }