Why IONIC/ Cordova SMS plugin takes time to load for the first time?

Viewed 111

I installed https://ionicframework.com/docs/native/sms for sending SMS from the IONIC app. The following is my implementation:

sendSMS(phoneNumber: string, text: string, event) {
    this.sms.send(String(phoneNumber), text).then(result => {
        console.log(result);
    }).catch(error => {
        console.log(error);
    });
}

I am testing this on an iOS physical device. When I click on the button to send an SMS for the first time after a fresh installation, it takes up to 30 seconds to load, but after that, it happens instantly. Even after closing the application, and restarting the phone. I looked into the Log and found:

file:xxxx/ios/capacitor-cordova-ios-plugins/sources/CordovaSmsPlugin/Sms.m: runtime: UI API called from background thread: -[UIViewController init] must be used from main thread only

Angular version 11, IONIC version 5.31.1, SMS ionic-native/sms": "^5.31.1", "cordova-sms-plugin": "^1.0.0"

1 Answers

The text composer needs to be run from the main thread. In order to do that, wrap the launch of the composer with async dispatch.

File: src/ios/Sms.m

dispatch_async(dispatch_get_main_queue(), ^{

MFMessageComposeViewController *composeViewController = [[MFMessageComposeViewController alloc] init];
composeViewController.messageComposeDelegate = self;

NSString* body = [command.arguments objectAtIndex:1];
if (body != nil) {
    BOOL replaceLineBreaks = [[command.arguments objectAtIndex:3] boolValue];
    if (replaceLineBreaks) {
        body = [body stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];
    }
    [composeViewController setBody:body];
}

NSMutableArray* recipients = [command.arguments objectAtIndex:0];
if (recipients != nil) {
    if ([recipients.firstObject isEqual: @""]) {
        [recipients replaceObjectAtIndex:0 withObject:@"?"];
    }
    
    [composeViewController setRecipients:recipients];
}

[self.viewController presentViewController:composeViewController animated:YES completion:nil];
});

Look here for more info: https://github.com/cordova-sms/cordova-sms-plugin/issues/218

Related