Skip to content

Instantly share code, notes, and snippets.

@ivanbuhov
Last active January 14, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanbuhov/fa7ce5b36b1a364ab674 to your computer and use it in GitHub Desktop.
Save ivanbuhov/fa7ce5b36b1a364ab674 to your computer and use it in GitHub Desktop.
var CustomMessageCompositeViewControllerDelegate = NSObject.extend({
// pass resolve and reject functions to the delegate instance and save them as instance properties
initWithResolveReject: function(resolve, reject) {
var self = this.super.init();
if(self) {
this.resolve = resolve;
this.reject = reject;
}
return self;
},
messageComposeViewControllerDidFinishWithResult: function(controller, result) {
controller.dismissModalViewControllerAnimated(true);
console.log(result);
if(result == MessageComposeResultCancelled) {
console.log("Message Cancelled.");
this.resolve({
response: "canceled",
message: "User cancelled the message."
});
}
else if(result == MessageComposeResultSent) {
console.log("Message Sent.");
this.resolve({
response: "sent",
message: "Message sent."
});
}
else {
console.log("Something Failed.");
this.reject(Error("Message send failed."));
}
// release the delegate instance
CFRelease(controller.messageComposeDelegate);
}
}, {
protocols: [MFMessageComposeViewControllerDelegate]
});
function groupMessage(numbers, message, subject) {
return new Promise(function (resolve, reject) {
if(MFMessageComposeViewController.canSendText()){
var controller = MFMessageComposeViewController.alloc().init();
if(controller != null){
if(numbers){
controller.recipients = numbers;
}
if(message){
controller.body = message;
}
if(subject){
controller.subject = subject;
}
var delegate = CustomMessageCompositeViewControllerDelegate.alloc().initWithResolveReject(resolve, reject);
// retain the delegate because messageComposeDelegate property won't do it for us
CFRetain(delegate);
controller.messageComposeDelegate = delegate;
var page = frameModule.topmost().ios.controller;
page.presentModalViewControllerAnimated(controller, true);
}else{
reject(Error("You're not able to send SMS messages. Please check device settings."));
}
} else {
reject(Error("You're not able to send SMS messages. Please check device settings."));
}
});
}
@sitefinitysteve
Copy link

Any chance you could elaborate on this for us so we know why this needs to happen in the future (or update the official docs)? I haven't seen needing to pass resolve\reject in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment