Is anyone aware of the possibility and how to send SMS programmatically via iPhone via the official SDK / Cocoa Touch?
#1 building
Use this:
- (void)showSMSPicker { Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); if (messageClass != nil) { // Check whether the current device is configured for sending SMS messages if ([messageClass canSendText]) { [self displaySMSComposerSheet]; } } } - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { //feedbackMsg.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MessageComposeResultCancelled: { UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert1 show]; [alert1 release]; } // feedbackMsg.text = @"Result: SMS sending canceled"; break; case MessageComposeResultSent: { UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert2 show]; [alert2 release]; } // feedbackMsg.text = @"Result: SMS sent"; break; case MessageComposeResultFailed: { UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert3 show]; [alert3 release]; } // feedbackMsg.text = @"Result: SMS sending failed"; break; default: { UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert4 show]; [alert4 release]; } // feedbackMsg.text = @"Result: SMS not sent"; break; } [self dismissModalViewControllerAnimated: YES]; }
#2 building
//Add the Framework in .h file #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> //Set the delegate methods UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate> //add the below code in .m file - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if([MFMessageComposeViewController canSendText]) { NSString *str= @"Hello"; controller.body = str; controller.recipients = [NSArray arrayWithObjects: @"", nil]; controller.delegate = self; [self presentModalViewController:controller animated:YES]; } } - (void)messageComposeViewController: (MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { switch (result) { case MessageComposeResultCancelled: NSLog(@"Cancelled"); break; case MessageComposeResultFailed: NSLog(@"Failed"); break; case MessageComposeResultSent: break; default: break; } [self dismissModalViewControllerAnimated:YES]; }
#3 building
XPC is one of the interprocess communication systems in Mac OS. This system layer has been developed for interprocess communication based on plist structure using libSystem and has been started. In fact, it's an interface that allows you to manage processes through structures such as an exchange dictionary. For genetic reasons, iOS 5 also has this mechanism.
You may have understood the meaning of this introduction. Yes, there are some system services in iOS, including tools for XPC communication. I want to illustrate this with a daemons for sending SMS. However, it should be noted that this feature has been fixed in iOS 6, but is related to iOS 5.0-5.1.1. No need to use jailbreak, special framework and other illegal tools can be used. Only the header file set in directory / usr / include / xpc / * is required.
One of the elements of sending SMS in iOS is system service com.apple.chatkit, whose tasks include generating, managing and sending short text messages. For the convenience of control, it has a public available communication port com.apple.chatkit.clientcomposeserver.xpc. With the XPC subsystem, you can generate and send messages without user approval.
Well, let's try to create a connection.
xpc_connection_t myConnection; dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT); myConnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
Now, we set up XPC connection myConnection as SMS sending service. However, XPC configuration can create a pending connection - we need to take further steps to activate.
xpc_connection_set_event_handler(myConnection, ^(xpc_object_t event){ xpc_type_t xtype = xpc_get_type(event); if(XPC_TYPE_ERROR == xtype) { NSLog(@"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION)); } // Always set an event handler. More on this later. NSLog(@"Received a message event!"); }); xpc_connection_resume(myConnection);
Connection activated. At this point, iOS 6 will display a message in the phone log indicating that this communication is prohibited. Now, we need to generate a dictionary similar to XPC dictionary, which contains the data needed for message sending.
NSArray *recipient = [NSArray arrayWithObjects:@"+7 (90*) 000-00-00", nil]; NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient format:200 options:0 error:NULL]; xpc_object_t mydict = xpc_dictionary_create(0, 0, 0); xpc_dictionary_set_int64(mydict, "message-type", 0); xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]); xpc_dictionary_set_string(mydict, "text", "hello from your application!");
There is little left: send the message to the XPC port and make sure it is delivered.
xpc_connection_send_message(myConnection, mydict); xpc_connection_send_barrier(myConnection, ^{ NSLog(@"The message has been successfully delivered"); });
That's it. Message sent.
#4 building
Add MessageUI.Framework and use the following code
#import <MessageUI/MessageUI.h>
Then?
if ([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc] init]; NSString *message = @"Your Message here"; [messageComposer setBody:message]; messageComposer.messageComposeDelegate = self; [self presentViewController:messageComposer animated:YES completion:nil]; }
And delegation methods-
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; }
#5 building
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; UIImage *ui =resultimg.image; pasteboard.image = ui; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]]; }