Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why Cordova SMS plugin takes time to load for the first time? #218

Closed
maihannijat opened this issue Jan 26, 2021 · 4 comments
Closed

Why Cordova SMS plugin takes time to load for the first time? #218

maihannijat opened this issue Jan 26, 2021 · 4 comments

Comments

@maihannijat
Copy link
Contributor

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

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

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

@TravisHeaver
Copy link

I believe this has been fixed in the WIP_MMS_support branch. The issue is the new view controller is created on a different thread:
dispatch_async(dispatch_get_main_queue(), ^{ [self.viewController presentViewController:composeViewController animated:YES completion:nil]; });
this could be fixed by creating the view controller inside the async thread. see the WIP_MMS_support, which was held of for other reasons. see #14

@maihannijat
Copy link
Contributor Author

maihannijat commented Jan 26, 2021

@TravisHeaver Thanks for the reply. dispatch_async fixes the problem. I wrapped the whole block with the function.

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];
});

@dbaq
Copy link
Member

dbaq commented Jan 26, 2021

@maihannijat @TravisHeaver do you guys mind opening a PR?

@dbaq dbaq reopened this Jan 26, 2021
maihannijat added a commit to maihannijat/cordova-sms-plugin that referenced this issue Jan 26, 2021
cordova-sms#218 Wrapped the opening of text composer with `dispatch_async` to fix the main thread issue.
@dbaq
Copy link
Member

dbaq commented Feb 17, 2021

@maihannijat 's contribution has been merged. Version 1.0.1 has been published.

@dbaq dbaq closed this as completed Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants