I am building an iMessage extension app that I do not intend to submit to the App Store. This app aims to send a scheduled SMS message given a date and time. Does the iOS environment offer a way to send SMS messages from a background thread? I have done research, however, most of the information I have come across is now many years old. I have attempted to use the activeConversation
instance member variable of the MSMessagesAppViewController
class to send an SMS message, however, this has issues when executing on a background thread. In my own research I have seen two additional methods of sending SMS messages, namely, ChatKit & CTMessageCenter. I have attempted the CTMessageCenter approach but to no avail. I do not know how to link the private headers from CTMessageCenter so instead I use NSInvocation to interact with CTMessageCenter. I will provide an Objective-C code snippet.
void sendSMS(NSObject* receiver, NSString* message, NSString* phoneNumber) {
SEL selector = @selector(sendSMSWithText:serviceCenter:toAddress:);
NSMethodSignature* signature = [receiver methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:receiver];
[invocation setSelector:selector];
[invocation setArgument:&message atIndex:2];
[invocation setArgument:&(NSObject*) { nil } atIndex:3];
[invocation setArgument:&phoneNumber atIndex:4];
printf("Before.\n");
[invocation invoke];
printf("After.\n");
}
I call the code from Swift like this:
let receiver = (NSClassFromString("CTMessageCenter")! as AnyObject)
.perform(NSSelectorFromString("sharedMessageCenter"))
.takeUnretainedValue() as! NSObject
sendSMS(receiver, "Your program worked!", "+10000000000")
Here is the output that I am met with in the console.
I have not tried the ChatKit approach, however, the iPhone Dev Wiki leads me to think that this is not an option for me since some of the API may only be used from the MobileSMS (iMessage) process. That information may be found here.
Does anyone know if I am doing something wrong or if there is another way to send SMS messages? Otherwise, are there any open source projects that illustrate similar functionality that I may take a look at? Thank you for any and all help.