Skip to content

Commit

Permalink
Merge pull request #56 from cordova-sms/WIP_iOS_bg
Browse files Browse the repository at this point in the history
iOS: use a background thread
  • Loading branch information
dbaq committed Sep 27, 2015
2 parents 2cb0c7e + dff89a3 commit 98e1efd
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions src/ios/Sms.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,47 @@ - (CDVPlugin *)initWithWebView:(UIWebView *)theWebView {
}

- (void)send:(CDVInvokedUrlCommand*)command {

self.callbackID = command.callbackId;

if(![MFMessageComposeViewController canSendText]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice"
message:@"SMS Text not available."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil
];
[alert show];
return;
}

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"];
[self.commandDelegate runInBackground:^{
self.callbackID = command.callbackId;

if(![MFMessageComposeViewController canSendText]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice"
message:@"SMS Text not available."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil
];

dispatch_async(dispatch_get_main_queue(), ^{
[alert show];
});
return;
}
[composeViewController setBody:body];
}

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

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

[composeViewController setRecipients:recipients];
}

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

[composeViewController setRecipients:recipients];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.viewController presentViewController:composeViewController animated:YES completion:nil];
});
}];
}

#pragma mark - MFMessageComposeViewControllerDelegate Implementation
Expand Down

0 comments on commit 98e1efd

Please sign in to comment.