[Code Snippet] Send SMS with CoreTelephony [Work in Progress]

Discussion in 'iOS Development' started by Cyanidepoison, Jul 5, 2009.

  1. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    So I've been working on reversing CT for a couple of hours, and here is all the information I've gained:

    Code:
    typedef struct     __CTSMSMessage CTSMSMessage;
    CTSMSMessage* CTSMSMessageCreate(void*,CFStringRef, CFStringRef);
    void                  CTSMSMessageSend(void*,CTSMSMessage*);
    CFStringRef        CTSMSMessageCopyAddress(void *, CTSMSMessage *);
    CFStringRef        CTSMSMessageCopyText(void *, CTSMSMessage *);
    
    Ok, so this may seem a bit confusing, but it really isn't. Make a CTSMSMessage object with CTSMSMessageCreate and get the telephony center like this:
    Code:
    CTSMSMessage *msg = CTSMSMessageCreate(NULL,CFSTR("PHONE NUMBER"),CFSTR("MESSAGE TEXT"));
    void *ct = CTTelephonyCenterGetDefault(); 
    
    and then send it like this:
    Code:
    CTSMSMessageSend(ct,msg);
    

    Credit to Jeepston for helping with the last bit of reversing!
  2. Tyr0

    Tyr0 New Member

    Joined:
    Dec 17, 2007
    Messages:
    533
    Likes Received:
    6
    Device:
    iPhone 3GS (White)
    Hmm... nice code! I actually needed this 6 months ago, but now i can finish the app!
  3. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    This is for 2.x, by the way.

    I'll have information available for 3.x very soon.
  4. Jeepston

    Jeepston New Member

    Joined:
    May 17, 2009
    Messages:
    45
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    As I mentioned previously, this function doesn't create proper record in sms.db. The problem is in field named group_id, which associates particular SMS with a conversation thread in MobileSMS. So, if group_id is empty or incorrect (isn't present in group_member table of sms.db), the message will not be shown in MobileSMS.

    Here is the code which allows me to create proper SMS record in sms.db:

    PHP:
    // functions declaration
    CTSMSMessageCTSMSMessageCreateWithGroupAndAssociation(void*, CFStringRef numberCFStringRef messageintint);

    int _CTSMSMessageStoreGroupIDForPerson(CFStringRef number);

    int CTSMSMessageCreateGroupWithMembers(NSArray *numbers);

    .................................

        
    NSString *addressStr = @"555123456";
        
    CFStringRef address CFStringCreateWithCString(NULL, [addressStr UTF8String], kCFStringEncodingUTF8);

        
    int group _CTSMSMessageStoreGroupIDForPerson(address);  
        if (
    group 0)
        {
            
    group CTSMSMessageCreateGroupWithMembers([NSArray arrayWithObject:addressStr]);
        }
        
    CTSMSMessage *msg_to_send CTSMSMessageCreateWithGroupAndAssociation(NULLaddressCFSTR("MESSAGE TEXT"), group0);
        
    CTSMSMessageSend(ctmsg_to_send);
    After that your SMS will be displayed as outgoing message in MobileSMS.
  5. BennyLava

    BennyLava New Member

    Joined:
    Jun 5, 2008
    Messages:
    3
    Likes Received:
    0
    How do you know if the SMS was sent?
  6. Jeepston

    Jeepston New Member

    Joined:
    May 17, 2009
    Messages:
    45
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    I don't know it yet. But there is a function CTSMSMessageHasBeenSent in CoreTelephony framework. Probably it returns BOOL value. I will play with it later.
  7. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Nice, you should add that to theiphonewiki entry on CoreTelephony that I started.

    Please Register or Log in to view images

  8. Jeepston

    Jeepston New Member

    Joined:
    May 17, 2009
    Messages:
    45
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    Looks like it's not what I thought. This function returns 0 all the time.
  9. BennyLava

    BennyLava New Member

    Joined:
    Jun 5, 2008
    Messages:
    3
    Likes Received:
    0
    Here's one way to tell if the SMS was sent

    Code:
    
    static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
    {
    	NSString *IncomingNotification = [NSString stringWithFormat:@"%@", name];
    	
    	if ([@"kCTMessageSentNotification" isEqualToString:IncomingNotification]) {
    		//SMS trapped here
    	}
        return;
    }
    
    CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);	
    
  10. Jeepston

    Jeepston New Member

    Joined:
    May 17, 2009
    Messages:
    45
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    I was trying to port this piece of code to 3.0, but apparently, there is an obstacle in the form of external SQL function, which is called when I am trying to get GroupID for a phone number... So for a moment it is not possible to send SMS and record it into sms.db

    Please Register or Log in to view images




    This method:

    PHP:
    BOOL success = [[CTMessageCenter sharedMessageCenter]  sendSMSWithText:@"a message" serviceCenter:nil toAddress:@"a number"];
    doesn't leave any trace of sent message in sms.db. Looks like pretty good candidate for spyware

    Please Register or Log in to view images

Share This Page